DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. 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 · Interview
Like (0)
Save
Tweet
Share
13.76K 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 Build a Spring Boot GraalVM Image
  • Real-Time Analytics for IoT
  • Getting a Private SSL Certificate Free of Cost
  • Introduction To OpenSSH

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: