Jim has posted 16 posts at DZone. View Full User Profile

Bindstorming

05.13.2009
| 3387 views |
  • submit to reddit

It is within our nature, even in the most infinitesimal way, to leave our mark on this world before we exit it.  I'd like to coin the following term, heretofore unseen in the JavaFX space, and submit it as my humble contribution to the human collective:

bindstorm \'bïnd•storm\ (noun): condition where a multitude of JavaFX bind recalculations severely hampers interactive performance

Yeah, I know, using the word you wish to define inside its definition is bad, but there is precedent for this: (1) Fancy-schmancy, hoity-toity college dictionaries do it all the time. (2) Mathematicians and computer scientists call this recursion: that mysterious concept which developers use to impress others of their programming prowess.

Don't get me wrong, JavaFX binding is incredibly powerful.  Heck, we dedicated a whole chapter to it in our soon-to-be-released book JavaFX: Developing Rich Internet Applications.  But binding does come with a price, and like most anything else, over-consumption can lead to abuse.

Consider this use case: you've got a JavaFX application with dozens or maybe even hundreds of Nodes that are part of the scenegraph.  Each of the Nodes are ultimately sized and positioned in proportion to height and width instance variables that are passed on down.  If you define width and height at startup and have no interest in a resizeable interface, then you stand a good chance of avoiding the use of many bind expressions.  The one potential twist here is that if you're sincerely interested in a non-resizeable application, but want it to consume the entire screen, what do you do?  As screens come in all shapes and sizes, you may not know what the resolution is at start time.  JavaFX has an elegant solution for this which uses binding.

Here's a simple application which defines a Rectangle and Circle that fill the entire screen.  You can click anywhere within the Circle to exit the application.  Notice the number of binds required to get this to work.


import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;
import javafx.scene.input.*;

function run() : Void {
var stage: Stage = Stage {
fullScreen: true
scene: Scene {
content: [
Rectangle {
width: bind stage.width
height: bind stage.height
fill: Color.BLUE
}
Circle {
centerX: bind stage.width / 2
centerY: bind stage.height / 2
radius: bind if (stage.width < stage.height) then
stage.width / 2 else stage.height / 2
fill: Color.RED
onMouseClicked: function(me: MouseEvent) {
FX.exit();
}
}
]
}
}
}

Imagine what this would look like if you had lots of complex custom components with many more dependencies on height and width.  In addition to the potential performance impact, this could be error-prone and cumbersome to code.  To avoid the over usage of binding and the potential for a bindstorm, applications of this sort could be re-written as follows:


import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;
import javafx.scene.input.*;

function run() : Void {
var AWTtoolkit = java.awt.Toolkit.getDefaultToolkit ();
var screenSizeFromAWT = AWTtoolkit.getScreenSize ();
Stage {
fullScreen: true
scene: Scene {
content: [
Rectangle {
width: screenSizeFromAWT.width
height: screenSizeFromAWT.height
fill: Color.BLUE
}
Circle {
centerX: screenSizeFromAWT.width / 2
centerY: screenSizeFromAWT.height / 2
radius: if (screenSizeFromAWT.width <
screenSizeFromAWT.height) then
screenSizeFromAWT.width / 2
else screenSizeFromAWT.height / 2
fill: Color.RED
onMouseClicked: function(me: MouseEvent) {
FX.exit();
}
}
]
}
}
}

We achieve the same effect as the first example by first making a call to a method in the java.awt.Toolkit package.  With this information we can statically define our scenegraph without the use of binding.

There is one caveat to this solution.  As the AWT (Advanced Windowing Toolkit) is an integral part of Java SE, this code should be portable across all JavaFX desktops.  However, if you wish to deploy a JavaFX Mobile solution, the AWT calls would likely change.  Is there a mechanism that might work across both models?

As a final thought, while we're on this theme of coining terms, my compadres Jim Clarke and Eric Bruno, co-authors of the aforementioned JavaFX book, jokingly asked what word could be used to describe this scenario:

"Condition where binds lead to binds that leads back to the original bind, ending up in a Stack fault?"

BindQuake? BindTsunami? Bindless? BindSpin? BindHole (BlackHole)? BindPit?

From http://blogs.sun.com/jtc/

Published at DZone with permission of its author, Jim Connors.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Andrew Melmoth replied on Wed, 2009/05/13 - 11:46am

Bind Panic?

Andrew McVeigh replied on Wed, 2009/05/13 - 1:24pm in response to: Andrew Melmoth

the bind leading the bind?

Geertjan Wielenga replied on Fri, 2009/05/15 - 8:46am in response to: Andrew McVeigh

Haha.

dfdf dfd replied on Wed, 2009/06/24 - 9:34pm

I like the ed hardy clothing. ed hardy is one of the most popular brands. ed hardy clothes displays the brilliant work of Don ed hardy uk. He is a gifted painter, printmaker and tattoo artist. cheap ed hardy offerings include ed hardy tank, cheap ed hardy clothing, ed hardy on sale, ed hardy handbags, ed hardy discount etc. ed hardy swimwear is just 4 years old and was launched by Audigier in 2004. There were many Hollywood stars who wear his ed hardy swimwear. Some of the famous celebrities include Ed Hardy Belts, Jessica Alba, Mariah Carey, Paris Hilton etc.

abu jabie replied on Tue, 2009/08/11 - 11:59pm in response to: dfdf dfd

    The bind leading the bind?     MKV to MP4

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.