Canvas for processing game actions


<strong>/*————————————————–</strong>
<strong>* GameActions.java</strong>
<strong>*</strong>
<strong>* Canvas for processing game actions</strong>
<strong>*</strong>
<strong>* Example from the book: Core J2ME Technology</strong>
<strong>* Copyright John W. Muchow http://www.CoreJ2ME.com</strong&gt;
<strong>* You may use/modify for any non-commercial purpose</strong>
<strong>*————————————————-*/ </strong>
<strong>import javax.microedition.midlet.*;</strong>
<strong>import javax.microedition.lcdui.*;</strong>

<strong>public class GameActions extends MIDlet</strong>
<strong>{</strong>
<strong> private Display display; // The display</strong>
<strong> private GameActionCanvas canvas; // Canvas</strong>

<strong>public GameActions()</strong>
<strong> {</strong>
<strong> display = Display.getDisplay(this);</strong>
<strong> canvas = new GameActionCanvas(this);</strong>
<strong> }</strong>

<strong>protected void startApp()</strong>
<strong> {</strong>
<strong> display.setCurrent( canvas );</strong>
<strong> }</strong>

<strong>protected void pauseApp()</strong>
<strong> { }</strong>

<strong>protected void destroyApp( boolean unconditional )</strong>
<strong> { }</strong>

<strong>public void exitMIDlet()</strong>
<strong> {</strong>
<strong> destroyApp(true);</strong>
<strong> notifyDestroyed();</strong>
<strong> }</strong>
<strong>}</strong>

<strong>/*————————————————–</strong>
<strong>* GameActionCanvas.java</strong>
<strong>*</strong>
<strong>* Game action event handling</strong>
<strong>*————————————————-*/</strong>
<strong>class GameActionCanvas extends Canvas implements CommandListener</strong>
<strong>{</strong>
<strong> private Command cmExit; // Exit midlet</strong>
<strong> private String keyText = null; // Key code text</strong>
<strong> private GameActions midlet;</strong>

<strong>/*————————————————–</strong>
<strong> * Constructor</strong>
<strong> *————————————————-*/</strong>
<strong> public GameActionCanvas(GameActions midlet)</strong>
<strong> {</strong>
<strong> this.midlet = midlet;</strong>

<strong>// Create exit command &amp; listen for events</strong>
<strong> cmExit = new Command(”Exit”, Command.EXIT, 1);</strong>
<strong> addCommand(cmExit);</strong>
<strong> setCommandListener(this);</strong>
<strong> }</strong>

<strong>/*————————————————–</strong>
<strong> * Paint the text representing the key code </strong>
<strong> *————————————————-*/</strong>
<strong> protected void paint(Graphics g)</strong>
<strong> {</strong>
<strong> // Clear the background (to white)</strong>
<strong> g.setColor(255, 255, 255);</strong>
<strong> g.fillRect(0, 0, getWidth(), getHeight());</strong>

<strong>// Set color and draw text</strong>
<strong> if (keyText != null)</strong>
<strong> {</strong>
<strong> // Draw with black pen</strong>
<strong> g.setColor(0, 0, 0);</strong>
<strong> // Center the text</strong>
<strong> g.drawString(keyText, getWidth()/2, getHeight()/2, Graphics.TOP | Graphics.HCENTER);</strong>
<strong> }</strong>
<strong> }</strong>

<strong>/*————————————————–</strong>
<strong> * Command event handling</strong>
<strong> *————————————————-*/ </strong>
<strong> public void commandAction(Command c, Displayable d)</strong>
<strong> {</strong>
<strong> if (c == cmExit)</strong>
<strong> midlet.exitMIDlet();</strong>
<strong> }</strong>

<strong>/*————————————————–</strong>
<strong> * Game action event handling</strong>
<strong> * A game action will be converted into a key code </strong>
<strong> * and handed off to this method</strong>
<strong> *————————————————-*/ </strong>
<strong> protected void keyPressed(int keyCode)</strong>
<strong> {</strong>
<strong> switch (getGameAction(keyCode))</strong>
<strong> {</strong>
<strong> // Place logic of each action inside the case</strong>
<strong> case FIRE:</strong>
<strong> case UP: </strong>
<strong> case DOWN:</strong>
<strong> case LEFT:</strong>
<strong> case RIGHT:</strong>
<strong> case GAME_A:</strong>
<strong> case GAME_B:</strong>
<strong> case GAME_C:</strong>
<strong> case GAME_D:</strong>
<strong> default:</strong>
<strong> // Print the text of the game action</strong>
<strong> keyText = getKeyName(keyCode);</strong>
<strong> } </strong>
<strong> repaint();</strong>
<strong> }</strong>
<strong>}</strong>

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

Zoemalang's community

ujung malang adalah Sebuah desa yang hilang terganti dengan ujung harapan

YoYo Games Blog Feed

Ujung malang adalah Sebuah desa yang hilang terganti dengan ujung harapan

Dealer Termurah

Menjual Segala Jenis Motor Baru Di Indonesia Cash Maupun Kredit

%d blogger menyukai ini: