mobile gameand application with j2me

29
Mobile Game and Application with J2ME อ.ออออออ ออออ อออออออ (ออ) [email protected] อออออออออออออออออ อออออออออออออออออ ออออออออออออออออออออ http://

Upload: jenchoke-tachagomain

Post on 15-May-2015

1.283 views

Category:

Documents


4 download

DESCRIPTION

Java J2ME Canvas

TRANSCRIPT

Page 1: Mobile Gameand Application with J2ME

Mobile Gameand Application withJ2ME

อ.เจนโชค เตชะโกเมนท์� (เจ)[email protected]สาขาวิ�ชาส��อนฤม�ตคณะสารสนเท์ศศาสตร�มหาวิ�ท์ยาลั�ยมหาสารคามhttp://www.it.msu.ac.th/nmd

Page 2: Mobile Gameand Application with J2ME

Canvas Canvas ม� method ส�าหร�บ handle game

action, key event น��นค�อเราสามารถเข�ยนโปรแกรมให%ร�บร& %การกดป()มบนม�อถ�อได% โดยใช% Canvas

นอกจากน�+น Canvas ย�งสามารถ regist CommandListener ได%เหม�อนก�บ Displayable อ��นๆ

แต. ท์��ส�าค�ญค�อ Canvas เป0น abstract class ด�งน�+น เราต%องสร%าง class อ��นๆให% extends Canvas เช.น

class MyCanvas extends Canvasแลั%วิจากน�+นจ1งสร%าง object MyCanvas เพื่��อน�าไปวิางบน Screen

Page 3: Mobile Gameand Application with J2ME

Canvas

Page 4: Mobile Gameand Application with J2ME

public class HelloCanvasMIDlet extends MIDlet {Display display;HelloCanvas canvas;public HelloCanvasMIDlet() {

canvas = new HelloCanvas();}protected void destroyApp(boolean arg0) {}

protected void pauseApp() {}

protected void startApp() throws MIDletStateChangeException {display = Display.getDisplay(this);display.setCurrent(canvas);canvas.repaint();

}}

class HelloCanvas extends Canvas{protected void paint(Graphics g) {

g.setColor(255, 0, 0); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(255, 255, 255);

g.drawString("HelloCanvasWorld", 5, 5,Graphics.TOP | Graphics.LEFT);}

}

Page 5: Mobile Gameand Application with J2ME

class HelloCanvas extends Canvas{

protected void paint(Graphics g) {

g.setColor(255, 0, 0);

g.fillRect(0, 0, getWidth(), getHeight());

g.setColor(255, 255, 255);

g.drawString("HelloCanvasWorld", 5, 5,

Graphics.TOP | Graphics.LEFT);

}

}

Page 6: Mobile Gameand Application with J2ME

import java.io.IOException;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;public class GameActionExample extends MIDlet{ private Display display; private MyCanvas canvas; public GameActionExample() { display = Display.getDisplay(this); canvas = new MyCanvas (this); } protected void startApp() { display.setCurrent(canvas); } protected void pauseApp() { } protected void destroyApp( boolean unconditional ) { } public void exitMIDlet() { destroyApp(true); notifyDestroyed(); }}

GameActionExample.java

Page 7: Mobile Gameand Application with J2ME

class MyCanvas extends Canvas implements CommandListener{ private Command exit; private String message; private GameActionExample gameActionExample; private int x, y; public MyCanvas (GameActionExample gameActionExample) { x = 5; y = 5; message = "Use Game Keys"; this.gameActionExample = gameActionExample; exit = new Command("Exit", Command.EXIT, 1); addCommand(exit); setCommandListener(this); } protected void paint(Graphics graphics) {

Image img = null;graphics.setColor(255,255,255);

graphics.fillRect(0, 0, getWidth(), getHeight()); graphics.setColor(255, 0, 0); try {

img = Image.createImage("/images/0.png");} catch (IOException e) {

e.printStackTrace();}

graphics.drawImage(img, x, y, Graphics.TOP | Graphics.LEFT); graphics.drawString(message, x + 10, y +50, Graphics.TOP | Graphics.LEFT); }

GameActionExample.java

Page 8: Mobile Gameand Application with J2ME

public void commandAction(Command command, Displayable displayable)

{ if (command == exit) { gameActionExample.exitMIDlet(); } }

Page 9: Mobile Gameand Application with J2ME

protected void keyPressed(int key) { switch ( getGameAction(key) ){ case Canvas.UP: message = "up"; y--; break; case Canvas.DOWN: message = "down"; y++; break; case Canvas.LEFT: message = "left"; x--; break; case Canvas.RIGHT: message = "right"; x++; break; case Canvas.FIRE: message = "FIRE"; break; } repaint(); }}

