Thursday, October 13, 2011

Pac-Man in JavaFX 2

JavaFX 2 Beta was FINALLY released for Mac OS X. I thought I would learn it by porting one of my favorite JavaFX 1 games: Pac-Man! The original code was written by Henry Zhang. He wrote a five-part series of articles (Part 1, Part 2, Part 3, Part 4, Part 5) explaining the design of the game. He also wrote a neat widget for the game.

I ported the original JavaFX 1 code to JavaFX 2 using the Java SE bundle of Netbeans 7.1 Beta on my MacBook. If you want to play the game on a Mac you can install the SDK and samples, download my JAR file, and place it in your samples folder. Double-click the pacman.jar file from within the javafx-samples-2.0 folder to launch the game. Make sure you install the samples according to the instructions here.

If you are on Windows, you will need to have Java SE 6 Update 26 or later and the JavaFX 2 runtime installed. If everything is installed correctly, you can click here to launch Pac-Man in Java Web Start. Or you can play in the applet below. If JavaFX 2 is not installed correctly, you will see an image asking you to install JavaFX rather than a working applet.


I can post the source code here or put it on GitHub if anyone is interested. Right now, the code is a bit messy. It still has all of Henry Zhang's original JavaFX 1 code commented-out inline with the new JavaFX 2 code. Enjoy the game!