Purpose: Since MPanel is derived from javax.microedition.lcdui.game.GameCanvas it's easy to use sprites for animated graphics. The sprite image is stored as frame that is part of an image that contains several frames of the same size. Some useful transformations are available such as flipping and rotation. A sprite collision detection mechanism is available that simplifies the construction of animated games. In the following example a series of sprite images are repeatedly shown.
Execute SpriteFrameGidlet (if you have the Sun's Wireless Toolkit (WTK) installed and the JAD extension registered. Learn how to register the JAD extension) Discussion: The image file teacher.png resides in the subdirectory res of the class root and should be packed into the MIDlet distribution jar file. It contains 12 sprite images of the same size.
In order to create a smooth animation, the following steps are necessary:
Fifth example Purpose: Because MForm is derived from Form, any of the usual GUI elements may be added to it. In the following example a ChoiceGroup is shown where the user may select one of the predefined system audio clips.
Execute ChoiceGidlet (if you have the Sun's Wireless Toolkit (WTK) installed and the JAD extension registered. Learn how to register the JAD extension) Discussion: If an ItemStateListener is registered, the callback method itemStateChanged() is triggered whenever the selection in the ChoiceGroup is changed. When running the phone emulator, use the cursor up/down key to change the group item and confirm the selection with the enter key.
Sixth example Purpose: The full set of Logo-like commands are implemented in the Turtle class. Since the famous work of Seymour Papert the Logo turtle is used from introductory courses in elementary schools to programming and math courses at university level. Turtle graphics combined with recursions let you create very easily the famous fractal pictures (Mandelbrot set, Julia set, Peano curve, etc.). Programming with the turtle is a great mental training for everyone and much fun due to the dynamic and esthetic outcome. In the following example the famous space-filling Hilbert curve is drawn.
Execute HilbertGidlet (if you have the Sun's Wireless Toolkit (WTK) installed and the JAD extension registered. Learn how to register the JAD extension) Discussion: For an explanation of the highly recursive algorithm consult Internet resources. The Turtle may be instantiated (globally) as class instance variable. Transparent to the user, the initialization is deferred until main() starts.
Seventh example Purpose: Mobile devices are more than just mobile phones. They are often used as portable music player. The Gidlet frameworks includes the class AudioPlayer that is a simple wrapper for the class Player in the Multimedia API (MMAPI) of JavaME. The following example simulates the usual command buttons of a real player: start to start playing from beginning or to resume playing after the pause command, pause to suspend playing for a while, stop to stop playing.
Execute PlayAudioGidlet (if you have the Sun's Wireless Toolkit (WTK) installed and the JAD extension registered. Learn how to register the JAD extension) Discussion: An instance of AudioPlayer creates the MMAPI Player and opens the connection to the audio resources and the output sound device. To avoid blocking, all work is done in a separate thread. doExit() must release all resources by calling close(). The callback method notifyPlayerUpdate() is called each time when the player's state changes. It may be used to inform the user when the playing is over. Be aware that it takes some time to load a big wav file from the internet. When running on a real device, it may cost you a lot of money! | ||||||||||||||