Deprecated: Assigning the return value of new by reference is deprecated in /home/citiz68/public_html/javafxgame/wp-settings.php on line 472

Deprecated: Assigning the return value of new by reference is deprecated in /home/citiz68/public_html/javafxgame/wp-settings.php on line 487

Deprecated: Assigning the return value of new by reference is deprecated in /home/citiz68/public_html/javafxgame/wp-settings.php on line 494

Deprecated: Assigning the return value of new by reference is deprecated in /home/citiz68/public_html/javafxgame/wp-settings.php on line 530

Deprecated: Assigning the return value of new by reference is deprecated in /home/citiz68/public_html/javafxgame/wp-includes/cache.php on line 103

Deprecated: Assigning the return value of new by reference is deprecated in /home/citiz68/public_html/javafxgame/wp-includes/query.php on line 21

Deprecated: Assigning the return value of new by reference is deprecated in /home/citiz68/public_html/javafxgame/wp-includes/theme.php on line 623
Pac-Man Widget for WidgetFX 1.2

Pac-Man Widget for WidgetFX 1.2

Posted: June 8th, 2009 under JavaFX News, JavaFX Technology, pac-man.
Tags: , , ,

Bookmark and Share

To fully take advantage of the power of JavaFX 1.2, Stephen Chin just released the WidgetFX 1.2 API beta version. There is even a widget contest running until the end of July. [Update: Steve had released the WidgetFX 1.2 version on June 29. ]


Just before the JavaOne 2009, Jim Weaver asked me to write a widget for my JavaFX Pac-man game. The WidgetFX API was quite simple to use, so I finished it pretty soon. The game widget later got demo-ed on Jim and Steve’s JavaOne sessions. There was a small problem of the pac-man widget: it run relatively slow due to the performance issue of JavaFX 1.1. Since JavaFX 1.2 and WidgetFX 1.2 are ready now, I am modifying the code to see the improvement on performance.


First, the JavaFX code of the Pac-Man game needs to be modified a little bit for JavaFX 1.2. Since multi inheritance is gone, we need to use mixin classes now. You can refer to my articles on insideRIA.com for details of the code. Changes for JavaFX 1.2 were given on comments of Article 4 by Patrick Webster. I also added in a pausing key(”P” button) handling for the game. I compiled the game into a pacman.jar file.


The next step is to write the widget. Actually, the code is quite simple if you do not have stuffs like configuartion etc. Let’s look at the code below:

/*
 * PacManWidget.fx
 * http://www.javafxgame.com
 */

package pacmanwidget;

import org.widgetfx.Widget;

/**
 * @author Henry Zhang
 */

def defaultWidth = 528.0;
def defaultHeight = 576.0;

def maze =  pacman.Maze {};

var widget:Widget = Widget {
    width: defaultWidth
    height: defaultHeight
    aspectRatio: defaultWidth / defaultHeight
    content: maze
    resizable: false

    onDock: function():Void {
       maze.pauseGame();
    }
}

return widget;

In the standalone game, an instance of the Maze class was put into the content variable of a Stage. Now, instead of putting it into a Stage, we add it into a Widget. To do this, we can just set the content variable of a Widget instance. Other attributes of the Widget class are quite straightforward, mostly for resizing purposes. The next thing is to write a onDock() function to pause the game when the widget gets docked. The game can be resumed after pressing the “p” button when it is undocked.


The last thing is to deploy it on a web server. We need a JNLP file. Be sure to write the jnlp file of the JavaFX 1.2 style. Netbeans can generate the JNLP file which we can modify for deployment. I listed below part of my jnlp file. Besides the widget code PacManWidget.jar, there are supporting jar files( pacman.jar and WidgetFX-API.jar) under the /lib folder as well. Notice that there is a bug in the generated JNLP file by NetBeans 6.5.1: the <update> tag is missing a slash(/) at the end of the tag.


 . . . . . .
 <resources>
     <j2se version="1.5+" />
     <extension name="JavaFX Runtime"
        href="http://dl.javafx.com/1.2/javafx-rt.jnlp"/>
     <jar href="PacManWidget.jar" main="true"/>
     <jar href="lib/pacman.jar"/>
     <jar href="lib/WidgetFX-API.jar"/>
 </resources>
 <application-desc main-class="com.sun.javafx.runtime.main.Main">
    <argument>MainJavaFXScript=pacmanwidget.PacManWidget</argument>
 </application-desc>
 <update check="background" />
 . . . . . .


Now, you can click on the below button to start the Pac-man widget for JavaFX 1.2. Enjoy!


Pac-Man Widget 1.2



Bookmark and Share

3 Comments

  1. [...] http://www.javafxgame.com/pac-man-widget-for-widgetfx-12/ [...]

    Pingback by WidgetFX 1.2 Beta Announcement « Steve on Java — June 18, 2009 @ 7:41 am

  2. [...] left-to-right: Clock, WebFeed, Weather, SlideShow, Pac-Man, World Clock, [...]

    Pingback by WidgetFX 1.2 Release Announcement « Steve on Java — June 30, 2009 @ 6:19 am

  3. [...] if you’re interested in creating games in JavaFX. Relatedly, he posted that the game has been ported to work on WidgetFX 1.2. Finally, Haining runs a blog that has a number of interesting posts related to JavaFX [...]

    Pingback by Java desktop links of the week, June 22 | Jonathan Giles — July 16, 2009 @ 4:14 pm

RSS feed for comments on this post.