Some reader favorites:
EJB fundamentals and session beans
Create a scrollable virtual desktop in Swing
Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API
The games industry is the biggest player in the J2ME market. When you're programming J2ME applications, chances are that you are working on games. Gaming is a tremendous success story for mobile Java and belongs to the "3 Gs" that supposedly form the main revenues in the mobile arena: games, gambling, and girls. The gaming market is growing rapidly and has an estimated volume of billion worldwide in 2005.
The industry has reacted by introducing great enhancements for game programming in the Mobile Information Device Profile 2.0 standard through the Java Community Process Website. Unfortunately, the majority of phones out there support only the MIDP 1.0 standard. This is where J2ME Polish's game engine comes to the rescue. As you'll learn in this chapter, you can use the game engine of J2ME Polish for quickly porting MIDP 2.0 games to MIDP 1.0 platforms.
Programming games using the MIDP 2.0 game API is easy. You have sprites, a layer manager with a view window, and much more.
These features are sorely missed when you develop your game for MIDP 1.0 platforms. This is why J2ME Polish includes a wrapper
API that enables you to use the javax.microedition.lcdui.game.* API, even on MIDP 1.0 devices. The game engine allows you to use the complete MIDP 2.0 game API on MIDP 1.0 devices. Usually,
no source code changes are necessary when you use proper import statements. However, there are some principal restrictions
that might require adjustments of your code, especially the inability to use the pixel-level collision detection. Also, not
all MIDP 1.0 platforms support sprite transformations. Dealing with these limitations is discussed in the "Working around
the Limitations of the Game Engine" section later in this article.
Using the game engine is not difficult. J2ME Polish weaves the wrapper classes in your code automatically when you target MIDP 1.0 devices. This is done just by exchanging the import statements, so you must use proper import statements instead of fully qualified class names. Listing 1 demonstrates what you should not do.
Listing 1. How not to use the game engine
public class MyGameCanvas
extends javax.microedition.lcdui.game.GameCanvas
implements Runnable
{
public MyGameCanvas(boolean supress) {
super(supress);
}
public void run() {
// main game-loop
}
}
The code in Listing 1 won't work when you target a MIDP 1.0 device. Listing 2 shows a working example, which uses import statements properly.
Listing 2. Proper use of the game engine with import statements
import javax.microedition.lcdui.game.GameCanvas;
public class MyGameCanvas
extends GameCanvas
implements Runnable
{
public MyGameCanvas(boolean supress) {
super(supress);
}
public void run() {
// main game-loop
}
}
You can tweak the game engine by defining several preprocessing variables in the <variables> section of your build.xml script. The following optimizations are available:
TiledLayer by using a back buffer and/or single tile images
Usually, you will want to use the full-screen mode for your game. To enable this mode, use the fullscreen attribute of the <build> element in your project's build.xml file. Possible values are true, false, or menu. The menu mode allows you to design the menu bar, but it is available only when you use the J2ME Polish GUI.
The fullscreen attribute of the <build> element enables the full-screen mode for the complete application. If you want to use a different setting for the actual
game play, define a specific setting for the GameCanvas implementation by setting the polish.GameCanvas.useFullScreen preprocessing variable. Allowed values are again true, false, or menu. Listing 3 demonstrates how you can use the menu full-screen mode for your application, while using the regular full-screen mode for the GameCanvas.
Listing 3. Enabling the full-screen mode for the GameCanvas
<j2mepolish>
<info
license="GPL"
name="MazeRace"
vendorName="A reader."
version="0.0.1"
jarName="${polish.vendor}-${polish.name}-${polish.locale}-mazerace.jar"
/>
<deviceRequirements>
<requirement name="Identifier" value="Nokia/Series60" />
</deviceRequirements>
<build
usePolishGui="true"
fullscreen="menu"
>
<midlet class="com.apress.mazerace.MazeRace" />
<variables>
<variable
name="polish.GameCanvas.useFullScreen"
value="true"
/>
</variables>
</build>
<emulator />
</j2mepolish>
You need to enable the menu mode when you add commands to your GameCanvas. The menu mode requires the J2ME Polish GUI, which, in turn, requires that you do not implement the paint( Graphics ) method, because this method is used by the J2ME Polish GUI internally.
In such cases, you should use the flushGraphics() method instead. When this is not possible, you can use some preprocessing for implementing the paintScreen( Graphics ) method instead of the paint( Graphics ) method, as shown in Listing 4.
Listing 4. Using the paintScreen() method instead of paint() in the menu full-screen mode
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.Graphics;
public class MyGameCanvas
extends GameCanvas
implements Runnable
{
public MyGameCanvas(boolean supress) {
super(supress);
}
public void run() {
// Main game loop
}
//#if polish.usePolishGui && polish.classes.fullscreen:defined && !polish.midp2
//# public void paintScreen( Graphics g )
//#else
public void paint( Graphics g )
//#endif
{
// Implement the paint method
}
}
The TiledLayer paints several tiles on the screen and is often used to render the background of a game. The back buffer optimization uses
an internal image buffer to which the tiles are painted only when they have changed. Instead of painting all tiles individually,
the complete buffer will be painted to the screen. This can quite dramatically increase the speed of your game, especially
in cases when the visible tiles are changed only occasionally.
| Subject | Replies |
Last post
|
|
By Witbit |
1 |
02/07/08 04:13 AM
by j2m3 |
|
By JavaWorld
|
0 |
02/26/07 08:12 AM
by JavaWorld |
Free Download - 5 Minute Product Review. When slow equals Off: Manage the complexity of Web applications - Symphoniq
![]()
Free Download - 5 Minute Product Review. Realize the benefits of real user monitoring in less than an hour. - Symphoniq