Search |
||
How to rock around the clock?Posted by malenkov on December 18, 2008 at 12:00 AM PST
JavaFX has been released recently and now many beginners google on how to start programming. Let us consider a very simple example of drawing a clock face in JavaFX. First, draw a second hand. Pay attention to the position of the hand relative to the origin of coordinates, which will be used as a center of rotation. For example, rotate the hand by 60° (10 seconds).
Rectangle {
x: - 2
y: - 100
width: 4
height: 120
arcWidth: 4
arcHeight: 4
transforms: Rotate {
angle: 60
}
}
Now draw the minute hand and rotate it, for example, by 301° (50 minutes and 10 seconds).
Rectangle {
x: - 4
y: - 80
width: 8
height: 100
arcWidth: 8
arcHeight: 8
transforms: Rotate {
angle: 301
}
}
Next, draw the hour hand and rotate it, for example, by 175° (5 hours and 50 minutes).
Rectangle {
x: - 6
y: - 60
width: 12
height: 80
arcWidth: 12
arcHeight: 12
transforms: Rotate {
angle: 175
}
}
Finally, mark the clock face. Draw 12 ticks with incremental rotation of 30°.
for (i in [1 .. 12]) Rectangle {
x: - 2
y: - 100
width: 4
height: 20
arcWidth: 4
arcHeight: 4
transforms: Rotate {
angle: 30 * i
}
}
For clock refresh I use an indefinite timeline which updates current time every second by using the
def timer = Timeline {
repeatCount: Timeline.INDEFINITE
keyFrames: KeyFrame {
time: 1s
action: function() {
calendar = Calendar.getInstance()
}
}
}
Note that the code above uses a Java class, the
function getCalendar() {
def calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar
}
In this example, the clock face is declared as a custom node so that the clock widget can be used in other applications. To further customize, set the following instance variables:
To create an applet with the clock widget, a simple JavaFX application was developed. There are some interesting details that should be described. First, in order to use transparent windows, you should not only set the transparent style of the stage, but also remove the background color of the scene.
var stage: Stage = Stage {
title: "Clock (JavaFX sample)"
style: StageStyle.TRANSPARENT
scene: Scene {
fill: null
width: 320
height: 320
content: Clock {
paused: false
...
Secondly, having the clock in the center of the screen is annoying. So adding an ability to drag the window would be good.
onMouseDragged: function(event) {
stage.x += event.dragX;
stage.y += event.dragY;
}
Finally, a linear gradient is created automatically using the colors sequence.
def colors = [ Color.CYAN Color.GREEN Color.YELLOW ];
...
stroke: colors[sizeof colors / 2]
fill: LinearGradient {
def factor = sizeof colors - 1.0;
stops: for (color in colors) Stop {
color: color
offset: indexof color / factor
}
}
Now you are prepared to look at the AnalogClock sample to learn how to add text labels for each tick and much more.
My other samples are: Draw, FlyingLetters, Magnetism, VideoCube, and WeatherWidget. »
Related Topics >>
Programming Comments
Comments are listed in date ascending order (oldest first)
Submitted by kirillcool on Wed, 2008-12-17 11:36.
Do you really want to prevent people that have the right Java installed from reading your blog. The dtfx.js script kicks me out of this page telling me to install Java (which i already have, including 6u10, 6u11 and 6u12). I had to blacklist javafx.com in NoScript extension to be able to read this blog.
Submitted by luggypm on Thu, 2008-12-18 00:59.
Nice example but one small criticism - it's stuck on my primary monitor. When I try to drag it off to the second monitor it refuses and stays at the locked at the screen border whilst my mouse continues on to the other monitor.
I know Swing behaves properly in these circumstances but it's a pretty poor thing for JavaFX to behave like this .
Submitted by malenkov on Thu, 2008-12-18 03:46.
Kirill, I've commented out FX applet. I'll try to solve the problem.
Submitted by malenkov on Thu, 2008-12-18 03:48.
The application can't be dragged because of security. Seems that the yellow triangle prevents dragging to the second monitor. I'll file a bug.
Submitted by arreumb on Thu, 2008-12-18 07:42.
You can drag it, just point the cursor inside one of the hands
Submitted by imichtch on Thu, 2008-12-18 11:40.
Nice example. But... could anyone explain why a new "language"/script has been invented? Could the same stuff be "formulated" using the good-old Java? What is the point really?
Submitted by malenkov on Thu, 2008-12-18 11:56.
You can use SceneGraph library in Java, but JavaFX script provides declarative syntax that simplifies creation of Java2D scenes.
|
||
|
|