samples/time_color.fx

Fri, 05 Jun 2015 12:38:53 +0200

author
mhaupt
date
Fri, 05 Jun 2015 12:38:53 +0200
changeset 1398
2f1b9f4daec1
parent 1371
a8c536d1d3e0
permissions
-rw-r--r--

8080087: Nashorn $ENV.PWD is originally undefined
Summary: On Windows, the PWD environment variable does not exist and cannot be imported in scripting mode, so it is set explicitly.
Reviewed-by: lagergren, sundar

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

mercurial