Ch06 code
From SCMAD Book
Finding the area of a Canvas (Listing 6.1)
package com.scmadkit.ch06; import javax.microedition.lcdui.game.GameCanvas; import javax.microedition.lcdui.Graphics; /** * This GameCanvas draws a white background rectangle, draws a small blue * rectangle whose top-left is at (0, 0) position of the GameCanvas and draw two * strings about its own height and width. * * * @author Ko Ko Naing */ public class AreaGCanvas extends GameCanvas { /** * Creates a new instance of AreaGCanvas */ public AreaGCanvas() { /* Calls the constructor of the GameCanvas */ super(false); } /** * Render the GameCanvas * * @param myGraphics Graphics */ public void paint(Graphics myGraphics) { /* Sets the current color to white */ myGraphics.setColor(255, 255, 255); myGraphics.fillRect(0, 0, this.getWidth(), this.getHeight()); /* Sets the current color to blue */ myGraphics.setColor(0, 0, 255); myGraphics.drawRect(0, 0, 100, 100); /* Draws a string about the current width of the GameCanvas */ myGraphics.drawString("Width = " + this.getWidth(), 100, 120, Graphics.TOP | Graphics.LEFT); /* Draws a string about the current height of the GameCanvas */ myGraphics.drawString("Height = " + this.getHeight(), 100, 100, Graphics.TOP | Graphics.LEFT); } }
Adding a Ticker to a Canvas (Listing 6.2)
package com.scmadkit.ch06; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * This MIDlet is to test the effect caused by adding a Ticker on the * GameCanvas. * * * @author Ko Ko Naing */ public class AreaMIDlet extends MIDlet { private Display myDisplay; private AreaGCanvas areaGCanvas; private Ticker myTicker; /** * Creates a new instance of AreaMIDlet */ public AreaMIDlet() { /* Gets the Display object of this MIDlet */ myDisplay = Display.getDisplay(this); /* Creates a new instance of AreaGCanvas */ areaGCanvas = new AreaGCanvas(); } /* Signals that the MIDlet has entered the Active state */ protected void startApp() { //myTicker = new Ticker("SCMAD Exam Study Kit - MIDP Game API"); /* Sets the Ticker to the AreaGCanvas */ areaGCanvas.setTicker(myTicker); /* Sets the AreaGCanvas as the current display */ myDisplay.setCurrent(areaGCanvas); } protected void destroyApp(boolean unconditional) { } protected void pauseApp() { } }
Printing event triggers in a Canvas (Listing 6.3)
package com.scmadkit.ch06; import javax.microedition.lcdui.game.GameCanvas; public class KeySuppressionGCanvas extends GameCanvas { /** * Creates a new instance of KeySuppressionGCanvas */ public KeySuppressionGCanvas(boolean isSuppressed) { /* Calls the constructor of the GameCanvas */ super(isSuppressed); /* Checks to see if the device supports repeating key events */ if (!hasRepeatEvents()) { System.out.println("Repeating key events not supported."); } } public void keyPressed(int keyCode) { System.out.println("A key is pressed."); } public void keyReleased(int keyCode) { System.out.println("A key is released."); } public void keyRepeated(int keyCode) { System.out.println("A key is repeated."); } }
Effects caused by key suppression (Listing 6.4)
package com.scmadkit.ch06; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * This MIDlet is to test the effect caused by key suppression parameter on an * instance of GameCanvas. * * * @author Ko Ko Naing */ public class KeySuppressionMIDlet extends MIDlet { private Display myDisplay; private KeySuppressionGCanvas keySuppressionGCanvas; private boolean isSuppressed; /** * Creates a new instance of KeySuppressionMIDlet */ public KeySuppressionMIDlet() { /* Gets the Display object of this MIDlet */ myDisplay = Display.getDisplay(this); /* * Sets the boolean value that is to be passed into GameCanvas * constructor */ isSuppressed = false; /* Creates a new instance of a KeySuppressionGCanvas */ keySuppressionGCanvas = new KeySuppressionGCanvas(isSuppressed); } /* Signals that the MIDlet has entered the Active state */ protected void startApp() throws MIDletStateChangeException { /* Sets the GameCanvas as the current display */ myDisplay.setCurrent(keySuppressionGCanvas); } protected void destroyApp(boolean unconditional) { } protected void pauseApp() { } }
Sprite Sequence (Listing 6.5)
package com.scmadkit.ch06; import javax.microedition.lcdui.game.*; import javax.microedition.lcdui.*; import java.io.IOException; /** * This SpriteSeqGCanvas draws a rectangle with white background, append a * Sprite into the LayerManager and render the LayerManager. The Sprite will be * an animated person on the display, rotating its frame sequence. * * * @author Ko Ko Naing */ public class SpriteSeqGCanvas extends GameCanvas implements Runnable { Sprite sprite; LayerManager layerManager; Thread thread; /** * Creates a new instance of SpriteSeqGCanvas */ public SpriteSeqGCanvas() { /* Calls the constructor of GameCanvas */ super(false); try { sprite = new Sprite(Image.createImage("/res/man1.png"), 30, 60); int[] spriteSeq = { 0, 1, 2, 3, 0, 3, 0, 3 }; /* Sets the frame sequence to the Sprite */ sprite.setFrameSequence(spriteSeq); /* Creates a LayerManager instance */ layerManager = new LayerManager(); /* Appends the Sprite into the LayerManager */ layerManager.append(sprite); } catch (IOException ioe) { System.out.println("I/O Access Error."); } } /** * Renders the GameCanvas * * @param myGraphics Graphics */ public void paint(Graphics myGraphics) { /* Sets the current color to white */ myGraphics.setColor(255, 255, 255); /* Fills a rectangle to the maximum size of the canvas */ myGraphics.fillRect(0, 0, this.getWidth(), this.getHeight()); /* Renders the LayerManager's view window */ layerManager.paint(myGraphics, 100, 100); } /** * Creates a new thread for this SpriteSeqGCanvas instance and start the * thread instance */ public synchronized void start() { thread = new Thread(this); thread.start(); } public void run() { while (true) { sprite.nextFrame(); // sprite.prevFrame(); /* Repaints the canvas */ repaint(); /* */ try { Thread.sleep(500); } catch (InterruptedException err) { System.out.println(err.getMessage()); } } } }
Sprite Sequence MIDlet (Listing 6.6)
package com.scmadkit.ch06; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * This MIDlet is to test the effect caused by changing the frame sequence of a * Sprite. * * * @author Ko Ko Naing */ public class SpriteSeqMIDlet extends MIDlet { private Display myDisplay; private SpriteSeqGCanvas spriteSeqGCanvas; /** * Creates a new instance of SpriteSeqMIDlet */ public SpriteSeqMIDlet() { /* Gets the Display object of this MIDlet */ myDisplay = Display.getDisplay(this); /* Creates a new instance of GameCanvas */ spriteSeqGCanvas = new SpriteSeqGCanvas(); } /* Signals that the MIDlet has entered the Active state */ protected void startApp() { spriteSeqGCanvas.start(); /* Sets the GameCanvas as the current display */ myDisplay.setCurrent(spriteSeqGCanvas); } protected void destroyApp(boolean unconditional) { } protected void pauseApp() { } }
Collision between two Sprite objects
package com.scmadkit.ch06; import javax.microedition.lcdui.game.*; import javax.microedition.lcdui.*; import java.io.IOException; /** * This SpriteCollisionGCanvas draws a rectangle, with white background, append * two Sprites into the LayerManager, render the LayerManager, and check if the * two Sprites collide. * * * @author Ko Ko Naing */ public class SpriteCollisionGCanvas extends GameCanvas implements Runnable { Sprite manSprite; Sprite ballSprite; LayerManager layerManager; Thread thread; /** * Creates a new instance of SpriteCollisionGCanvas */ public SpriteCollisionGCanvas() { /* Calls the constructor of GameCanvas */ super(false); try { /* Creates a Sprite instance for a walking stick man */ manSprite = new Sprite(Image.createImage("/StickManWalk.png"), 21, 43); /* Places the man at an appropriate position */ manSprite.setPosition(0, 100); /* Creates a Sprite instance for a hanging ball */ ballSprite = new Sprite(Image.createImage("/HangingBall.png"), 21, 106); /* Places the ball at an appropriate position */ ballSprite.setPosition(150, 0); /* Creates a frame sequence to be displayed */ int[] spriteSeq = { 0, 1 }; /* Sets the frame sequence to the walking man Sprite */ manSprite.setFrameSequence(spriteSeq); /* Creates a LayerManager instance */ layerManager = new LayerManager(); /* Appends the Sprites into the LayerManager */ layerManager.append(manSprite); layerManager.append(ballSprite); } catch (IOException ioe) { System.out.println("I/O Access Error."); } } /** * Renders the GameCanvas * * @param myGraphics Graphics */ public void paint(Graphics myGraphics) { /* Sets the current color to white */ myGraphics.setColor(255, 255, 255); /* Fills a rectangle to the maximum size of the canvas */ myGraphics.fillRect(0, 0, this.getWidth(), this.getHeight()); /* Renders the LayerManager's view window */ layerManager.paint(myGraphics, 0, 0); } /** * Creates a new thread for this SpriteCollisionGCanvas instance and start * the thread instance */ public synchronized void start() { thread = new Thread(this); thread.start(); } public void run() { while (true) { if (!manSprite.collidesWith(ballSprite, true)) { manSprite.nextFrame(); manSprite.setPosition(manSprite.getX() + 10, manSprite.getY()); } /* Repaints the canvas */ repaint(); /* Waits for a while, before changing to a new frame */ try { Thread.sleep(500); } catch (InterruptedException err) { System.out.println(err.getMessage()); } } } }
MIDlet to test Sprite Collision (Listing 6.8)
package com.scmadkit.ch06; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * This MIDlet is to test the Sprite collision. * * * @author Ko Ko Naing */ public class SpriteCollisionMIDlet extends MIDlet { private Display myDisplay; private SpriteCollisionGCanvas spriteCollisionGCanvas; /** * Creates a new instance of SpriteCollisionMIDlet */ public SpriteCollisionMIDlet() { /* Gets the Display object of this MIDlet */ myDisplay = Display.getDisplay(this); /* Creates a new instance of SpriteCollisionGCanvas */ spriteCollisionGCanvas = new SpriteCollisionGCanvas(); } /* Signals that the MIDlet has entered the Active state */ protected void startApp() { /* Starts the GameCanvas thread */ spriteCollisionGCanvas.start(); /* Sets the GameCanvas as the current display */ myDisplay.setCurrent(spriteCollisionGCanvas); } protected void destroyApp(boolean unconditional) { } protected void pauseApp() { } }
