All the Interesting Things

This is a personal web log. All information posted here does not represent my employer. I do not speak for my employer.
Saturday May 16, 2009

Five Articles on JavaFX Pac-Man Game

After I wrote a Pac-Man game using the JavaFX API, many people were quite interested in the game. They either enjoyed playing it or asked me for the details fo the JavaFX code. JavaFX expert Jim Weaver invited me to write some articles about the process of building the game. After a few weeks' hard work, with Jim's constructive ideas and great help in proofreading, I finished the articles. Now they are published on insideRIA.com, an O'Reilly's web site, as featured articles. There will be 5 articles in total and they will run through the coming 5 weeks.

In each article, there are a few web start links that you can click on and start a JavaFX program to see how it works. If you follow the 5 articles, you will find out how the game is built bit by bit. I hope the articles can help people who want to learn JavaFX or want to develop games in JavaFX.

Here is the links for the articles:

May 14, 2009: Writing the Pac-Man Game in JavaFX - Part 1
May 21, 2009: Writing the Pac-Man Game in JavaFX - Part 2
May 28, 2009: Writing the Pac-Man Game in JavaFX - Part 3
June 4, 2009: Writing the Pac-Man Game in JavaFX - Part 4
June 11, 2009: Writing the Pac-Man Game in JavaFX - Part 5

Related articles:
My JavaFX Demo Game: PACMAN
JavaFX Demo Game: PAC-MAN
JavaFX Game
JavaFX Wish Tree
JavaFX

Thursday May 14, 2009

My Articles on Writing the Pac-Man Game in JavaFX

After I wrote the Pac-Man game with JavaFX, many people were asking me to give more details on writing the game. JavaFX guru Jim Weaver had invited me to write some articles on the game. Now, with Jim's help, our articles are featured on O'Reilly's insiderRIA.com . Here is the link to the articles:

Writing the Pac-Man Game in JavaFX

Friday Jan 02, 2009

JavaFX技术中文博客

新年伊始,趁着假期,把几篇英文的JavaFX博文翻译了一下,以后争取中英两种语言一起发吧,可以参考这里:

JavaFX技术 学习 编程 演示 教程 例子程序范例 示范

javaFX guy的博客 教程 例子示范游戏 学习 编程

JavaFX技术 交流 学习

Java和JavaFX之间的互操作性 兼容性

ABC Canadian Citizenship Test

Friday Dec 26, 2008

Interoperability between JavaFX and Java

The JavaFX blog has an article which discussed the possiblity of a SWING application to invoke methods of JavaFX classes from Java code. Right now, JavaFX can call Java code without any problem, however, the reverse is not supported by JavaFX. Doing some googling shows that programmers are trying all kinds of hacks to invoke a JavaFX class method from Java. You can check out an interesting article on reverse engineering of JavaFX classes. Even the example on JavaFX blog provided a hack to work around this.

So do we have the need of such kind of interaction between Java and JavaFX? I vote it for "YES". If Java and JavaFX can be used interchangeably (when possible), this could give more life to JavaFX in the long run. Just consider the MVC design pattern, we can write an application by using Java and JavaFX together. The "M" and "C" part can be implemented in Java while the "V" can be done by JavaFX. It would be very interesting to see this.

Right now, there are a few "standard" ways to call JavaFX from Java:

1) Using the ScriptEngineManager class. From Geertjan Wielenga's article, we can do it in this way:

package calc; 
 
import java.io.InputStreamReader; 
import javax.script.ScriptEngine; 
import javax.script.ScriptEngineManager; 
import javax.script.ScriptException; 
 
public class CalculatorLauncher { 
 
public static void main(String[] args) { 
 try { 
 ScriptEngineManager manager=new ScriptEngineManager(); 
 ScriptEngine engine = manager.getEngineByExtension("fx"); 
 InputStreamReader reader = new InputStreamReader
 (CalculatorLauncher.class.getResourceAsStream
 ("Calculator.fx")); 
 engine.eval(reader); 
   } catch (ScriptException ex) { 
  } 
 } 
}
However, this is just like System.exec("calc") as pointed out by cronseaux. I agree with him. A even simpler way is to use System.exec("javafx Calculator.fx") to complete the above code. So this is not a good solution.

2) Manage to use java reflection to call JavaFX class methods. This one should work because JavaFX classes are compiled into Java classes and byte code. However, the complexity makes it almost unusable and code written in this way has no readability.

3) A third approach is to define an interface in Java and implement it in JavaFX. For example,

public interface JavaInterface
{ ... }
In MyJavaFXClass.fx, do something like:
public class MyJavaFXClass extends JavaInterface
{ ... }
In you java code, just invoke the JavaFX object directly using the interface. This approach can solve most of the interoperation issues. Just that an interface is needed for every JavaFX class which is going to be called from Java. Though it is very cumbersome, at least it is the best workaround I can find so far.

Since this is the first release of JavaFX, I don't criticize much on this given the strong powerful features of JavaFX. I do hope the future releases of JavaFX can improve on this.

JavaFX Blog
JavaFX Game



other articles:
American US Citizenship APplication Forms
Online Practice Test for US Citizenship Applicants

Monday Dec 08, 2008

My First JavaFX Game: PAC MAN (updated example)

I updated my JavaFX PACMAN game with some features today:

1) The ghosts can run away from the pacman after the pacman eats the magic bean.
2) The period of a ghost staying hollow is adjusted based on the level of the game.

Right now, 85% of the game is completed. Things to do:

  1. add some audio
  2. modify the ghosts images
  3. add in strawberry, orange etc to stand for level
  4. add in a "Pause" key (this one should be easy)
  5. add in a splash screen
  6. add in some description or menu
  7. add in eyes after the ghosts are eaten by pacman

JRE 1.5+ required, JRE1.6 U10 is better, click on the below image or button to start playing, or watch it on Youtube:

screenshot


Need to wait a little bit when you are the first time launching the game ...

If you cannot play now, you can watch the video:



other articles:
Diecast cars of NASCAR
Online Practice Test for US Citizenship Applicants


Archives
Links
Referrers