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