今天看到这位博客的博主,把Chris Oliver 写的时钟改了一些代码,变成这个样子,呵呵,有兴趣的朋友可以参考下学习下面的源代码:
1import javafx.ui.*;
2import javafx.ui.canvas.*;
3import java.util.Date;
4
5public class Timer {
6 private attribute elapsed: Number;
7 public attribute minutes: Number;
8 public attribute seconds: Number;
9 public attribute hours: Number;
10 public attribute running: Boolean;
11}
12
13attribute Timer.elapsed = bind
14if running then
15[1..60] dur 60000 linear
16while running
17continue if true
18else 0;
19
20trigger on Timer.elapsed = value {
21 var now = new Date();
22 minutes = now.getMinutes();
23 seconds = now.getSeconds() +
24 [.35,0.34 .. 0.0] dur 350 +
25 (now.getTime() % 1000)/1000;
26 hours = now.getHours();
27}
28
29public class Clock extends CompositeNode {
30 public attribute ticking: Boolean;
31}
32
33operation Clock.composeNode() {
34 var t = Timer {running: bind ticking};
35 return Group {
36 transform: [translate(5,5),scale(1,1)]
37 var secs = bind t.seconds
38 var mins = bind t.minutes + secs/60
39 var hrs = bind t.hours + mins/60
40 content:
41 [
42 Rect {
43 height: 200
44 width: 200
45 fill: white
46 },
47 ImageView {
48 transform: []
49 image: Image {url: "http://sellmic.com/lab/dev/jfx/clock/images/clock_face.png"}
50 },
51 Group {
52 var hourHand =
53 ImageView {
54 transform: bind rotate(hrs*30,255,245)
55 image: Image {url: "http://sellmic.com/lab/dev/jfx/clock/images/hour_hand.png"}
56 }
57
58 var minuteHand =
59 ImageView {
60 transform: bind rotate(mins *6,255,245)
61 image: Image {url: "http://sellmic.com/lab/dev/jfx/clock/images/minute_hand.png"}
62 }
63
64 var secondHand =
65 ImageView {
66 transform: bind rotate(t.seconds * 6,255,245)
67 image: Image {url: "http://sellmic.com/lab/dev/jfx/clock/images/second_hand.png"}
68 }
69
70 content: [hourHand, minuteHand, secondHand]
71 },
72 ImageView {
73 transform: []
74 image: Image {url: "http://sellmic.com/lab/dev/jfx/clock/images/pin.png"}
75 }]
76 };
77}
78
79Clock {ticking: true}
80
81