Part Deux of Building a Calculator with the Shiny New JavaFX SDK 1.0
Join the DZone community and get the full member experience.
Join For Freein the first post of this simple calculator app series, we took some baby steps toward creating a simple calculator. we're using the comp, shown below, that graphics designer mark dingman of malden labs supplied:
for teaching purposes, i'm taking a very incremental and sometimes indirect approach to building this. for example, in the first post i used the swingbutton class for the calculator keys. in this post, we'll use a group that consists of a rectangle and text object to create each key. also, we'll use a for expression to succinctly create the keys -- a technique that has another benefit related to event handling that you'll see in a moment. and remember, the calculator in its current state simply displays the keys typed rather than performing any calculations. we'll create that code when showing you how to define classes, functions, etc. in javafx.
click on the calculator image above to invoke the simple calculator javafx applet, or the java web start icon below to make the application appear in its own window:
understanding the code behind this javafx applet
take a look at the code below, and note what has changed since its first incarnation .
/* * simplecalc2.fx * * developed 2008 by james l. weaver (jim.weaver at javafxpert.com) * to demonstrate creating applications using javafx sdk 1.0 */ package javafxpert; import javafx.ext.swing.*; import javafx.stage.stage; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.input.*; import javafx.scene.layout.*; import javafx.scene.paint.*; import javafx.scene.shape.*; import javafx.scene.text.*; import javafx.scene.transform.*; /** * the "stage" for the application */ stage { // the model var displaystr:string; var keylabels = ["7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "x", "0", ".", "=", "/"]; title: "calculator 2" scene: scene { // the gradient on the calculator keys var btngradient = lineargradient { startx: 0.0, starty: 0.0, endx: 0.0, endy: 1.0 stops: [ stop { offset: 0.0 color: color.rgb(166, 166, 168) }, stop { offset: 1.0 color: color.rgb(122, 122, 122) } ] }; content: [ // the rounded rectangle behind the entire calculator rectangle { width: 278 height: 401 arcwidth: 16 archeight: 16 fill: lineargradient { startx: 0.0, starty: 0.0, endx: 0.5, endy: 1.0 stops: [ stop { offset: 0.0 color: color.rgb(119, 116, 123) }, stop { offset: 1.0 color: color.rgb(2, 2, 2) } ] }, }, vbox { transforms: bind translate.translate(20, 20) spacing: 27 content: [ // the calculator's display textbox { text: bind displaystr width: 238 height: 65 editable: false style: bind "text-fill: #343434; " "background-fill: #f4f4f4; " "font: 28pt dialog; " "font-weight: bold; " }, // the calculator's keys vbox { spacing: 2 content: for (row in [0..3]) { hbox { spacing: 2 content: for (column in [0..3]) { group { var rectref:rectangle; var textref:text; content: [ rectref = rectangle { width: 58 height: 50 fill: btngradient }, textref = text { transforms: bind translate.translate((rectref.layoutbounds.width - textref.layoutbounds.width) / 2, (rectref.layoutbounds.height - textref.layoutbounds.height) / 2) content: keylabels[row * 4 + column] textorigin: textorigin.top fill: color.rgb(222, 222, 222) font: font { name: "arial bold" size: 30 } } ] onmouseclicked: function(me:mouseevent):void { displaystr = "{displaystr}{textref.content}" } } } } } } ] } ] } }
notice the for expressions that creates an hbox for each row, and a group for each calculator key in a row. the value of a for expression is a sequence (think array) of objects. more information on the for expression can be found in the javafx language reference (in development at this writing).
another noteworthy item is the value of the transforms attribute in the text object. the translate.translate function above causes the text to be centered horizonally and vertically within the rectangle.
one of the advantages of using the for expression is that only one event handler is required for all of the keys, rather than one for each key as in the previous simple calculator version. in this case the onmouseclicked attribute is on the group that contains the rectangle and text.
one more item of note is the use of the lineargradient class to make the application begin looking more like the graphic comp shown above. consult the javafx api docs for more information on using this class. by the way, the api documentation is available online as well as being in the javafx sdk.
please leave a comment if you have any questions!
regards,
jim weaver
javafxpert.com
Opinions expressed by DZone contributors are their own.
Comments