Roll the Dice - A Compiled JavaFX Script Example
Join the DZone community and get the full member experience.
Join For Freei'm on my way to vegas to speak at the server side java symposium , so in keeping with that theme i wanted to create a simple dice program as today's example. when you click the roll button, random values appear on the dice. here's a screenshot of this application:
gamemain.fx
/*
* gamemain.fx -
* a compiled javafx program that demonstrates creating custom
* components with compositenode
*
*
* developed 2008 by james l. weaver (jim.weaver at lat-inc.com)
* to serve as a javafx script example.
*/
import javafx.ui.*;
import javafx.ui.canvas.*;
import java.lang.system;
frame {
var model = gamemodel {}
width: 210
height: 170
title: "dice"
background: color.white
content:
borderpanel {
center:
canvas {
content:
for (dicenum in [0 .. model.numdice - 1]) {
model.newdice =
dice {
x: dicenum * 100 + 10
y: 10
width: 80
height: 80
facecolor: color.red
pipcolor: color.white
}
}
}
bottom:
flowpanel {
content:
button {
text: "roll"
defaultbutton: true
action:
function():void {
model.roll();
}
}
}
}
visible: true
onclose:
function():void {
system.exit(0);
}
}
dice.fx
/*
* dice.fx -
* a compiled javafx program that demonstrates creating custom
* components with compositenode
*
*
* developed 2008 by james l. weaver (jim.weaver at lat-inc.com)
* to serve as a javafx script example.
*/
import javafx.ui.*;
import javafx.ui.canvas.*;
import java.lang.math;
import java.lang.system;
public class dice extends compositenode {
public attribute value:integer = 5;
public attribute x:integer;
public attribute y:integer;
public attribute width:integer;
public attribute height:integer;
public attribute facecolor:color;
public attribute pipcolor:color;
attribute pipplace:pipplacement[];
postinit {
insert
pipplacement {
piplocsx: [
.5, 0, 0, 0, 0, 0
]
piplocsy: [
.5, 0, 0, 0, 0, 0
]
}
into pipplace;
insert
pipplacement {
piplocsx: [
0.3, 0.7, 0, 0, 0, 0
]
piplocsy: [
0.7, 0.3, 0, 0, 0, 0
]
}
into pipplace;
insert
pipplacement {
piplocsx: [
0.2, 0.5, 0.8, 0, 0, 0
]
piplocsy: [
0.8, 0.5, 0.2, 0, 0, 0
]
}
into pipplace;
insert
pipplacement {
piplocsx: [
0.25, 0.25, 0.75, 0.75, 0, 0
]
piplocsy: [
0.25, 0.75, 0.25, 0.75, 0, 0
]
}
into pipplace;
insert
pipplacement {
piplocsx: [
0.2, 0.5, 0.8, 0.8, 0.2, 0
]
piplocsy: [
0.8, 0.5, 0.2, 0.8, 0.2, 0
]
}
into pipplace;
insert
pipplacement {
piplocsx: [
0.3, 0.3, 0.3, 0.7, 0.7, 0.7
]
piplocsy: [
0.8, 0.5, 0.2, 0.8, 0.5, 0.2
]
}
into pipplace;
}
public function roll():void {
value = (math.random() * 6 + 1) as integer;
}
public function composenode():node {
group {
transform: bind [
translate.translate(x, y)
]
content: bind [
rect {
x: 0
y: 0
width: this.width;
height: this.height;
fill: facecolor
},
for (pipidx in [0 .. value-1]) {
circle {
cx: bind pipplace[value - 1].piplocsx[pipidx] * width
cy: bind pipplace[value - 1].piplocsy[pipidx] * height
radius: width * .1
fill: pipcolor
}
}
]
}
}
}
gamemodel.fx
/*
* gamemodel.fx -
* the model behind the dice game
*
* developed 2008 by james l. weaver (jim.weaver at lat-inc.com)
* to serve as a javafx script example.
*/
class gamemodel {
attribute numdice:integer = 2;
attribute newdice:dice on replace {
insert newdice into dice;
}
attribute dice:dice[];
function roll():void {
for (die in dice) {
die.roll();
}
}
}
pipplacement.fx
/*
* pipplacement.fx -
* the placement of the pips on a dice
*
* developed 2008 by james l. weaver (jim.weaver at lat-inc.com)
* to serve as a javafx script example.
*/
public class pipplacement {
public attribute piplocsx:number[];
public attribute piplocsy:number[];
}
good luck!
jim weaver
javafx script: dynamic java scripting for rich internet/client-side applications
immediate ebook (pdf) download available at
the book's apress site
Opinions expressed by DZone contributors are their own.
Comments