nashorn/samples/time_color.fx
author duke
Wed, 05 Jul 2017 20:21:13 +0200
changeset 29057 d6224d602145
parent 28126 9e60ca1ed968
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28126
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
     1
#// Usage: jjs -fx time_color.js [-- true/false]
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
     2
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
     3
/*
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
     4
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
     5
 *
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
     6
 * Redistribution and use in source and binary forms, with or without
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
     7
 * modification, are permitted provided that the following conditions
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
     8
 * are met:
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
     9
 *
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    10
 *   - Redistributions of source code must retain the above copyright
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    11
 *     notice, this list of conditions and the following disclaimer.
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    12
 *
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    13
 *   - Redistributions in binary form must reproduce the above copyright
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    14
 *     notice, this list of conditions and the following disclaimer in the
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    15
 *     documentation and/or other materials provided with the distribution.
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    16
 *
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    17
 *   - Neither the name of Oracle nor the names of its
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    18
 *     contributors may be used to endorse or promote products derived
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    19
 *     from this software without specific prior written permission.
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    20
 *
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    21
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    22
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    23
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    24
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    25
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    26
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    28
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    29
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    30
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    31
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    32
 */
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    33
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    34
// A simple javafx program that changes background color
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    35
// of scene based on current time value (once per sec).
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    36
// inspired by http://whatcolourisit.scn9a.org/
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    37
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    38
if (!$OPTIONS._fx) {
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    39
    print("Usage: jjs -fx time_color.js");
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    40
    print("       jjs -fx time_color.js -- true");
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    41
    exit(1);
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    42
}
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    43
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    44
// JavaFX classes used
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    45
var Color = Java.type("javafx.scene.paint.Color");
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    46
var Group = Java.type("javafx.scene.Group");
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    47
var Label = Java.type("javafx.scene.control.Label");
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    48
var Platform = Java.type("javafx.application.Platform");
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    49
var Scene = Java.type("javafx.scene.Scene");
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    50
var Timer = Java.type("java.util.Timer");
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    51
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    52
// execute function periodically once per given time in millisec
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    53
function setInterval(func, ms) {
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    54
    // New timer, run as daemon so the application can quit
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    55
    var timer = new Timer("setInterval", true);
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    56
    timer.schedule(function() Platform.runLater(func), ms, ms);	
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    57
    return timer;
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    58
}
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    59
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    60
// do you want to flip hour/min/sec for RGB?
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    61
var flip = arguments.length > 0? "true".equals(arguments[0]) : false;
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    62
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    63
// JavaFX start method
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    64
function start(stage) {
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    65
    start.title = "Time Color";
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    66
    var root = new Group();
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    67
    var label = new Label("time");
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    68
    label.textFill = Color.WHITE;
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    69
    root.children.add(label); 
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    70
    stage.scene = new Scene(root, 700, 500);
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    71
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    72
    setInterval(function() {
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    73
        var d = new Date();
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    74
        var hours = d.getHours();
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    75
	var mins = d.getMinutes();
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    76
	var secs = d.getSeconds();
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    77
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    78
        if (hours < 10) hours = "0" + hours;
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    79
        if (mins < 10) mins = "0" + mins;
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    80
        if (secs < 10) secs = "0" + secs;
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    81
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    82
	var hex = flip?
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    83
            "#" + secs + mins + hours : "#" + hours + mins + secs;
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    84
        label.text = "Color: " + hex;
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    85
        stage.scene.fill = Color.web(hex);
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    86
    }, 1000);
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    87
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    88
    stage.show();
9e60ca1ed968 8067636: ant javadoc target is broken
sundar
parents:
diff changeset
    89
}