This article picks up where Moving to NetBeans IDE 5.5.1 BlueJ Edition leaves off by making the Zuul game a server which can then be played on clients other than then existing Swing GUI (web browser, cell phone) as well a playable in other languages. Each step along the way has a solution project that you can use to compare against your work if things go horribly wrong.
The BlueJ Edition of NetBeans is capable of opening projects in BlueJ's project format. To take advantage of additional NetBeans features like the Mobility Pack, we need to convert the project to NetBeans native project format.
Start NetBeans IDE 5.5.1 BlueJ Edition and open the zuul-for-netbeans project.
Switch to the Projects tab and right-click the zuul-for-netbeans project. Select Convert to J2SE project.
For the Destination Folder, click the Browse button to open the Open dialog.
Click the Create New Folder icon and name the folder zuul-converted.
Select the folder and click Open, then click Finish.
You now have 2 zuul-for-netbeans projects open in the IDE, one in BlueJ project format and the other in NetBeans project format.
NetBeans 5.5 supports the latest Java EE standards, which we'll use to create our game server.
Open the zuul-converted project in NetBeans 5.5.
Test the project, press F6. If you're prompted to select the main class, select zuul.MainFrame.
Internationalize Zuul
If Zuul's going to be playable everywhere, we need to change it so it's easy to support languages other than English. As it's currently written, all of our strings are interlaced throughout our Java code.
If we wanted to create a version in another language, say Spanish, we'd have to find all the strings and replace them. For a large project, this search and replace can be quite time consuming. Not to mention that we now have two versions of our file, one in English and another in Spanish, which creates maintenance nightmares moving forward.
A Better Way
We can internationalize the program so that all of the strings interlaced throughout our Java code are instead stored in one file, reference by a key in our Java code. Then to add a new language (localize), we just need to have someone produce a new version of the file with all of the text strings.
Create a new properties file named bundle to hold the strings. Put it in the src\zuul folder.
Right click the Source Packages node and select Tools > Internationalization > Internationalization Wizard. Click Next and Next to get to the Modify Founds Strings page. Here, use the Source combo box to switch between the Java files and uncheck any strings that we don't want to internationalize. Certainly uncheck all of the newline markers, "/n", as these don't need to be changed.
Once complete, you source code will be updated to pull text values from the bundle.properties file:
Press F6 to run the project. You shouldn't notice any difference. However, all of our strings are now being pulled from the bundle.properties file, so it's easy to localize.
Localize to Another Language
Once our project is Internationalized, we can begin the process of localizing it to other languages.
Right-click the bundle.properties file and choose Add Locale. You can choose whatever local you want, but for this tutorial I'm using Spanish (and I'll provide the translations).
Then if you expand the bundle.properties, you'll see the new new locale node.
Double-click it to open its bundle.properties file.
NetBeans doesn't do language translations yet, so you need to do this process manually. If you stuck with me and went with Spanish, you can copy the translations from this file here.
Now since Spanish isn't the default language on our machines (at least not mine), we need to tell the VM to run using the Spanish language. Open the project properties and add the following to the VM Options under the Run node: -Duser.language=ES -Duser.region=CO.
Press F6 to run your project again...
Notice how the buttons automatically resized to accommodate the longer text. Another beauty of the NetBeans GUI builder.
Here we build an application that allows us to play Zuul on our phones. However, instead of porting the Zuul game to Java ME, we'll use the Zuul server for the game logic and just build the client interface in Java ME.
Create a new Mobile Application named ZuulMobileApp. Uncheck the option to create the Hello MIDlet and click Finish.
Add a new J2ME Web Service Client.
Enter the WSDL URL that you noted above and click Retrieve WSDL, then Finish
Create the MIDlet
As part of the web service client generation, a sample MIDlet was created which will run the web service's operations. We'll create a more appropriate interface that better understands the flow of the game, but feel free to test the sample MIDlet.
Start the Game
Create a new Visual MIDlet named Zuul.
Drag a List, WaitScreen, Alert and a Form from the Palette onto the Flow Designer.
Drag 2 List Elements onto the List. Name them Play Zuul and Quit.
Connect the screens as shown below:
Double-click the form to open it in the Screen Designer and set the Title property to Zuul.
Drag a StringItem onto the form and set it's Label property to Zuul Says:
Set the Instance Name to stringItemOutput.
Drag a SimpleCancellableTask onto the WaitScreen.
Set it's Executable Method Body property to play();
Switch to the Source view and add the following code:
At this point you should be able to start the game and see the welcome text:
Completing the Mobile UI
Next will add user interface support for moving around and displaying help.
Open form1 in the Screen Designer and add a Choice Group and 4 Choice Elements. Set the Choice Groups Label to Go and its Type to EXCLUSIVE. Change the Choice Element's text to North, South, East, and West.
Drag OK, Exit and Help commands onto the Screen
Change the label of the OK command to Go and the Instance Name to goCommand.
Your screen should now look something like the following:
Switch to the Source view and add the following methods:
If you haven't already done so, install the Visual Web Pack.
Create a new Visual Web Application project called ZuulWeb.
Drop the necessary components on the page. I've come up with this boring but functional design (feel free to improve upon):
Create the Web Service Client
Add a new Web Service Client.
Enter the WSDL URL that you noted above. The default value should be http://localhost:8080/ZuulWebServiceService/ZuulWebService?WSDL.
Set the Package to zuulws and click Finish
Establish a Reference to the Web Service Client
Underneath the covers, the Visual Web Pack is generating a Java Server Faces (JSF) compliant application and it helps to have a basic familiarity with JSF when using the VWP. When we created our project, 3 managed beans were created for us, Application, Request and Session.
We're going to use the Session bean to hold a reference to our web service throughout our user session.
Double-click SessionBean1 to open it in the Java editor.
Add the following fields and accessor methods:
private ZuulWebService game;
private boolean playing = false;
public ZuulWebService getGame() { return game; }
public boolean isPlaying() { return playing; }
Expand the init method and add the following to the bottom:
if (game == null) { ZuulWebServiceService service = new ZuulWebServiceService(); game = service.getZuulWebServicePort(); }
The add the following play method to call our web service operation:
public String play() { String result = game.play(); playing = true;
return result; }
Link the Web Service Operations to the Page
Double-click a blank spot of the Designer to open the Java editor.
Our current architecture has one big problem, especially for a game server - it's stateless! Try running the mobile and web clients at the same time. You can move the player to the computer lab with the web client, and then to the admin office with the mobile client. Cool, but probably not the desired behavior. I'll leave this piece of the puzzle for a latter tutorial. As a hint, I'll be using the new Stateful Web Services coming with JAX-WS 2.1.