<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jayway Team Blog &#187; javafx</title>
	<atom:link href="http://blog.jayway.com/tag/javafx/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jayway.com</link>
	<description>Sharing Experience</description>
	<lastBuildDate>Sat, 11 Feb 2012 10:33:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Transparent bind of JavaFX and POJOs</title>
		<link>http://blog.jayway.com/2009/05/12/transparent-bind-of-javafx-and-pojos/</link>
		<comments>http://blog.jayway.com/2009/05/12/transparent-bind-of-javafx-and-pojos/#comments</comments>
		<pubDate>Tue, 12 May 2009 12:24:06 +0000</pubDate>
		<dc:creator>Magnus Robertsson</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[javafx]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=1517</guid>
		<description><![CDATA[When I started coding JavaFX I quickly found that the great bind mechanism doesn't work together very well with my legacy Java code. In fact, it doesn't work at all. In my case I wanted to reuse my domain objects and just add a fancy user interface on top of it. This happen to be [...]]]></description>
			<content:encoded><![CDATA[<p>When I started coding JavaFX I quickly found that the great <a href="http://openjfx.java.sun.com/current-build/doc/reference/Binding.html">bind mechanism</a> doesn't work together very well with my legacy Java code. In fact, it doesn't work at all. In my case I wanted to reuse my domain objects and just add a fancy user interface on top of it. This happen to be harder that I anticipated. Very annoying. Since bind is a new feature in JavaFX and all primitive types have been replaced with special object equivalents they can't interoperate with the Java primitive types. (This is another reasons why primitive types should have been objects in Java from the start.)</p>
<p>After searching on Google I found that there are two solutions for the problem which both includes an adapter class:</p>
<ul>
<li>Polling - The adapter class polls for changes in the POJO. While this solution is transparent to our POJOs it does not keep the state synchronised and is not very resource efficient.</li>
<li>Observer-pattern - The adapter class listens for changes in the POJO. This implies that we add listener mechanism to our POJOs and thus not very transparent.
</li>
</ul>
<p>Michael Heinrichs at Sun have a <a href="http://blogs.sun.com/michaelheinrichs/entry/binding_java_objects_in_javafx">great post</a> about it. The problem is that none of these solutions are really adequate.</p>
<h3>The best of both worlds</h3>
<p>Since I didn't want to change my POJOs or coding adapters I had to come up with a new strategy. We've seen how listener mechanisms can be added using AOP. Great! By adding an aspect we can mixin the functionality we need for JavaFX to be able to bind to my POJOs. Furthermore we can generate the adapters. We only need to decide what fields we need to bind to in our POJOs. These will serve as our pointcuts. The default strategy would be to select all JavaBean properties. What a great day for coding a tool coding! The result is an easy to use tool that I hosted on Google Code:</p>
<p>http://code.google.com/p/javafxbinder/</p>
<p>For example if I have a POJO like this:</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">package</span> se.<span style="color: #006600;">jayway</span>.<span style="color: #006600;">javafxbinder</span>.<span style="color: #006600;">example</span>.<span style="color: #006600;">domain</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> name;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">int</span> age;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Person<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> getName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> name;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setName<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> name<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Name was set: &quot;</span> + name<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">name</span> = name;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">int</span> getAge<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> age;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setAge<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> age<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">age</span> = age;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>Using JavaFX binder I can run the following command to generate some JavaFX code:</p>
<pre>javafxbinder.sh -classpath ./target/classes -includes se.jayway.javafxbinder.example.domain.* -out ./output</pre>
<p>And here is what gets generated:</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">package</span> se.<span style="color: #006600;">jayway</span>.<span style="color: #006600;">javafxbinder</span>.<span style="color: #006600;">example</span>.<span style="color: #006600;">domain</span>;
&nbsp;
<span style="color: #a1a100;">import se.jayway.javafxbinder.example.domain.Person;</span>
<span style="color: #a1a100;">import se.jayway.javafxbinder.infrastructure.Bean;</span>
<span style="color: #a1a100;">import java.beans.PropertyChangeEvent;</span>
<span style="color: #a1a100;">import java.beans.PropertyChangeListener;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JfxPerson <span style="color: #000000; font-weight: bold;">extends</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3APropertyChangeListener+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">PropertyChangeListener</span></a> <span style="color: #66cc66;">&#123;</span>
    public-init var pojo : Person on replace oldValue <span style="color: #66cc66;">&#123;</span>
        var oldBean : Bean = oldValue as Bean;
        oldBean.<span style="color: #006600;">removePropertyChangeListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        var newBean : Bean = pojo as Bean;
        newBean.<span style="color: #006600;">addPropertyChangeListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> var age : <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AInteger+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Integer</span></a> = pojo.<span style="color: #006600;">getAge</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> on replace <span style="color: #66cc66;">&#123;</span>
        pojo.<span style="color: #006600;">setAge</span><span style="color: #66cc66;">&#40;</span>age<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>;
    <span style="color: #000000; font-weight: bold;">public</span> var name : <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> = pojo.<span style="color: #006600;">getName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> on replace <span style="color: #66cc66;">&#123;</span>
        pojo.<span style="color: #006600;">setName</span><span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>;
&nbsp;
    override <span style="color: #000000; font-weight: bold;">public</span> function propertyChange<span style="color: #66cc66;">&#40;</span>event : <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3APropertyChangeEvent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">PropertyChangeEvent</span></a><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    	<span style="color: #808080; font-style: italic;">// Make sure that we're updating in the right thread</span>
        FX.<span style="color: #006600;">deferAction</span><span style="color: #66cc66;">&#40;</span>function<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            var bean : Bean = pojo as Bean;
            bean.<span style="color: #006600;">removePropertyChangeListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">getPropertyName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;age&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                age = event.<span style="color: #006600;">getNewValue</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AInteger+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Integer</span></a>;
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">getPropertyName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;name&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                name = event.<span style="color: #006600;">getNewValue</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a>;
            <span style="color: #66cc66;">&#125;</span>
            bean.<span style="color: #006600;">addPropertyChangeListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>To enable the listener functionality in my POJO I need to define an aspect:</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">package</span> se.<span style="color: #006600;">jayway</span>.<span style="color: #006600;">javafxbinder</span>.<span style="color: #006600;">example</span>.<span style="color: #006600;">domain</span>;
&nbsp;
<span style="color: #a1a100;">import se.jayway.javafxbinder.infrastructure.Bean;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> aspect MyJavaBeans <span style="color: #66cc66;">&#123;</span>
    declare parents: <span style="color: #66cc66;">&#40;</span>se.<span style="color: #006600;">jayway</span>.<span style="color: #006600;">javafxbinder</span>.<span style="color: #006600;">example</span>.<span style="color: #006600;">domain</span>.<span style="color: #006600;">Person</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">implements</span> Bean;
<span style="color: #66cc66;">&#125;</span></pre>
<h3>Conclusion</h3>
<p>Using AOP and a simple code generator is a good solution to the annoying JavaFX bind to Java problem. We use this tool for one of our clients and it works really good. However, my hope goes to someone at Sun/Oracle to integrate a similar functionality in the JavaFX compiler itself... Transparency looks good both in GUIs and in software design!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2009/05/12/transparent-bind-of-javafx-and-pojos/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>JavaFX &#8211; Hands On</title>
		<link>http://blog.jayway.com/2009/05/01/javafx-hands-on/</link>
		<comments>http://blog.jayway.com/2009/05/01/javafx-hands-on/#comments</comments>
		<pubDate>Fri, 01 May 2009 09:00:11 +0000</pubDate>
		<dc:creator>Per Böckman</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[javafx]]></category>
		<category><![CDATA[jayview]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=2813</guid>
		<description><![CDATA[The JavaFX 1.0 release was launched in December 2008 after years of development. So what is it? Sun markets JavaFX as a Rich Internet Application (RIA) platform and a tools suite aimed both for developers and UI designers. In this sense JavaFX can be compared with Adobe Flash and Microsoft Silverlight. With JavaFX a new [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The JavaFX 1.0 release was launched in December 2008 after<br />
years of development. So what is it? Sun markets JavaFX as a<br />
Rich Internet Application (RIA) platform and a tools suite aimed<br />
both for developers and UI designers. In this sense JavaFX can be<br />
compared with Adobe Flash and Microsoft Silverlight.   </p>
<p>With JavaFX a new programming language called JavaFX Script<br />
was introduced. In this article we’ll take a closer look at this new<br />
language. </strong></p>
<p>JavaFX will be able to run on PCs, mobile devices and TV screens. Things have<br />
been moving pretty fast with JavaFX 1.1 released just two months after 1.0<br />
and in early June 2009 a 1.2 version was released. The modifications have been<br />
substantial affecting both the API and the language syntax. It seems that Sun is<br />
using the opportunity to fix early mistakes in JavaFX before it is stabilized. This<br />
seems like a very good idea to avoid mistakes like those that will forever haunt<br />
standard Java.</p>
<h2>HelloWorld</h2>
<p>Let’s start with the traditional HelloWorld example. As you may have guessed<br />
JavaFX Script is very closely related to Java. In fact JavaFX scripts are compiled<br />
into standard Java .class files by the java fx compiler. A major advantage of this<br />
approach is that the JVM, which is available for many platforms, can be used.<br />
Now let’s see some code. Create a new file called helloworld.fx with following<br />
contents: </p>
<pre>println(”Hello JavaFX”);</pre>
<p>Compile the file with the javafxc compiler</p>
<pre>javafxc helloworld.fx</pre>
<p>The javafxc compiler have created a standard java class file:</p>
<pre>helloworld.class</pre>
<p>Now what happens if we try to run the program with java?</p>
<pre>java helloworld
Exception in thread ”main” java.lang.NoClassDefFoundError: com/sun/javafx/
runtime/FXObject</pre>
<p>That’s not quite the intended result... To run a JavaFX script the javafx tool is used. It is a java launcher that configures the runtime environment so that the classes used by JavaFX are included:</p>
<pre>javafx helloworld</pre>
<p>This time the output shouldn’t surprise anyone:</p>
<pre>Hello JavaFX</pre>
<h2>A Closer Look </h2>
<p>In the simple hello world example we didn’t declare a class or a main function.<br />
A JavaFX script can contain any number of classes or loose statements. JavaFX<br />
script is a statically typed language just like the Java language. </p>
<h3>Variables</h3>
<p>A variable is declared with the var keyword and can optionally specify a type.<br />
Unlike Java all types (even primitives) are backed by classes so you can call<br />
methods on them.</p>
<pre>var age: Integer; // type explicitly stated
age = 7;
var name = ”Foo”; // name gets the type String
name = ”Bar”;     // ok
name = 7;         // This results in a compile error since name is a String</pre>
<h3>Classes </h3>
<p>Classes in JavaFX differs from Java class. Lets have a look at simple example.</p>
<pre>class Person{
  var name : String;
  var age : Integer;
} </pre>
<p>You may have noticed that the the Person class didn’t have a constructor. Objects<br />
in JavaFX are created with the initialization arguments named. Also note that<br />
the keyword new is missing: </p>
<pre>var person = Person{name:”Joe”, age:21}</pre>
<h3>Sequences</h3>
<p>Sequences is a kind of Java arrays on steroids. They are used in many places in<br />
the language and can contain zero or more elements of a type.</p>
<pre>var nums = [1..5];
insert 6 into nums;
println(nums);     // prints [ 1, 2, 3, 4, 5, 6 ]</pre>
<h3>Duration</h3>
<p>The Duration type is very handy for calculations regarding time:</p>
<pre>var twoSeconds = 2s;
var oneHourAndABit = 1h + twoSeconds;</pre>
<h3>Function Types</h3>
<p>Functions are first class objects in JavaFX. In the example below the<br />
timesNWithBonus function returns another function that has access to the<br />
argument n and the variable bonus. The variable timesTenWithBonus holds a<br />
reference to the returned function and the reference is later used to invoke the<br />
function and print the result.</p>
<pre>function timesNWithBonus(n: Integer): function(:Integer): Integer {
  var bonus=2;
  function(x: Integer): Integer { x * n + bonus}
}
var timesTenWithBonus = timesNWithBonus(10);
println(timesTenWithBonus(4));    // prints 42 (10 * 4 + 2)</pre>
<h3>Binding</h3>
<p>Binding makes it possible connect or bind a variable value to some some<br />
expression.</p>
<pre>function square(x:Integer):Integer{ return x*x };
var y = 3;
var sqY = bind square(y);
println(sqY); // prints 9
y=4;
println(sqY); // prints 16</pre>
<h3>Calling Home </h3>
<p>It’s easy to use Java objects from JavaFX. In this example we use the java.util.<br />
Random class but the syntax would be the same if you were to use your own<br />
existing Java libraries.</p>
<pre>var rand = new java.util.Random();
var randNbr = rand.nextInt(6);
println(”Random number: {randNbr}”); </pre>
<p>As shown in the last line you can use braces in a string to evaluate an expression.</p>
<h2>Graphics in JavaFX</h2>
<p>One of the main goals with JavaFX is to make it easy to create an application’s<br />
UI. To accomplish this goal a declarative approach is used. This has the benefit<br />
that the structure of JavaFX script code can be kept similar to what is actually<br />
drawn on the screen. To give an idea of how this works we will walk through the<br />
creation of a simple application showing a circle and a text.</p>
<h3>Preparing the Stage</h3>
<p>The application window is referred to as a stage. To open a window you simply<br />
state that there should be one and it will be opened for you when the application<br />
starts. The following code opens a window with ...well really nothing inside:</p>
<pre>Stage {
    title: ”Jayview FX”
    width: 200
    height: 250
    scene: Scene {
        fill: Color.BLACK
        content: []
    }
} </pre>
<p>As you can see in the code a scene is declared inside the stage. The scene is the<br />
area that you can draw on inside the application window. JavaFX will draw all<br />
graphical objects placed inside a scene; however the example’s content is empty<br />
so the result is a black background.</p>
<h3>Making a Scene</h3>
<p>To make the application more interesting we add a circle and some text to the<br />
scene. This is done by simply adding them to the scene’s content variable:</p>
<pre>scene: Scene {
        fill: Color.BLACK
        content: [
            Circle {
                centerX: 100
                centerY: 100
                radius: 50
                fill: Color.RED
            },
            Text{
                content: ”Jayview!”
                x: 75
                y: 100
                fill: Color.WHITE
            }
        ]
    } </pre>
<p>The <em>Text</em> and <em>Circle</em> classes inherit from the <em>Node</em> class which contains basic graphical stuff like positioning, scaling and rotating. Graphical objects are<br />
organized in a tree structure of <em>Node</em> objects called a scene graph in which the<br />
scene is the root. Now that we have both a circle and a text, what more could<br />
you possibly want?</p>
<h3>Effects</h3>
<p>If you have used image effects in Java you know that quite a lot of code is<br />
required to achieve effects like reflections. In JavaFX this has been addressed by<br />
image filters providing effects like blur, shadow lighting and glow. In our example<br />
we want the circle and text to have a reflection. To treat multiple nodes as one<br />
when applying an effect a Group is needed. A Group has a content attribute<br />
in which you place the nodes that should be treated together, so the circle and<br />
text is moved inside it. Following the content we declare that a reflection effect<br />
should be applied on it:</p>
<pre>Group{
    content: [
        // Circle and text code goes here...
    ]
    effect: Reflection {
        fraction: 0.9   // 90% of the image visible
        topOpacity: 0.5 // 50% is max opacity
        topOffset: 2.5  // Offset between node end and reflection
    }
}
</pre>
<h3>Animations Step One - Binding Variables</h3>
<p>Combining the declarative UI and data binding is very useful for creating<br />
animations. In this example we want to modify the circle’s color and radius so<br />
therefore declare two variables: </p>
<pre>var fancyColor = Color.RED;
var circleRadius = 50;</pre>
<p>To bind the circle’s color and radius to the new variables it is modified to use<br />
the bind expression: </p>
<pre>Circle {
    centerX: 100
    centerY: 100
    radius: bind circleRadius
    fill}</pre>
<p>Let’s also modify the text to show the circle’s radius and align its y-position with<br />
the circle:</p>
<pre>Text{
    content: bind ”Radius{circleRadius}”
    x: 75
    y: bind 100 + circleRadius
  } </pre>
<p>When the variables circleRadius or fancyColor change JavaFX will automatically<br />
redraw the circle using the new values. </p>
<h3>Animations Step Two - Let’s Get Moving</h3>
<p>Animations are controlled by a Timeline which can be started and stopped. The<br />
time line contains a sequence of key frames, but lets start with an empty time<br />
line: </p>
<pre>var timeline = Timeline {
    autoReverse: true // Play in reverse when reaching the end
    repeatCount: Timeline.INDEFINITE
    keyFrames: []
}
timeline.play();</pre>
<p>A key frame contains information about what the scene should look like at a<br />
specific point in time, typically the end and beginning of the animation. First we<br />
add the animation’s start to the keyFrames sequence:</p>
<pre>KeyFrame {
    time: 0s  // 0 seconds into the animation
    values: [
        fancyColor => Color.RED tween Interpolator.EASEBOTH,
        circleRadius => 50 tween Interpolator.EASEBOTH
    ]} </pre>
<p>The syntax used for the values sequence may look strange but is easy to read<br />
when you get used to it: </p>
<pre>variable => value at the time of the key frame tween method for calculating
variable values between frames</pre>
<p>To make an animation look smooth acceleration and deceleration is used to<br />
prevent changes from appearing jerky. JavaFX provides built in support for this<br />
using Interpolator.EASEBOTH. In a similar way we define what the color and<br />
radius values should be at 5 seconds:</p>
<pre>KeyFrame {
    time: 5s
    values: [
        fancyColor => Color.YELLOW tween Interpolator.EASEBOTH,
        circleRadius => 0 tween Interpolator.EASEBOTH
    ]
} </pre>
<p>The application is now complete with an animated circle and radius readout.<br />
The images below show the shrinking circle, but don’t worry it comes back<br />
again <img src='http://blog.jayway.com/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p><img src="http://blog.jayway.com/wordpress/wp-content/uploads/2009/05/Picture-53.png" alt="Radius" title="Radius" width="440" height="178" class="alignnone size-full wp-image-3005" /></p>
<h3>Example Code</h3>
<p>The complete example code (excluding import statements) is shown below to<br />
give a better overview: </p>
<pre>var fancyColor =  Color.RED;
var circleRadius = 50;
Stage {
    title: ”Jayview FX”
    width: 200
    height: 250
    scene: Scene {
        fill: Color.BLACK
        content: [
            Group{
                content: [
                    Circle {
                        centerX: 100
 centerY: 100
                        radius: bind circleRadius
                        fill: bind fancyColor
                    }
                    Text{
                        content: bind ”Radius {circleRadius}”
                        x: 50
                        y: bind 100 + circleRadius
                        fill: Color.WHITE
                    }
                ]
                effect: Reflection {
                    fraction: 0.9
                    topOpacity: 0.5
                    topOffset: 2.5
                }
            }
        ]
    }
}
var timeline = Timeline {
    autoReverse: true
    repeatCount: Timeline.INDEFINITE
    keyFrames: [
        KeyFrame {
            time: 0s
            values: [
                fancyColor => Color.RED tween Interpolator.EASEBOTH,
                circleRadius => 50 tween Interpolator.EASEBOTH
            ]
        },
        KeyFrame {
            time: 5s
            values: [
                fancyColor => Color.YELLOW tween Interpolator.EASEBOTH,
                circleRadius => 0 tween Interpolator.EASEBOTH
            ]
        }
    ]
};
timeline.play(); </pre>
<h2>Conclusion </h2>
<p>First of all JavaFX is fun to play with! We really think you should download it<br />
and try it out to see for yourself. The new syntax does take some time to get used<br />
to but the Netbeans IDE gives good support to get started. </p>
<p>You can tell that JavaFX is still an early version, the documentation isn’t complete<br />
and the tools for designers do not yet match competing tools like Adobe Flash.<br />
Sun is currently addressing these shortcomings and when JavaFX reaches a more<br />
mature level it may well become the default choice for creating rich Internet<br />
applications that it strives to be. </p>
<p><em>Per Böckman<br />
Mattias Lindskog </em></p>
<h2>References </h2>
<ul>
<il>JavaFX Script Programming Language Reference: <a href="https://openjfx.dev.java.net/langref/">https://openjfx.dev.<br />
       java.net/langref/</a><br />
<il>JavFX home page: <a href="http://javafx.com/">http://javafx.com/</a>
</ul>
<p><em>Originally published in <a href="http://jayway.se/jayview">JayView</a>.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2009/05/01/javafx-hands-on/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

