The Maze3DMIDlet controls the transitions between the different screens.
The canvas3D
field holds the game, and there are two
extra screens for the MenuList
and Graphics3DProperties
classes.
If the MIDlet is stopped or paused, the canvas3D
is stopped
to kill the Game
thread.
Create the Maze3DMIDlet
class
file.
Import the required classes.
import java.util.*; import javax.microedition.lcdui.*; import javax.microedition.m3g.*; import javax.microedition.midlet.*;
Set Maze3DMIDlet
to
extend MIDlet. Make sure it includes the creation of a MazeCanvas
object,
an Image
object, and a MenuList
object.
public class Maze3DMIDlet extends MIDlet { private final MazeCanvas canvas3D; private final Image logo; private MenuList menuList;
Write the below code to construct the MIDlet.
//Construct the midlet public Maze3DMIDlet() { logo = makeImage("/logo.png"); canvas3D = new MazeCanvas(this); ErrorScreen.init(logo, Display.getDisplay(this)); }
Construct
the startApp
, pauseApp
and destroyApp
methods,
required for a MIDlet to run.
public void startApp() { Displayable current = Display.getDisplay(this).getCurrent(); if (current == null) { // check that the API is available boolean isApiAvailable = (System.getProperty("microedition.m3g.version") != null); menuList = new MenuList(this, isApiAvailable); if (!isApiAvailable) { quitApp(); } else { // Display a splash screen and then the game StringBuffer splashText = new StringBuffer("") .append(getAppProperty("MIDlet-Name")). append("\n").append(getAppProperty("MIDlet-Vendor")); Alert splashScreen = new Alert("Maze3D", splashText.toString(), logo, AlertType.INFO); splashScreen.setTimeout(3000); Display.getDisplay(this).setCurrent(splashScreen, menuList); } } else { // In case the MIDlet has been hidden if (current == canvas3D) { canvas3D.start(); } Display.getDisplay(this).setCurrent(current); } } public void pauseApp() { canvas3D.stop(); } public void destroyApp(boolean unconditional) { canvas3D.stop(); }
Create the methods used to control the MIDlet and load the required graphics.
// gets informed when the canvas itself switches the view void viewSwitched() { menuList.viewSwitched(); } // starts a new ga,e void newGame() { canvas3D.stop(); canvas3D.init(); Display.getDisplay(this).setCurrent(canvas3D); } // shows the menu thread void showMenu() { canvas3D.stop(); Display.getDisplay(this).setCurrent(menuList); } // quits the app but first stops the game thread void quitApp() { canvas3D.stop(); notifyDestroyed(); }
The Graphics3D
class
is a singleton 3D graphics context that can be bound to a rendering target.
All rendering is done through the render methods in this class, including
the rendering of World
objects. The getInstance
method
is used to retrieve the singleton Graphics3D
instance
associated with this application. For more information, see Graphics3D
and getInstance
in the M3G API specification.
// shows the Graphics3D properties list void show3DProperties() { Graphics3D g3d = Graphics3D.getInstance(); Display.getDisplay(this).setCurrent( new Graphics3DProperties(g3d, this)); } // switches the canvas' view void switchView() { canvas3D.switchView(); canvas3D.start(); Display.getDisplay(this).setCurrent(canvas3D); } // shows the main canvas void showMain() { canvas3D.start(); Display.getDisplay(this).setCurrent(canvas3D); } // loads a given image by name static Image makeImage(String filename) { Image image = null; try { image = Image.createImage(filename); } catch (Exception e) { // use a null image instead } return image; } }