sundar@1371: #// Usage: jjs -fx time_color.js [-- true/false] sundar@1371: sundar@1371: /* sundar@1371: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. sundar@1371: * sundar@1371: * Redistribution and use in source and binary forms, with or without sundar@1371: * modification, are permitted provided that the following conditions sundar@1371: * are met: sundar@1371: * sundar@1371: * - Redistributions of source code must retain the above copyright sundar@1371: * notice, this list of conditions and the following disclaimer. sundar@1371: * sundar@1371: * - Redistributions in binary form must reproduce the above copyright sundar@1371: * notice, this list of conditions and the following disclaimer in the sundar@1371: * documentation and/or other materials provided with the distribution. sundar@1371: * sundar@1371: * - Neither the name of Oracle nor the names of its sundar@1371: * contributors may be used to endorse or promote products derived sundar@1371: * from this software without specific prior written permission. sundar@1371: * sundar@1371: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS sundar@1371: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, sundar@1371: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sundar@1371: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR sundar@1371: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, sundar@1371: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, sundar@1371: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR sundar@1371: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF sundar@1371: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING sundar@1371: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS sundar@1371: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. sundar@1371: */ sundar@1371: sundar@1371: // A simple javafx program that changes background color sundar@1371: // of scene based on current time value (once per sec). sundar@1371: // inspired by http://whatcolourisit.scn9a.org/ sundar@1371: sundar@1371: if (!$OPTIONS._fx) { sundar@1371: print("Usage: jjs -fx time_color.js"); sundar@1371: print(" jjs -fx time_color.js -- true"); sundar@1371: exit(1); sundar@1371: } sundar@1371: sundar@1371: // JavaFX classes used sundar@1371: var Color = Java.type("javafx.scene.paint.Color"); sundar@1371: var Group = Java.type("javafx.scene.Group"); sundar@1371: var Label = Java.type("javafx.scene.control.Label"); sundar@1371: var Platform = Java.type("javafx.application.Platform"); sundar@1371: var Scene = Java.type("javafx.scene.Scene"); sundar@1371: var Timer = Java.type("java.util.Timer"); sundar@1371: sundar@1371: // execute function periodically once per given time in millisec sundar@1371: function setInterval(func, ms) { sundar@1371: // New timer, run as daemon so the application can quit sundar@1371: var timer = new Timer("setInterval", true); sundar@1371: timer.schedule(function() Platform.runLater(func), ms, ms); sundar@1371: return timer; sundar@1371: } sundar@1371: sundar@1371: // do you want to flip hour/min/sec for RGB? sundar@1371: var flip = arguments.length > 0? "true".equals(arguments[0]) : false; sundar@1371: sundar@1371: // JavaFX start method sundar@1371: function start(stage) { sundar@1371: start.title = "Time Color"; sundar@1371: var root = new Group(); sundar@1371: var label = new Label("time"); sundar@1371: label.textFill = Color.WHITE; sundar@1371: root.children.add(label); sundar@1371: stage.scene = new Scene(root, 700, 500); sundar@1371: sundar@1371: setInterval(function() { sundar@1371: var d = new Date(); sundar@1371: var hours = d.getHours(); sundar@1371: var mins = d.getMinutes(); sundar@1371: var secs = d.getSeconds(); sundar@1371: sundar@1371: if (hours < 10) hours = "0" + hours; sundar@1371: if (mins < 10) mins = "0" + mins; sundar@1371: if (secs < 10) secs = "0" + secs; sundar@1371: sundar@1371: var hex = flip? sundar@1371: "#" + secs + mins + hours : "#" + hours + mins + secs; sundar@1371: label.text = "Color: " + hex; sundar@1371: stage.scene.fill = Color.web(hex); sundar@1371: }, 1000); sundar@1371: sundar@1371: stage.show(); sundar@1371: }