Coding By Numbers
Rambling about Java, Swing, JavaFX, Android and iPhone development
Rambling about Java, Swing, JavaFX, Android and iPhone development
Dec 21st
As promised, the source code for the Laser Orbit demo is now available as a NetBeans (ANT) project under the MIT license. I refined the source since I originally wrote the demo, as I’ve learned more about JavaFX writing other demos (coming soon). As a result I’ve cut the code down by a third and I’m left with something that is almost entirely declarative.
def FPS = 24;
def SPRINGNESS = 0.2;
def DECAY = 0.8;
def SPAN_DISTANCE = 100;
var current = Point {}
var mouse = Point {x:225 y:200}
var previous = Point {}
var target = Point {}
var temp = Point{}
var random = new Random();
var scene: Scene;
Stage {
title: "Laser Orbit"
width: 550
height: 400
scene: scene = Scene {
content: [
Rectangle {
x: 0
y: 0
width: bind scene.width
height: bind scene.height
fill: Color.BLACK
onMouseMoved: function(event: MouseEvent) {
mouse.x = event.x;
mouse.y = event.y;
}
}
]
}
}
class Point {
var x = 0.0;
var y = 0.0;
}
Timeline {
keyFrames: [
KeyFrame {
time: 1000ms / FPS
action: function() {
previous.x = current.x;
previous.y = current.y;
target.x = mouse.x + (SPAN_DISTANCE - SPAN_DISTANCE * 2 * random.nextDouble());
target.y = mouse.y + (SPAN_DISTANCE - SPAN_DISTANCE * 2 * random.nextDouble());
temp.x = temp.x * DECAY + (target.x - current.x) * SPRINGNESS;
temp.y = temp.y * DECAY + (target.y - current.y) * SPRINGNESS;
current.x = current.x + temp.x;
current.y = current.y + temp.y;
var line = Line {
startX: previous.x
startY: previous.y
endX: current.x
endY: current.y
strokeWidth: 1.5
stroke: Color.BLUE
}
insert line into scene.content;
Timeline {
keyFrames: [
KeyFrame {
time: 0.0s
values: {line.opacity => 1.0}
},
KeyFrame {
time: 1.0s
values: {line.opacity => 0.0 tween Interpolator.LINEAR}
action: function() {
delete line from scene.content;
}
}
]
}.play();
}
}
]
repeatCount: Timeline.INDEFINITE
}.play();
Download the NetBeans project.
Dec 13th
This is a demo that I ported from www.shinedraw.com as an exercise to learn a bit more about JavaFX. Basically the line should follow and dance around the mouse pointer.
Note: I did have this embedded in the page as an applet but it appears to crash FireFox on Linux (not good) so for now I have changed it to a web start application: Run demo with Java webstart.
I learned quite a lot about JavaFX in the process of porting this little example, and I will publish the source and dissect it in an upcoming post. For now, I’ll just say that there is a lot that I like about JavaFX script, but the platform feels quite incomplete for a 1.0 release. Even for this relatively simple example I had to lean on Java on more than one occasion. Fortunately, calling on it’s big brother for help is pretty seamless, so I don’t think it will hold it back.