DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Roll the Dice - A Compiled JavaFX Script Example

Roll the Dice - A Compiled JavaFX Script Example

James Weaver user avatar by
James Weaver
·
Mar. 22, 08 · Java Zone · Interview
Like (0)
Save
Tweet
13.28K Views

Join the DZone community and get the full member experience.

Join For Free

i'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:

dice

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

JavaFX Script JavaFX

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Leverage Method Chaining To Add Smart Message Routing in Java
  • 6 Quick Tips for Building an App
  • Privacy and the 7 Laws of Identity
  • What Is Cloud-Native Architecture?

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo