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