<?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>Mike&#039;s Blog</title>
	<atom:link href="http://blog.netopyr.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.netopyr.com</link>
	<description>Articles related to Java</description>
	<lastBuildDate>Mon, 15 Apr 2013 20:19:07 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Tutorial &#8211; JavaFX Library for Inverse Kinematics 2.0</title>
		<link>http://blog.netopyr.com/2013/03/28/tutorial-javafx-library-for-inverse-kinematics-2-0/</link>
		<comments>http://blog.netopyr.com/2013/03/28/tutorial-javafx-library-for-inverse-kinematics-2-0/#comments</comments>
		<pubDate>Thu, 28 Mar 2013 22:24:45 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[JavaFX]]></category>

		<guid isPermaLink="false">http://blog.netopyr.com/?p=494</guid>
		<description><![CDATA[This is the first tutorial explaining the basics of javafx-ik, a library for inverse kinematics with JavaFX. The sources of the library can be downloaded from GitHub. What is a Bone object ? A bone is the fundamental building block for inverse kinematics with the library javafx-ik. A bone has a length and a joint [...]]]></description>
				<content:encoded><![CDATA[<p>This is the first tutorial explaining the basics of <em>javafx-ik</em>, a library for inverse kinematics with JavaFX. The sources of the library can be downloaded from <a title="javafx-ik sources" href="https://github.com/netopyr/javafx-ik">GitHub</a>.<br />
<span id="more-494"></span></p>
<h3>What is a <em>Bone</em> object ?</h3>
<div id="attachment_525" class="wp-caption alignright" style="width: 178px"><img src="http://blog.netopyr.com/wp-content/uploads/figure1-1.png" alt="Single Bone" width="178" height="135" class="size-full wp-image-525" /><p class="wp-caption-text">Figure 1: Single Bone</p></div>
<p>A bone is the fundamental building block for inverse kinematics with the library <em>javafx-ik</em>. A bone has a length and a joint around which it can be rotated. The end with the joint is called the head of the bone, the other end is called the tail. Figure 1 shows a single bone, its head, tail, and length.</p>
<p>In the library <em>javafx-ik</em>, a bone is resembled by the class <em>Bone</em>. To initialize it, the length has to be passed as a minimum, but usually also the angle is set during construction. The code sample above sets up a bone with a length of 50 and a rotation of 0, which means it will point horizontally to the right.</p>
<script src="https://gist.github.com/5258114.js"></script><noscript><p>View the code on <a href="https://gist.github.com/5258114">Gist</a>.</p></noscript>
<h3>Assembling bones</h3>
<div id="attachment_526" class="wp-caption alignright" style="width: 178px"><img src="http://blog.netopyr.com/wp-content/uploads/figure1-2.png" alt="Assembling two Bones" width="178" height="135" class="size-full wp-image-526" /><p class="wp-caption-text">Figure 2: Assembling two Bones</p></div>
<p>Bones can be assembled to define the skeleton of an animated object. Two bones are linked by attaching the tail of one bone <em>b1</em> to the head of another bone <em>b2</em>. Bone <em>b1</em> is called the parent of bone <em>b2</em>, bone <em>b2</em> is called a child of bone <em>b1</em>. Figure 2 shows two connected bones <em>b1</em> and <em>b2</em>.</p>
<p>The class <em>Bone</em> offers two properties for the structure. The read-only property <em>parent</em> stores the parent of a bone, the property <em>children</em> is an <em>ObservableList</em> of all child-bones. The following snippet shows how two bones <em>b1</em> and <em>b2</em> can be connected:</p>
<script src="https://gist.github.com/5258244.js"></script><noscript><p>View the code on <a href="https://gist.github.com/5258244">Gist</a>.</p></noscript>
<div id="attachment_503" class="wp-caption alignright" style="width: 260px"><img src="http://blog.netopyr.com/wp-content/uploads/figure1-3.png" alt="Structure of the Dummy" width="260" height="300" class="size-full wp-image-503" /><p class="wp-caption-text">Figure 3: Structure of the Dummy</p></div>
<p>The read-only property <em>angle</em> defines the rotation between a bone and the extension of its parent bone. A value of 0 results in a straight line, a value of 180 results in the child bone overlapping its parent, but pointing in the opposite direction.</p>
<p>Although having parents and children imposes an order on the bones in a skeleton, it usually makes no difference, which bone is the parent and which is the child.</p>
<p>By repeatedly linking heads and tails of <em>Bone</em> objects, it is possible to create a chain. This simple structure is used in the caterpillar sample. Complex objects are defined using a tree of bones.</p>
<p>The exact position of a bone depends on the position of the parent and the rotation.</p>
<p>The bone at the top of the tree, which does not have a parent, is called the root bone. The root bone is treated slightly different during initialization. The property <em>angle</em> of a root bone defines its rotation in the overall scene. You can think of it as having a parent bone which points horizontally to the right.</p>
<p>Now we are able to define the structure of our dummy, as shown in Figure 3. The code to generate the skeleton of the dummy can be seen in the code sample below.</p>
<script src="https://gist.github.com/5267083.js"></script><noscript><p>View the code on <a href="https://gist.github.com/5267083">Gist</a>.</p></noscript>
<h3>Attaching Visual Components</h3>
<p>Bones themselves are not visible. They are only defining the structure. The property <em>content</em>, which is an <em>ObservableList</em> of <em>Node</em> objects, can be used to attach visible elements to a bone.</p>
<script src="https://gist.github.com/5258529.js"></script><noscript><p>View the code on <a href="https://gist.github.com/5258529">Gist</a>.</p></noscript>
<div id="attachment_522" class="wp-caption alignleft" style="width: 178px"><img src="http://blog.netopyr.com/wp-content/uploads/figure1-4.png" alt="Adding visual elements" width="178" height="135" class="size-full wp-image-522" /><p class="wp-caption-text">Figure 4: Adding visual elements</p></div>
<p>The position and rotation of the attached <em>Node</em> objects is determined by the underlying bone. The origin of the local coordinate system for all attached nodes is the head position of the bone. If the head position is moved, so are all its nodes.</p>
<p>The rotation of a bone is also passed to the nodes. If the bone is not rotated at all and <em>angle</em> has the value 0, it is pointing horizontally to the right. In the code sample above a bone is defined with two circles and an ellipse attached. This bone is shown in Figure 4.</p>
<p>The code sample below shows the required changes that define the appearance of our dummy by attaching circles and ellipses. The resulting dummy can be seen in Figure 5. In the picture, I added symbols for the bones to make them visible.</p>
<p>Note that none of the visual components are rotated themselves and all positions are local to the bone. The final positions and rotations in the scene are calculated from the positions and rotations of the bones only.</p>
<script src="https://gist.github.com/5258634.js"></script><noscript><p>View the code on <a href="https://gist.github.com/5258634">Gist</a>.</p></noscript>
<h3>Adding everything to the scenegraph</h3>
<div id="attachment_504" class="wp-caption alignright" style="width: 260px"><img src="http://blog.netopyr.com/wp-content/uploads/figure1-5.png" alt="Dummy with all visual elements attached" width="260" height="300" class="size-full wp-image-504" /><p class="wp-caption-text">Figure 5: Dummy with all visual elements attached</p></div>
<p>Now there is just one final piece missing to be able to render the dummy on the screen: the class <em>Skeleton</em>. The class <em>Skeleton</em> is the bridge between the Scenegraph and the bones with the attached <em>Node</em> objects. It extends <em>Parent</em> and can therefore be added anywhere in the Scenegraph. It can be translated, rotated, scaled and transformed like any other node in the Scenegraph.</p>
<p>A Skeleton object and the bones of an animated object are linked by setting the property <em>skeleton</em> of any bone. You will notice that the property <em>skeleton</em> of all bones in the same structure will be updated to point to the same <em>Skeleton</em> object afterwards. It is not possible that two bones in the same structure point to different <em>Skeleton</em> objects.</p>
<p>To see the result, download the full <a href="https://gist.github.com/netopyr/5267248" title="Dummy.java">Dummy.java</a> file. To compile the script, you need to add the library <em>javafx-ik</em> to your classpath. You can download the sources from <a title="javafx-ik sources" href="https://github.com/netopyr/javafx-ik">GitHub</a>.</p>
<h4>What&#8217;s next?</h4>
<p>The first part of this tutorial showed how to define the static structure &#8211; or skeleton &#8211; of an animated object and how to attach visible components. The second part will explain how this structure can be animated to create naturally looking animations.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.netopyr.com%2F2013%2F03%2F28%2Ftutorial-javafx-library-for-inverse-kinematics-2-0%2F&amp;title=Tutorial%20%E2%80%93%20JavaFX%20Library%20for%20Inverse%20Kinematics%202.0" id="wpa2a_2"><img src="http://blog.netopyr.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.netopyr.com/2013/03/28/tutorial-javafx-library-for-inverse-kinematics-2-0/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JavaFX Library for Inverse Kinematics 2.0</title>
		<link>http://blog.netopyr.com/2013/03/06/javafx-library-for-inverse-kinematics-2-0/</link>
		<comments>http://blog.netopyr.com/2013/03/06/javafx-library-for-inverse-kinematics-2-0/#comments</comments>
		<pubDate>Wed, 06 Mar 2013 20:18:38 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[JavaFX]]></category>

		<guid isPermaLink="false">http://blog.netopyr.com/?p=457</guid>
		<description><![CDATA[Last week I had to stay sick at home for a couple of days. On the positive side, I finally found some time to look into my old library for inverse kinematics. I wrote it a couple of years ago for JavaFX 1.x. And I decided to port it to JavaFX 2. The initial version [...]]]></description>
				<content:encoded><![CDATA[<p>Last week I had to stay sick at home for a couple of days. On the positive side, I finally found some time to look into my old library for inverse kinematics. I wrote it a couple of years ago for JavaFX 1.x. And I decided to port it to JavaFX 2. The initial version is ready for download.</p>
<p><span id="more-457"></span></p>
<div id="attachment_460" class="wp-caption alignleft" style="width: 320px"><a href="http://blog.netopyr.com/wp-content/uploads/Caterpillar.png"><img class="size-full wp-image-460" alt="Screenshot Caterpillar" src="http://blog.netopyr.com/wp-content/uploads/Caterpillar.png" width="320" height="232" /></a><p class="wp-caption-text">Fig. 1: Screenshot Caterpillar</p></div>
<p>The original library was written in JavaFX Script and looking at the code created some cozy nostalgic feelings. <img src='http://blog.netopyr.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  What a beautiful language! Doing the port was pretty interesting, because even though Java and JavaFX Script had many similarities, some of the concepts were extremely different. Definitely it was a fun exercise.</p>
<p>An initial version is ready and can be used. I plan to add more features later, which means the API may change at some point. There is no documentation ready right now except for some basic comments in the source code. But I added two examples, which are probably a good starting point. JavaFX veterans might remember these samples, because they are direct ports from the samples of the first version of the library.</p>
<div id="attachment_461" class="wp-caption alignright" style="width: 320px"><a href="http://blog.netopyr.com/wp-content/uploads/Dummy.png"><img class="size-full wp-image-461" alt="Screenshot Dummy" src="http://blog.netopyr.com/wp-content/uploads/Dummy.png" width="320" height="232" /></a><p class="wp-caption-text">Fig. 2: Screenshot Dummy</p></div>
<p>First there is the Caterpillar Demo. You can click anywhere in the window and the caterpillar will move there. Only the head is actually animated directly, the rest of the body is moved automatically using inverse kinematics.</p>
<p>The second example is a little dummy figure. You can drag the parts of its body and the rest will follow.</p>
<p>I also found the text of a tutorial I had written. Rewriting it to match the new Java API should not be a problem, but unfortunately I could not find the images that belong to the tutorial yet. I keep searching! <img src='http://blog.netopyr.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>And finally, the most important part: the link. You can find the sources of the library including the code of the two samples on GitHub. <a title="GitHub Repository javafx-ik" href="https://github.com/netopyr/javafx-ik">Sources of javafx-ik.</a></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.netopyr.com%2F2013%2F03%2F06%2Fjavafx-library-for-inverse-kinematics-2-0%2F&amp;title=JavaFX%20Library%20for%20Inverse%20Kinematics%202.0" id="wpa2a_4"><img src="http://blog.netopyr.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.netopyr.com/2013/03/06/javafx-library-for-inverse-kinematics-2-0/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>JavaOne Material</title>
		<link>http://blog.netopyr.com/2012/10/10/javaone-material/</link>
		<comments>http://blog.netopyr.com/2012/10/10/javaone-material/#comments</comments>
		<pubDate>Wed, 10 Oct 2012 08:42:51 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[JavaFX]]></category>

		<guid isPermaLink="false">http://blog.netopyr.com/?p=413</guid>
		<description><![CDATA[At this year&#8217;s JavaOne, I did a session &#8220;JavaFX for Business Application Developers&#8221; and a BOF &#8220;Live-Coding BOF: Writing a Game with JavaFX&#8221;. This post provides the material. JavaFX for Business Application Developers First the slides of the session &#8220;JavaFX for Business Application Developers&#8221;. Edit: I am not sure, if the session was recorded. If [...]]]></description>
				<content:encoded><![CDATA[<p>At this year&#8217;s JavaOne, I did a session &#8220;JavaFX for Business Application Developers&#8221; and a BOF &#8220;Live-Coding BOF: Writing a Game with JavaFX&#8221;. This post provides the material.<br />
<span id="more-413"></span></p>
<h2>JavaFX for Business Application Developers</h2>
<p>First the slides of the session &#8220;JavaFX for Business Application Developers&#8221;.</p>
<h5>Edit:</h5>
<p><del datetime="2012-10-10T10:50:44+00:00">I am not sure, if the session was recorded. If additional material becomes available, I will update this post.</del><br />
There is <a href="https://oracleus.activeevents.com/connect/sessionDetail.ww?SESSION_ID=5434">more material including a video</a> available.</p>
<p><iframe src="http://www.slideshare.net/slideshow/embed_code/14635914" width="590" height="481" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe><br/><br/><br />
<br/></p>
<h2>Live-Coding BOF: Writing a Game with JavaFX</h2>
<p>Below is the link of the NetBeans project I created during the BOF. It is almost the original code, I just reordered some of the statements and made the constants private. For those of you who did not attend the BOF:  I am considering to make a tutorial out of this and post it here on the blog. Please tell me, if you would be interested in that.</p>
<p><a href="http://blog.netopyr.com/wp-content/uploads/FxGame.zip">FxGame Sources</a></p>
<h5>Edit:</h5>
<p>There is <a href="https://oracleus.activeevents.com/connect/sessionDetail.ww?SESSION_ID=5440">more material including a video</a> available.</p>
<p><a href="http://creativecommons.org/licenses/by/3.0/deed.en_US" rel="license"><img style="border-width: 0;" src="http://i.creativecommons.org/l/by/3.0/88x31.png" alt="Creative Commons License" /></a><br />
<span>FXGame</span> by <a href="http://blog.netopyr.com" rel="cc:attributionURL">Michael Heinrichs</a> is licensed under a <a href="http://creativecommons.org/licenses/by/3.0/deed.en_US" rel="license">Creative Commons Attribution 3.0 Unported License</a>.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.netopyr.com%2F2012%2F10%2F10%2Fjavaone-material%2F&amp;title=JavaOne%20Material" id="wpa2a_6"><img src="http://blog.netopyr.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.netopyr.com/2012/10/10/javaone-material/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using the JavaFX AnimationTimer</title>
		<link>http://blog.netopyr.com/2012/06/14/using-the-javafx-animationtimer/</link>
		<comments>http://blog.netopyr.com/2012/06/14/using-the-javafx-animationtimer/#comments</comments>
		<pubDate>Thu, 14 Jun 2012 20:25:35 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[JavaFX]]></category>

		<guid isPermaLink="false">http://blog.netopyr.com/?p=391</guid>
		<description><![CDATA[In retrospect it was probably not a good idea to give the AnimationTimer its name, because it can be used for much more than just animation: measuring the fps-rate, collision detection, calculating the steps of a simulation, the main loop of a game etc. In fact, most of the time I saw AnimationTimer in action [...]]]></description>
				<content:encoded><![CDATA[<p>In retrospect it was probably not a good idea to give the AnimationTimer its name, because it can be used for much more than just animation: measuring the fps-rate, collision detection, calculating the steps of a simulation, the main loop of a game etc. In fact, most of the time I saw AnimationTimer in action was not related to animation at all. Nevertheless there are cases when you want to consider using an AnimationTimer for your animation. This post will explain the class and show an example where AnimationTimer is used to calculate animations.</p>
<p><img class="alignnone size-full wp-image-398" title="Animation calculated with AnimationTimer" src="http://blog.netopyr.com/wp-content/uploads/screenshot.png" alt="Screenshot of the AnimationTimer example used in this post" width="590" height="439" /><br />
<span id="more-391"></span><br />
The AnimationTimer provides an extremely simple, but very useful and flexible feature. It allows to specify a method, that will be called in every frame. What this method is used for is not limited and, as already mentioned, does not have anything to do with animation. The only requirement is, that it has to return fast, because otherwise it can easily become the bottleneck of a system.</p>
<p>To use it, a developer has to extend AnimationTimer and implement the abstract method handle(). This is the method that will be called in every frame while the AnimationTimer is active. A single parameter is passed to handle(). It contains the current time in nanoseconds, the same as what you would get when calling System.nanoTime().</p>
<p>Why should one use the passed in value instead of calling System.nanoTime() or its little brother System.currentTimeMillis() oneself? There are several reasons, but the most important probably is, that it makes your life a lot easier while debugging. If you ever tried to debug code, that depended on these two methods, you know that you are basically screwed. But the JavaFX runtime goes into a paused state while it is waiting to execute the next step during debugging and the internal clock does not proceed during this pause. In other words no matter if you wait two seconds or two hours before you resume a halted program while debugging, the increment of the parameter will roughly be the same!</p>
<p>AnimationTimer has two methods start() and stop() to activate and deactivate it. If you override them, it is important that you call these methods in the super class.</p>
<p>The Animation API comes with many feature rich classes, that make defining an animation very simple. There are predefined Transition classes, it is possible to define a key-frame based animation using Timeline, and one can even write a custom Transition easily. But in which cases does it make sense to use an AnimationTimer instead? &#8211; Almost always you want to use one of the standard classes. But if you want to specify many simple animations, using an AnimationTimer can be the better choice.</p>
<p>The feature richness of the standard animation classes comes with a price. Every single animation requires a whole bunch of variables to be tracked &#8211; variables that you often do not need for simple animations. Plus these classes are optimized for speed, not for small memory footprint. Some of the variables are stored twice, once in the format the public API requires and once in a format that helps faster calculation while playing.</p>
<p>Below is a simple example that shows a star field. It animates thousands of rectangles flying from the center to the outer edges. Using an AnimationTimer allows to store only the values that are needed. The calculation is extremely simple compared to the calculation within a Timeline for example, because no advanced features (loops, animation rate, direction etc.) have to be considered.</p>
<pre class="code" style="padding-left: 30px;">
<span class="keyword-directive">package</span> fxsandbox;

<span class="keyword-directive">import</span> java.util.Random;
<span class="keyword-directive">import</span> javafx.animation.AnimationTimer;
<span class="keyword-directive">import</span> javafx.application.Application;
<span class="keyword-directive">import</span> javafx.scene.Group;
<span class="keyword-directive">import</span> javafx.scene.Node;
<span class="keyword-directive">import</span> javafx.scene.Scene;
<span class="keyword-directive">import</span> javafx.scene.paint.Color;
<span class="keyword-directive">import</span> javafx.scene.shape.Rectangle;
<span class="keyword-directive">import</span> javafx.stage.Stage;

<span class="keyword-directive">public</span> <span class="keyword-directive">class</span> <span class="ST_BOLD">FXSandbox</span> <span class="keyword-directive">extends</span> Application {
    
    <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">final</span> <span class="keyword-directive">int</span> <span class="ST_GREEN_ITALIC">STAR_COUNT</span> = 20000;
    
    <span class="keyword-directive">private</span> <span class="keyword-directive">final</span> Rectangle[] <span class="ST_GREEN">nodes</span> = <span class="keyword-directive">new</span> Rectangle[<span class="ST_GREEN_ITALIC">STAR_COUNT</span>];
    <span class="keyword-directive">private</span> <span class="keyword-directive">final</span> <span class="keyword-directive">double</span>[] <span class="ST_GREEN">angles</span> = <span class="keyword-directive">new</span> <span class="keyword-directive">double</span>[<span class="ST_GREEN_ITALIC">STAR_COUNT</span>];
    <span class="keyword-directive">private</span> <span class="keyword-directive">final</span> <span class="keyword-directive">long</span>[] <span class="ST_GREEN">start</span> = <span class="keyword-directive">new</span> <span class="keyword-directive">long</span>[<span class="ST_GREEN_ITALIC">STAR_COUNT</span>];
    
    <span class="keyword-directive">private</span> <span class="keyword-directive">final</span> Random <span class="ST_GREEN">random</span> = <span class="keyword-directive">new</span> Random();

    @Override
    <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> <span class="ST_BOLD">start</span>(<span class="keyword-directive">final</span> Stage primaryStage) {
        <span class="keyword-directive">for</span> (<span class="keyword-directive">int</span> i=0; i&lt;<span class="ST_GREEN_ITALIC">STAR_COUNT</span>; i++) {
            <span class="ST_GREEN">nodes</span>[i] = <span class="keyword-directive">new</span> Rectangle(1, 1, Color.<span class="ST_GREEN_ITALIC">WHITE</span>);
            <span class="ST_GREEN">angles</span>[i] = 2.0 * Math.<span class="ST_GREEN_ITALIC">PI</span> * <span class="ST_GREEN">random</span>.nextDouble();
            <span class="ST_GREEN">start</span>[i] = <span class="ST_GREEN">random</span>.nextInt(2000000000);
        }
        <span class="keyword-directive">final</span> Scene scene = <span class="keyword-directive">new</span> Scene(<span class="keyword-directive">new</span> Group(<span class="ST_GREEN">nodes</span>), 800, 600, Color.<span class="ST_GREEN_ITALIC">BLACK</span>);
        primaryStage.setScene(scene);
        primaryStage.show();
        
        <span class="keyword-directive">new</span> AnimationTimer() {
            @Override
            <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> <span class="ST_BOLD">handle</span>(<span class="keyword-directive">long</span> now) {
                <span class="keyword-directive">final</span> <span class="keyword-directive">double</span> width = 0.5 * primaryStage.getWidth();
                <span class="keyword-directive">final</span> <span class="keyword-directive">double</span> height = 0.5 * primaryStage.getHeight();
                <span class="keyword-directive">final</span> <span class="keyword-directive">double</span> radius = Math.<span class="ST_ITALIC">sqrt</span>(2) * Math.<span class="ST_ITALIC">max</span>(width, height);
                <span class="keyword-directive">for</span> (<span class="keyword-directive">int</span> i=0; i&lt;<span class="ST_GREEN_ITALIC">STAR_COUNT</span>; i++) {
                    <span class="keyword-directive">final</span> Node node = <span class="ST_GREEN">nodes</span>[i];
                    <span class="keyword-directive">final</span> <span class="keyword-directive">double</span> angle = <span class="ST_GREEN">angles</span>[i];
                    <span class="keyword-directive">final</span> <span class="keyword-directive">long</span> t = (now - <span class="ST_GREEN">start</span>[i]) % 2000000000;
                    <span class="keyword-directive">final</span> <span class="keyword-directive">double</span> d = t * radius / 2000000000.0;
                    node.setTranslateX(Math.<span class="ST_ITALIC">cos</span>(angle) * d + width);
                    node.setTranslateY(Math.<span class="ST_ITALIC">sin</span>(angle) * d + height);
                }
            }
        }.start();
    }
    
    <span class="keyword-directive">public</span> <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> <span class="ST_BOLD_ITALIC">main</span>(String[] args) {
        <span class="ST_ITALIC">launch</span>(args);
    }
    
}
</pre>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.netopyr.com%2F2012%2F06%2F14%2Fusing-the-javafx-animationtimer%2F&amp;title=Using%20the%20JavaFX%20AnimationTimer" id="wpa2a_8"><img src="http://blog.netopyr.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.netopyr.com/2012/06/14/using-the-javafx-animationtimer/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Slides &#8220;JavaFX for Business Application Developers&#8221; published</title>
		<link>http://blog.netopyr.com/2012/06/08/slides-javafx-for-business-application-developers-published/</link>
		<comments>http://blog.netopyr.com/2012/06/08/slides-javafx-for-business-application-developers-published/#comments</comments>
		<pubDate>Fri, 08 Jun 2012 08:46:59 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.netopyr.com/?p=372</guid>
		<description><![CDATA[Just uploaded the slides of my talk &#8220;JavaFX for Business Application Developers&#8221; at Java Developer Day in Prague. JavaFX for Business Application Developers View more presentations from michael_heinrichs Too bad I still have not figured out, how I can add a script to the slides. I know I can add notes, but they are very [...]]]></description>
				<content:encoded><![CDATA[<p>Just uploaded the slides of my talk &#8220;JavaFX for Business Application Developers&#8221; at Java Developer Day in Prague.</p>
<p><span id="more-372"></span></p>
<div style="width:510px" id="__ss_13246315"> <strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/michael_heinrichs/javafx-for-business-application-developers-13246315" title="JavaFX for Business Application Developers" target="_blank">JavaFX for Business Application Developers</a></strong> <iframe src="http://www.slideshare.net/slideshow/embed_code/13246315?rel=0" width="510" height="426" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC;border-width:1px 1px 0" allowfullscreen></iframe>
<div style="padding:5px 0 12px"> View more <a href="http://www.slideshare.net/" target="_blank">presentations</a> from <a href="http://www.slideshare.net/michael_heinrichs" target="_blank">michael_heinrichs</a> </div>
</p></div>
<p>Too bad I still have not figured out, how I can add a script to the slides. I know I can add notes, but they are very well hidden in my opinion. There must be a better way.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.netopyr.com%2F2012%2F06%2F08%2Fslides-javafx-for-business-application-developers-published%2F&amp;title=Slides%20%E2%80%9CJavaFX%20for%20Business%20Application%20Developers%E2%80%9D%20published" id="wpa2a_10"><img src="http://blog.netopyr.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.netopyr.com/2012/06/08/slides-javafx-for-business-application-developers-published/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New website SketchHatch is going online &#8211; your feedback is wanted</title>
		<link>http://blog.netopyr.com/2012/05/15/new-website-sketchhatch-is-going-online-your-feedback-is-wanted/</link>
		<comments>http://blog.netopyr.com/2012/05/15/new-website-sketchhatch-is-going-online-your-feedback-is-wanted/#comments</comments>
		<pubDate>Tue, 15 May 2012 16:05:51 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[SketchHatch]]></category>

		<guid isPermaLink="false">http://blog.netopyr.com/?p=362</guid>
		<description><![CDATA[SketchHatch is a web application that helps you to sketch out new ideas in a fun and inspiring way. Usually all you want to put down during early phases are some rough drafts, maybe some notes and that&#8217;s it. I did not find a single tool that gave that to me, so I wrote my [...]]]></description>
				<content:encoded><![CDATA[<p><a title="Website SketchHatch" href="http://sketchhatch.com">SketchHatch</a> is a web application that helps you to sketch out new ideas in a fun and inspiring way. Usually all you want to put down during early phases are some rough drafts, maybe some notes and that&#8217;s it. I did not find a single tool that gave that to me, so I wrote my own. <img src='http://blog.netopyr.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p><span id="more-362"></span></p>
<p><a href="http://SketchHatch.com"><img class="alignnone size-full wp-image-363" style="border-image: initial; border-width: 1px; border-color: black; border-style: solid;" title="Screenshot SketchHatch" src="http://blog.netopyr.com/wp-content/uploads/tour2a_blog.png" alt="" width="590" height="364" /></a></p>
<p>Pretty much every time when I have a new idea and want to create some initial drafts, I do not really know which tool to use. My favorite currently is using a real whiteboard. But unfortunately you cannot take it with you and you cannot keep the content for very long. My phone is full of pictures from whiteboard drawings, I guess that sounds familiar to most of you…</p>
<p>Paper and pen is certainly another possible solution, but the drawbacks are very similar. It is hard to find your notes later. Plus it is cumbersome to copy everything into the computer when you want to continue with your idea.</p>
<p>Using a web application looks like a natural choice, because it allows you to keep everything forever while making it easy to find old notes. You can continue to work from anywhere. And your drafts are already in the computer, you do not have to copy them.</p>
<p>But unfortunately the available tools do not really work for me. They are usually great for creating presentable content, giving you all kinds of options to make the result look good. But that is only the second step, first you want to sketch out your ideas roughly and quickly &#8211; worrying about pixel perfect alignment etc. is hindering rather than helping.</p>
<p>And here SketchHatch comes in. I tried to make the user interface as close to a real whiteboard as possible. For instance you will notice, that the sticky notes are tilted, just like real ones. Or all lines and rectangles are not straight, but a little wiggly just like hand drawn lines.</p>
<p>I encourage you to give it a try. Pretty much all the people that have tried the application so far agreed that the user interface is fun and really different from anything they have seen so far. So please try <a title="SketchHatch Website" href="http://sketchhatch.com">SketchHatch</a> (and do not forget to send me your feedback, even if you do not like the application)! <img src='http://blog.netopyr.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>&nbsp;</p>
<p>Thanks in advance for all replies,</p>
<p>Michael</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.netopyr.com%2F2012%2F05%2F15%2Fnew-website-sketchhatch-is-going-online-your-feedback-is-wanted%2F&amp;title=New%20website%20SketchHatch%20is%20going%20online%20%E2%80%93%20your%20feedback%20is%20wanted" id="wpa2a_12"><img src="http://blog.netopyr.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.netopyr.com/2012/05/15/new-website-sketchhatch-is-going-online-your-feedback-is-wanted/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Teaser of my upcoming website</title>
		<link>http://blog.netopyr.com/2012/04/19/teaser-of-my-upcoming-website/</link>
		<comments>http://blog.netopyr.com/2012/04/19/teaser-of-my-upcoming-website/#comments</comments>
		<pubDate>Thu, 19 Apr 2012 21:01:56 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.netopyr.com/?p=355</guid>
		<description><![CDATA[It has been very quite on my blog for some time now. The reason is, that I was busy writing a new web application at day and night &#8211; well mostly at night. It is almost done and I will hopefully be able to open it for public beta next week. The application provides an [...]]]></description>
				<content:encoded><![CDATA[<p>It has been very quite on my blog for some time now. The reason is, that I was busy writing a new web application at day and night &#8211; well mostly at night. It is almost done and I will hopefully be able to open it for public beta next week.</p>
<p>The application provides an online whiteboard. You can attach sticky notes and draw on the canvas directly. It is pretty flexible. My original idea was a tool that helps me while brainstorming new ideas. But looking at the final result now, I guess you can use it for all sorts of things you would usually use a whiteboard for. Maybe even as a Scrum board or a Kanban board. <img src='http://blog.netopyr.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Below is a screenshot. If this looks interesting, stay tuned for the announcement sometime next week.</p>
<p><a href="http://blog.netopyr.com/wp-content/uploads/tour1_small.png"><img class="alignnone size-full wp-image-356" style="border-image: initial; border-width: 1px; border-color: black; border-style: solid;" title="Screenshot" src="http://blog.netopyr.com/wp-content/uploads/tour1_small.png" alt="Screenshot of my new web application" width="420" height="240" /></a></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.netopyr.com%2F2012%2F04%2F19%2Fteaser-of-my-upcoming-website%2F&amp;title=Teaser%20of%20my%20upcoming%20website" id="wpa2a_14"><img src="http://blog.netopyr.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.netopyr.com/2012/04/19/teaser-of-my-upcoming-website/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Example CSS Stylying in JavaFX</title>
		<link>http://blog.netopyr.com/2012/03/17/example-css-stylying-in-javafx/</link>
		<comments>http://blog.netopyr.com/2012/03/17/example-css-stylying-in-javafx/#comments</comments>
		<pubDate>Sat, 17 Mar 2012 17:56:38 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[JavaFX]]></category>

		<guid isPermaLink="false">http://blog.netopyr.com/?p=327</guid>
		<description><![CDATA[To learn how to use CSS with JavaFX, I wrote a small application and created two new looks with CSS &#8211; and I must say I am truly impressed by its capabilities. Look for yourself how much you can do with CSS only.  The last couple of weeks I spent some time looking at parts [...]]]></description>
				<content:encoded><![CDATA[<p>To learn how to use CSS with JavaFX, I wrote a small application and created two new looks with CSS &#8211; and I must say I am truly impressed by its capabilities. Look for yourself how much you can do with CSS only.<br />
<span id="more-327"></span> The last couple of weeks I spent some time looking at parts of the JavaFX runtime, I have not used before. For this I wrote a small application, which I want to use during one of my talks at <a title="33rd Degree Conference" href="http://2012.33degree.org/" target="_blank">33rd Degree in Krakow, Poland</a>, next week.</p>
<p>It took me a while to pick up CSS in JavaFX to be honest. One reason was, that it uses some features of CSS 3, that I was not familiar with. It was interesting to learn this stuff. Another reason was, that names for the same thing differ between JavaFX and &#8220;standard CSS&#8221; (e.g. color vs. -fx-text-fill). But I heard there are plans to fix that. Though the main reason why it took a while to use it efficiently was, that I did not know how to style a concrete control class. I ended up looking at the default stylesheet caspian.css to figure it out.</p>
<p>Overall I am very impressed and like CSS in JavaFX a lot. (Disclaimer: I was not involved in the design or implementation of the CSS functionality at any point. This is my honest opinion without any hidden motives.) <img src='http://blog.netopyr.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>I think it is worth mentioning, that all of the examples are fully functional applications, layouts continued to work, and the Java code was not changed at all &#8211; all is done in CSS, even the bracket shapes in the last example. No images were used.</p>
<p>&nbsp;</p>
<p><a href="http://blog.netopyr.com/wp-content/uploads/screenshot13.png"><img class="alignnone size-full wp-image-347" title="Screenshot with Default Style" src="http://blog.netopyr.com/wp-content/uploads/screenshot13.png" alt="Screenshot of the application with the default stylesheet caspian.css" width="579" height="463" /></a></p>
<p><a href="http://blog.netopyr.com/wp-content/uploads/screenshot22.png"><img class="alignnone size-full wp-image-348" title="Screenshot &quot;Facebook&quot; Style" src="http://blog.netopyr.com/wp-content/uploads/screenshot22.png" alt="Screenshot of the application with a style inspired by Facebook" width="579" height="463" /></a></p>
<p><a href="http://blog.netopyr.com/wp-content/uploads/screenshot32.png"><img class="alignnone size-full wp-image-349" title="Screenshot &quot;Enterprise&quot; Style" src="http://blog.netopyr.com/wp-content/uploads/screenshot32.png" alt="Screenshot of the application with a style inspired by  Star Trek" width="578" height="462" /></a></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.netopyr.com%2F2012%2F03%2F17%2Fexample-css-stylying-in-javafx%2F&amp;title=Example%20CSS%20Stylying%20in%20JavaFX" id="wpa2a_16"><img src="http://blog.netopyr.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.netopyr.com/2012/03/17/example-css-stylying-in-javafx/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Creating a Sprite Animation with JavaFX</title>
		<link>http://blog.netopyr.com/2012/03/09/creating-a-sprite-animation-with-javafx/</link>
		<comments>http://blog.netopyr.com/2012/03/09/creating-a-sprite-animation-with-javafx/#comments</comments>
		<pubDate>Thu, 08 Mar 2012 23:11:47 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[JavaFX]]></category>

		<guid isPermaLink="false">http://blog.netopyr.com/?p=311</guid>
		<description><![CDATA[While most of my posts so far dealt with JavaFX properties and bindings, today I want to write about another part of the JavaFX runtime I also work on: the animation API. In this article I will explain how to write custom animations in JavaFX and use this approach to create a class for sprite [...]]]></description>
				<content:encoded><![CDATA[<p>While most of my posts so far dealt with JavaFX properties and bindings, today I want to write about another part of the JavaFX runtime I also work on: the animation API. In this article I will explain how to write custom animations in JavaFX and use this approach to create a class for sprite animations. (This will also be a good practice for one of my sessions at the conference <a title="33rd Degree 2012" href="http://2012.33degree.org/">33rd Degree</a>. I plan to write a game in JavaFX in just one hour. That&#8217;s going to be fun!) <img src='http://blog.netopyr.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<div class="wp-caption aligncenter" style="width: 330px"><a href="http://en.wikipedia.org/wiki/File:The_Horse_in_Motion.jpg"><img title=" The Horse in Motion" src="http://upload.wikimedia.org/wikipedia/commons/thumb/7/73/The_Horse_in_Motion.jpg/320px-The_Horse_in_Motion.jpg" alt=" The Horse in Motion" width="320" height="198" /></a><p class="wp-caption-text">The Horse in Motion</p></div>
<p><span id="more-311"></span></p>
<p>There are many very good articles out there describing predefined Transitions (TranslateTransition, RotateTransition etc.) and Timelines. Most of the time these approaches are sufficient, but in some cases one just needs more flexibility. And that is when the Transition class comes into play, which can be extended to define a custom animation.</p>
<p>To write your own animation class by extending Transition, two steps are required:</p>
<ol>
<li>Specify the duration of a single cycle</li>
<li>Implement the interpolate() method</li>
</ol>
<h3>Duration of a single cycle</h3>
<p>You can set the duration of a cycle by calling the protected method setCycleDuration(). Most of the time the duration is either fixed (if the animation is used only once) or configurable by the user. Almost all predefined Transitions in the JavaFX runtime fall into the second category. They expose their cycle duration via the duration property, you probably want to do that in your class, too. In rare cases the duration of a cycle depends on other values. For example the duration of a SequentialTransition and ParallelTransition depend on the duration of their children.</p>
<p>You can change the cycle duration as often as you like, but note that it does not effect a currently running animation. Only after an animation is stopped and started again is the new cycle duration taken into account.</p>
<h3>The interpolate() method</h3>
<p>The interpolate() method is abstract and needs to be overridden. It defines actual behavior of the animation. The method interpolate() is called by the runtime in every frame while the animation is playing. A value frac, a double between 0.0 and 1.0 (both inclusive) is passed in, which specifies the current position. The value 0.0 marks the start of the animation, the value 1.0 the end. Any value in between defines the relative position. Note that a possible Interpolator is already taken into account when the value of frac is calculated.</p>
<h3>The class SpriteAnimation</h3>
<p>To demonstrate how a custom transition is defined, we will look at a class that allows us to do Sprite animations. It takes an image that has several frames and moves the viewport from one frame to the other over time. We will test this class with the famous &#8220;The Horse in Motion&#8221; by <a title="Eadweard Muybridge" href="http://commons.wikimedia.org/wiki/Eadweard_Muybridge">Eadweard Muybridge</a>. Enough talk, here is the code:</p>
<pre class="code" style="padding-left: 30px;"><span class="keyword-directive">package</span> sandboxfx;

<span class="keyword-directive">import</span> javafx.animation.Interpolator;
<span class="keyword-directive">import</span> javafx.animation.Transition;
<span class="keyword-directive">import</span> javafx.geometry.Rectangle2D;
<span class="keyword-directive">import</span> javafx.scene.image.ImageView;
<span class="keyword-directive">import</span> javafx.util.Duration;

<span class="keyword-directive">public</span> <span class="keyword-directive">class</span> <span class="ST_BOLD">SpriteAnimation</span> <span class="keyword-directive">extends</span> Transition {

    <span class="keyword-directive">private</span> <span class="keyword-directive">final</span> ImageView <span class="ST_GREEN">imageView</span>;
    <span class="keyword-directive">private</span> <span class="keyword-directive">final</span> <span class="keyword-directive">int</span> <span class="ST_GREEN">count</span>;
    <span class="keyword-directive">private</span> <span class="keyword-directive">final</span> <span class="keyword-directive">int</span> <span class="ST_GREEN">columns</span>;
    <span class="keyword-directive">private</span> <span class="keyword-directive">final</span> <span class="keyword-directive">int</span> <span class="ST_GREEN">offsetX</span>;
    <span class="keyword-directive">private</span> <span class="keyword-directive">final</span> <span class="keyword-directive">int</span> <span class="ST_GREEN">offsetY</span>;
    <span class="keyword-directive">private</span> <span class="keyword-directive">final</span> <span class="keyword-directive">int</span> <span class="ST_GREEN">width</span>;
    <span class="keyword-directive">private</span> <span class="keyword-directive">final</span> <span class="keyword-directive">int</span> <span class="ST_GREEN">height</span>;

    <span class="keyword-directive">private</span> <span class="keyword-directive">int</span> <span class="ST_GREEN">lastIndex</span>;

    <span class="keyword-directive">public</span> <span class="ST_BOLD">SpriteAnimation</span>(
            ImageView imageView, 
            Duration duration, 
            <span class="keyword-directive">int</span> count,   <span class="keyword-directive">int</span> columns,
            <span class="keyword-directive">int</span> offsetX, <span class="keyword-directive">int</span> offsetY,
            <span class="keyword-directive">int</span> width,   <span class="keyword-directive">int</span> height) {
        <span class="keyword-directive">this</span>.<span class="ST_GREEN">imageView</span> = imageView;
        <span class="keyword-directive">this</span>.<span class="ST_GREEN">count</span>     = count;
        <span class="keyword-directive">this</span>.<span class="ST_GREEN">columns</span>   = columns;
        <span class="keyword-directive">this</span>.<span class="ST_GREEN">offsetX</span>   = offsetX;
        <span class="keyword-directive">this</span>.<span class="ST_GREEN">offsetY</span>   = offsetY;
        <span class="keyword-directive">this</span>.<span class="ST_GREEN">width</span>     = width;
        <span class="keyword-directive">this</span>.<span class="ST_GREEN">height</span>    = height;
        setCycleDuration(duration);
        setInterpolator(Interpolator.<span class="ST_GREEN_ITALIC">LINEAR</span>);
    }

    <span class="keyword-directive">protected</span> <span class="keyword-directive">void</span> <span class="ST_BOLD">interpolate</span>(<span class="keyword-directive">double</span> k) {
        <span class="keyword-directive">final</span> <span class="keyword-directive">int</span> index = Math.<span class="ST_ITALIC">min</span>((<span class="keyword-directive">int</span>) Math.<span class="ST_ITALIC">floor</span>(k * <span class="ST_GREEN">count</span>), <span class="ST_GREEN">count</span> - 1);
        <span class="keyword-directive">if</span> (index != <span class="ST_GREEN">lastIndex</span>) {
            <span class="keyword-directive">final</span> <span class="keyword-directive">int</span> x = (index % <span class="ST_GREEN">columns</span>) * <span class="ST_GREEN">width</span>  + <span class="ST_GREEN">offsetX</span>;
            <span class="keyword-directive">final</span> <span class="keyword-directive">int</span> y = (index / <span class="ST_GREEN">columns</span>) * <span class="ST_GREEN">height</span> + <span class="ST_GREEN">offsetY</span>;
            <span class="ST_GREEN">imageView</span>.setViewport(<span class="keyword-directive">new</span> Rectangle2D(x, y, <span class="ST_GREEN">width</span>, <span class="ST_GREEN">height</span>));
            <span class="ST_GREEN">lastIndex</span> = index;
        }
    }
}</pre>
<p style="padding-left: 30px;"><strong>Listing 1: The class SpriteAnimation</strong></p>
<p>For the sake of simplicity, this example class just takes all parameters in the constructor and does not allow to change them later. In most cases this is sufficient.</p>
<p>The class expects an ImageView, the duration of a single cycle (that is how long it should take to go through all frames), the number of frames, the number of columns (how many frames are in one row in the image), the offset of the first frame, and the width and height of all frames. The duration of the full cycle is passed to the super class by calling setCycleDuration(), all other values are stored. As a last step in the constructor, the interpolator is set to linear. By default an easing interpolator is set for all transitions, because that is what usually gives the best results. But in our case we want to run through all frames with the same speed and an easing interpolator would look weird.</p>
<p>The interpolate() method takes the passed in value and calculates which frame needs to be displayed at the moment. If it changed since the last time interpolate() was called, the position of the new frame is calculated and the viewport of the ImageView set accordingly. That&#8217;s it.</p>
<h3>The Horse in Motion</h3>
<p>To demonstrate the SpriteAnimation class, we will animate The Horse in Motion. The code to do that is straightforward, most of the work is already done. It creates an ImageView with the viewport set to the first frame and instantiates the SpriteAnimation class. The parameters are just estimates, you may want to tweak them a little.</p>
<pre class="code" style="padding-left: 30px;"><span class="keyword-directive">package</span> sandboxfx;

<span class="keyword-directive">import</span> javafx.animation.Animation;
<span class="keyword-directive">import</span> javafx.application.Application;
<span class="keyword-directive">import</span> javafx.geometry.Rectangle2D;
<span class="keyword-directive">import</span> javafx.scene.Group;
<span class="keyword-directive">import</span> javafx.scene.Scene;
<span class="keyword-directive">import</span> javafx.scene.image.Image;
<span class="keyword-directive">import</span> javafx.scene.image.ImageView;
<span class="keyword-directive">import</span> javafx.stage.Stage;
<span class="keyword-directive">import</span> javafx.util.Duration;

<span class="keyword-directive">public</span> <span class="keyword-directive">class</span> <span class="ST_BOLD">SandboxFX</span> <span class="keyword-directive">extends</span> Application {

    <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">final</span> Image <span class="ST_GREEN_ITALIC">IMAGE</span> = <span class="keyword-directive">new</span> Image(<span class="character">"</span><span class="character">http://upload.wikimedia.org/wikipedia/commons/7/73/The_Horse_in_Motion.jpg</span><span class="character">"</span>);

    <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">final</span> <span class="keyword-directive">int</span> <span class="ST_GREEN_ITALIC">COLUMNS</span>  =   4;
    <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">final</span> <span class="keyword-directive">int</span> <span class="ST_GREEN_ITALIC">COUNT</span>    =  10;
    <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">final</span> <span class="keyword-directive">int</span> <span class="ST_GREEN_ITALIC">OFFSET_X</span> =  18;
    <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">final</span> <span class="keyword-directive">int</span> <span class="ST_GREEN_ITALIC">OFFSET_Y</span> =  25;
    <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">final</span> <span class="keyword-directive">int</span> <span class="ST_GREEN_ITALIC">WIDTH</span>    = 374;
    <span class="keyword-directive">private</span> <span class="keyword-directive">static</span> <span class="keyword-directive">final</span> <span class="keyword-directive">int</span> <span class="ST_GREEN_ITALIC">HEIGHT</span>   = 243;

    <span class="keyword-directive">public</span> <span class="keyword-directive">static</span> <span class="keyword-directive">void</span> <span class="ST_BOLD_ITALIC">main</span>(String[] args) {
        <span class="ST_ITALIC">launch</span>(args);
    }

    <span class="keyword-directive">public</span> <span class="keyword-directive">void</span> <span class="ST_BOLD">start</span>(Stage primaryStage) {
        primaryStage.setTitle(<span class="character">"</span><span class="character">The Horse in Motion</span><span class="character">"</span>);

        <span class="keyword-directive">final</span> ImageView imageView = <span class="keyword-directive">new</span> ImageView(<span class="ST_GREEN_ITALIC">IMAGE</span>);
        imageView.setViewport(<span class="keyword-directive">new</span> Rectangle2D(<span class="ST_GREEN_ITALIC">OFFSET_X</span>, <span class="ST_GREEN_ITALIC">OFFSET_Y</span>, <span class="ST_GREEN_ITALIC">WIDTH</span>, <span class="ST_GREEN_ITALIC">HEIGHT</span>));

        <span class="keyword-directive">final</span> Animation animation = <span class="keyword-directive">new</span> SpriteAnimation(
                imageView,
                Duration.<span class="ST_ITALIC">millis</span>(1000),
                <span class="ST_GREEN_ITALIC">COUNT</span>, <span class="ST_GREEN_ITALIC">COLUMNS</span>,
                <span class="ST_GREEN_ITALIC">OFFSET_X</span>, <span class="ST_GREEN_ITALIC">OFFSET_Y</span>,
                <span class="ST_GREEN_ITALIC">WIDTH</span>, <span class="ST_GREEN_ITALIC">HEIGHT</span>
        );
        animation.setCycleCount(Animation.<span class="ST_GREEN_ITALIC">INDEFINITE</span>);
        animation.play();

        primaryStage.setScene(<span class="keyword-directive">new</span> Scene(<span class="keyword-directive">new</span> Group(imageView)));
        primaryStage.show();
    }
}</pre>
<p style="padding-left: 30px;"><strong>Listing 2: The Horse in Motion with JavaFX</strong></p>
<h3>Conclusion</h3>
<p>Defining your own animation by extending the Transition class is simple and straightforward. It is an extremely powerful approach though, because an animation created this way has all the functionality regular animations have. For example you can play it slower and faster by changing the rate and you can play it even backwards. You can run it in a loop and you can use it within ParallelTransition and SequentialTransition to create even more complex animations.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.netopyr.com%2F2012%2F03%2F09%2Fcreating-a-sprite-animation-with-javafx%2F&amp;title=Creating%20a%20Sprite%20Animation%20with%20JavaFX" id="wpa2a_18"><img src="http://blog.netopyr.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.netopyr.com/2012/03/09/creating-a-sprite-animation-with-javafx/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Most often asked questions about JavaFX</title>
		<link>http://blog.netopyr.com/2012/02/23/most-often-asked-questions-about-javafx/</link>
		<comments>http://blog.netopyr.com/2012/02/23/most-often-asked-questions-about-javafx/#comments</comments>
		<pubDate>Thu, 23 Feb 2012 22:38:15 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.netopyr.com/?p=304</guid>
		<description><![CDATA[Last week I did a talk about JavaFX at Jfokus 2012 in Stockholm, when I realized that there are three questions I am asked at every event. There seems to be a general interest, so I try to answer them in this post (as much as I can to be honest): What about JavaFX on [...]]]></description>
				<content:encoded><![CDATA[<p>Last week I did a talk about JavaFX at Jfokus 2012 in Stockholm, when I realized that there are three questions I am asked at every event. There seems to be a general interest, so I try to answer them in this post (as much as I can to be honest):</p>
<ul>
<li>What about JavaFX on the iPad or other mobile devices?</li>
<li>Is Swing dead now?</li>
<li>How do I migrate my Swing application?<span id="more-304"></span></li>
</ul>
<p>Jfokus is definitely one of the top Java conferences in Europe. There is a nice <a href="https://blogs.oracle.com/arungupta/entry/jfokus_2012_trip_report">trip report from Arun Gupta</a> and another <a href="http://blog.eisele.net/2012/02/jfokus-2012-trip-report.html">trip report form Markus Eisele</a>. There were many great moments and I enjoyed the conference a lot, but the one thing I liked the most was the fact that I actually helped a good cause. Speakers sometimes get little presents from the conference organizers, but the awesome Jfokus team had a better idea. Instead of a present, they donated a package of 30 doses of measles vaccine and 40 doses of polio vaccine to Unicef for every speaker. What a great idea, I hope it finds lots of copycats!</p>
<p>After my <a title="Slides from my JavaFX talk at Jfokus" href="http://www.slideshare.net/michael_heinrichs/javafx-11583106">JavaFX talk</a>, I got a lot of interesting questions and I realized a pattern, because there are three question, that I am always asked. They are pretty tough questions and I cannot answer them as much as I would like, but I will do my best.</p>
<h3>What about JavaFX on the iPad or other mobile devices?</h3>
<p><em>I am sorry, but I am not allowed to comment.</em></p>
<p>As much as I would like to answer this question, I am not allowed to. You can believe me, this really bugs me, especially since I hear time after time how important (and cool) JavaFX on these devices would be. All I can say is, that we presented prototypes of JavaFX on different tablets at JavaOne last year and asked, if this is something the community wants. The feedback was overwhelming. We continue to look into this, but for now I can only humbly ask you for your patience.</p>
<h3>Is Swing dead now?</h3>
<p><em>No, but&#8230;</em></p>
<p>Swing is not dead. And as the <a title="JavaFX 2.0 FAQ" href="http://www.oracle.com/technetwork/java/javafx/overview/faq-1446554.html#6">JavaFX FAQs</a> say: &#8220;Swing will remain part of the Java SE specification for the foreseeable future, and is included in the JRE&#8221;. &#8211; But it is in maintenance mode now, no new features are being developed at this point. So if you want to use the new and cool stuff that was developed for JavaFX, you have to migrate to JavaFX or embed JavaFX in your Swing application. Which leads me directly to the last question.</p>
<h3>How do I migrate my Swing application?</h3>
<p><em>Honestly? I do not know.</em></p>
<p>I do not have an answer for this question yet. As a library developer, I do not write JavaFX applications. The only time I actually use the JavaFX runtime, is when I write small scripts to reproduce a bug or in my unit tests.</p>
<p>So for a start, here is what I am aware of. There is a tutorial about how to <a href="http://docs.oracle.com/javafx/2.0/swing/jfxpub-swing.htm">integrate JavaFX into a Swing application</a>. I know there are efforts going on to build a JavaFX RCPs (Rich Client Platforms) on top of NetBeans and Eclipse although I do not know much about these projects. They may help, if your application was build on top of the NetBeans or the Eclipse platform. And on our mail alias, there is currently an interesting debate about an application framework for JavaFX going on, that you are welcome to join (openjfx-dev@openjdk.java.net).</p>
<p>This is certainly just a start, but at least I became (finally&#8230; <img src='http://blog.netopyr.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  ) aware of this question and I hereby promise, that I will do my homework and try to find better answers.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.netopyr.com%2F2012%2F02%2F23%2Fmost-often-asked-questions-about-javafx%2F&amp;title=Most%20often%20asked%20questions%20about%20JavaFX" id="wpa2a_20"><img src="http://blog.netopyr.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.netopyr.com/2012/02/23/most-often-asked-questions-about-javafx/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