Page 10: Mobile Gameand Application with J2ME

// A canvas that illustrates rectanglesclass RectanglesCanvas extends Canvas { public void paint(Graphics g) { int width = getWidth(); int height = getHeight();

// Create a white background g.setColor(0xffffff); g.fillRect(0, 0, width, height); // Draw a solid rectangle g.setColor(0); g.drawRect(width/4, 0, width/2, height/4); // Draw a dotted rectangle inside // the solid rectangle. g.setStrokeStyle(Graphics.DOTTED); g.drawRect(width/4 + 4, 4, width/2 - 8, height/4 - 8); // Draw a rounded rectangle g.setStrokeStyle(Graphics.SOLID); g.drawRoundRect(width/4, height/2, width/2, height/4, 16, 8); }}

Page 11: Mobile Gameand Application with J2ME

// A canvas that illustrates filled rectanglesclass RectangleFillsCanvas extends Canvas { public void paint(Graphics g) { int width = getWidth(); int height = getHeight();

// Create a black background g.setColor(0); g.fillRect(0, 0, width, height);

// Draw a filled rectangle with // a dotted rectangle around it g.setStrokeStyle(Graphics.DOTTED); g.setColor(0x00ff00); g.fillRect(width/4, height/4, width/2, height/2); g.setColor(0xffff00); g.drawRect(width/4, height/4, width/2, height/2); }}

Page 12: Mobile Gameand Application with J2ME

// A canvas that illustrates arcsclass ArcsCanvas extends Canvas { public void paint(Graphics g) { int width = getWidth(); int height = getHeight();

// Create a black background g.setColor(0); g.fillRect(0, 0, width, height);

g.setColor(0xffffff); g.drawArc(0, 0, width/2, height/2, 0, 90); g.setStrokeStyle(Graphics.DOTTED); g.setColor(0xffff00); g.drawRect(0, 0, width/2, height/2);

g.setStrokeStyle(Graphics.SOLID); g.setColor(0xffffff); g.drawArc(width/2, 0, width/2, height/2, 0, -90); g.setStrokeStyle(Graphics.DOTTED); g.setColor(0xffff00); g.drawRect(width/2, 0, width/2, height/2); }}

Page 13: Mobile Gameand Application with J2ME

End of AM

Page 14: Mobile Gameand Application with J2ME

การสร�างภาพบน Canvas ให้�เคลื่��อนไห้วด้�วย Thread

ในการเข�ยน application ต.างๆ โดยเฉพื่าะเกมส� Thread (เธรด) จ�ดเป0น Class ท์��ส�าค�ญมากใน

การควิบค(มการเคร��อนไหวิ อย.างอ�สระของ Image/Picture ใน game ให%ตรงก�บท์��เราต%อง

นอกจากน�+ย�งสามารถควิบค(ม input / output แลัะอ��นๆ อ�กด%วิย เก�อบท์�+งหมดของจาวิาเกมส�จะใช%

Class Thread น�+เป0นหลั�กในการ Control ฉะน�+นเราจ�าเป0นต%องท์�าควิามเข%าใจในเร��องของ Thread

อย.างลั1กซึ้1+งเพื่��อจะสามารภน�าไปใช%งานได%อย.างช�านาญ Method ห้ลื่�กๆของ Thread ประกอบด้�วย

Start() เป0นการส��งให% thread เร��มท์�างาน Sleep() เป0นการส��งให%พื่�กงานเป0นจ�งหวิะ Run() เป0นการควิบค(มการท์�างานของ thread

thread [N ] ; เร��องหร�อเหต(การณ�ท์��เก�ดข1+นต.อเน��องก�น

Page 15: Mobile Gameand Application with J2ME

Thread

Page 16: Mobile Gameand Application with J2ME

สร�างภาพ Animation บน Canvasimport javax.microedition.lcdui.*;

import javax.microedition.midlet.MIDlet;

public class SimpleGameMIDlet extends MIDlet implements CommandListener {

private Display mDisplay; private SimpleGameCanvas mCanvas; private Command mExitCommand;

public void startApp() { if (mCanvas == null) { mCanvas = new SimpleGameCanvas(); mCanvas.start(); mExitCommand = new Command("Exit", Command.EXIT, 0); mCanvas.addCommand(mExitCommand); mCanvas.setCommandListener(this); } mDisplay = Display.getDisplay(this); mDisplay.setCurrent(mCanvas); }

SimpleGameMIDlet.java

Page 17: Mobile Gameand Application with J2ME

public void pauseApp() {} public void destroyApp(boolean unconditional) { mCanvas.stop(); } public void commandAction(Command c, Displayable s) { if (c.getCommandType() == Command.EXIT) { destroyApp(true); notifyDestroyed(); } }

public void pauseApp() {} public void destroyApp(boolean unconditional) { mCanvas.stop(); } public void commandAction(Command c, Displayable s) { if (c.getCommandType() == Command.EXIT) { destroyApp(true); notifyDestroyed(); } }

SimpleGameMIDlet.java

Page 18: Mobile Gameand Application with J2ME

SimpleGameCanvas.java

import java.util.Timer;import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;

public class SimpleGameCanvas extends GameCanvas implements Runnable { private volatile boolean mTrucking; private long mFrameDelay; private int mX, mY; int color_r = 255,color_g = 255,color_b = 255; private int mState; Timer timer = new Timer(); Timer tm; Image img;

import java.util.Timer;import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;

public class SimpleGameCanvas extends GameCanvas implements Runnable { private volatile boolean mTrucking; private long mFrameDelay; private int mX, mY; int color_r = 255,color_g = 255,color_b = 255; private int mState; Timer timer = new Timer(); Timer tm; Image img;

Page 19: Mobile Gameand Application with J2ME

public SimpleGameCanvas() { super(true); mX = getWidth() / 2; mY = getHeight() / 2; mState = 0; mFrameDelay = 20; }public void start() { mTrucking = true; Thread t = new Thread(this); t.start(); PrimeThread p = new PrimeThread(143); p.start(); }public void stop() { mTrucking = false; }public void run() { Graphics g = getGraphics(); while (mTrucking == true) { tick(); input(); render(g); try { Thread.sleep(mFrameDelay); } catch (InterruptedException ie) { stop(); } } }

public SimpleGameCanvas() { super(true); mX = getWidth() / 2; mY = getHeight() / 2; mState = 0; mFrameDelay = 20; }public void start() { mTrucking = true; Thread t = new Thread(this); t.start(); PrimeThread p = new PrimeThread(143); p.start(); }public void stop() { mTrucking = false; }public void run() { Graphics g = getGraphics(); while (mTrucking == true) { tick(); input(); render(g); try { Thread.sleep(mFrameDelay); } catch (InterruptedException ie) { stop(); } } }

SimpleGameCanvas.java

Page 20: Mobile Gameand Application with J2ME

private void tick() { mState = (mState + 1) % 20; } private void input() { int keyStates = getKeyStates(); if ((keyStates & LEFT_PRESSED) != 0) mX = Math.max (0, mX - 1); if ((keyStates & RIGHT_PRESSED) != 0) mX = Math.min (getWidth(), mX + 1); if ((keyStates & UP_PRESSED) != 0) mY = Math.max (0, mY - 1); if ((keyStates & DOWN_PRESSED) != 0) mY = Math.min (getHeight(), mY + 1); } private void render(Graphics g) {

/* background color */g.setColor(255,255,255);

g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(0,0,255); /* X point */ g.drawLine(mX, mY, mX - 10 + mState, mY - 10); g.drawLine(mX, mY, mX + 10, mY - 10 + mState); g.drawLine(mX, mY, mX + 10 - mState, mY + 10); g.drawLine(mX, mY, mX - 10, mY + 10 - mState);

flushGraphics(); }

private void tick() { mState = (mState + 1) % 20; } private void input() { int keyStates = getKeyStates(); if ((keyStates & LEFT_PRESSED) != 0) mX = Math.max (0, mX - 1); if ((keyStates & RIGHT_PRESSED) != 0) mX = Math.min (getWidth(), mX + 1); if ((keyStates & UP_PRESSED) != 0) mY = Math.max (0, mY - 1); if ((keyStates & DOWN_PRESSED) != 0) mY = Math.min (getHeight(), mY + 1); } private void render(Graphics g) {

/* background color */g.setColor(255,255,255);

g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(0,0,255); /* X point */ g.drawLine(mX, mY, mX - 10 + mState, mY - 10); g.drawLine(mX, mY, mX + 10, mY - 10 + mState); g.drawLine(mX, mY, mX + 10 - mState, mY + 10); g.drawLine(mX, mY, mX - 10, mY + 10 - mState);

flushGraphics(); }

SimpleGameCanvas.java

Page 21: Mobile Gameand Application with J2ME

class PrimeThread extends Thread { long minPrime; PrimeThread(long minPrime) { this.minPrime = minPrime; } public void run() { while (true) { try { System.out.println(System.currentTimeMillis()); sleep(1000); } catch (InterruptedException e) { } } } }

class PrimeThread extends Thread { long minPrime; PrimeThread(long minPrime) { this.minPrime = minPrime; } public void run() { while (true) { try { System.out.println(System.currentTimeMillis()); sleep(1000); } catch (InterruptedException e) { } } } }

SimpleGameCanvas.java

Page 22: Mobile Gameand Application with J2ME

การทำ�างานของ TimerTaskข%อด�ของการใช% TimerTask ค�อใช%งานง.ายเพื่ราะม�

เมธอดให%ใช%งาน เพื่�ยงส��งให%เร��มต%นท์�างานด%วิยเมธอด run() แลัะหย(ดการท์�างานด%วิยเมธอด cancel()

การท์�างานของ TimerTask ต%องอาศ�ยการควิบค(มผ่.านเมธอด schedule() ของ class Timer อ�กท์�หน1�ง

timer = new Timer(); // ก�าหนด Instance ของ TimerTimerTask task = new TimerTask(Value); // สร%าง class ในส.วินการท์�างานในร&ปแบบของของ

// TimerTask

timer. Schedule(task,0,500); // timer จะควิบค(มการท์�างานของ TimerTask ท์�างานท์(กๆ

0.5 วิ�นาท์�

Page 23: Mobile Gameand Application with J2ME

การทำ�างานของ TimerTaskTime.schedule()Time.schedule() TimerTask.run

(display)

TimerTask.run(display)

Paintrandom line

Paintrandom line

Main Time Task Display

ควิบค(ม ท์�าหน%าท์��ตามค�าส��ง

มี ว!ธี การเข ยน code j2me แบบง$ายด้�งน %คร�บ โด้ยเร!�มีเข ยนจาก

Java Classextends Canvas

Java Classextends Canvas

Java Classextends TimerTask

Java Classextends TimerTask

J2ME MidletClass

J2ME MidletClass

Page 24: Mobile Gameand Application with J2ME

TimerTask

Page 25: Mobile Gameand Application with J2ME

TimerTask

Class TimerDemoextends MIDlet

Class FieldMoverextends TimerTask

Class StarFieldextends Canvas

import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import java.util.*;

Page 26: Mobile Gameand Application with J2ME

import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import java.util.*;

public class TimerDemo extends MIDlet { Display display; StarField field = new StarField(); FieldMover mover = new FieldMover(); Timer timer = new Timer(); public TimerDemo() { display = Display.getDisplay( this ); } protected void destroyApp( boolean unconditional ) { } protected void startApp() { display.setCurrent( field ); timer.schedule( mover, 0, 50 ); } protected void pauseApp() { } public void exit(){ timer.cancel(); // stop scrolling destroyApp( true ); notifyDestroyed(); }

TimerDemo.java

Page 27: Mobile Gameand Application with J2ME

class FieldMover extends TimerTask { public void run() { field.scroll(); }}

class StarField extends Canvas { int height; int width; int[] stars; Random generator = new Random(); boolean painting = false; private Random genColor = new Random(); public StarField(){ height = getHeight(); width = getWidth(); stars = new int[ height ]; for( int i = 0; i < height; ++i ){ stars[i] = -1; } }

TimerDemo.java

Page 28: Mobile Gameand Application with J2ME

public void scroll() { if( painting ) return; for( int i = height-1; i > 0; --i ){ stars[i] = stars[i-1]; } stars[0] = ( generator.nextInt() % ( 3 * width ) ) / 2; if( stars[0] >= width ){ stars[0] = -1; } repaint(); } protected void paint( Graphics g ){ painting = true; int r_color = genColor.nextInt(255); int g_color = genColor.nextInt(255); int b_color = genColor.nextInt(255); g.setColor( 0, 0, 0 ); g.fillRect( 0, 0, width, height ); g.setColor( r_color, g_color, b_color ); for( int y = 0; y < height; ++y ){ int x = stars[y]; if( x == -1 ) continue; g.drawLine( x, y, x, y ); } painting = false; }

TimerDemo.java

Page 29: Mobile Gameand Application with J2ME

protected void keyPressed( int keyCode ){ exit(); } }}

protected void keyPressed( int keyCode ){ exit(); } }}

TimerDemo.java