MIDP: Game API Jussi Pohjolainen TAMK University of Applied Sciences
Game API It is easy to handle animation and graphics with the Game API All the classes can be found from javax.microedition.lcdui.game.*;
GameCanvas Using the traditional Canvas -class You inherit the Canvas and override paint-method. repaint() Event handling is done by using methods like keypressed, keyreleased Using the GameCanvas -class You inherit the GameCanvas-class There is no need for paint-method, you can draw anywhere! flushGraphics() Two ways of doing event handling
Example of GameCanvas Usage class MyCanvas extends GameCanvas { public void anymethod(){ Graphics g = getGraphics(); // some drawing flushGraphics() } }
Handling Events Constructor of GameCanvas protected GameCanvas(boolean suppressKeyEvents) You have to call this constructor in you own GameCanvas class.. => You have to give a boolean value.. true : use only GameCanvases own event handling false : in addition to GameCanvases own event handling use Canvases event handling
GameCanvas Usage class MyCanvas extends GameCanvas { public MyCanvas() { // Let's use Game Canvas event handling! super(true); } public void anymethod() { // drawing.. } }
Event Handling You can ask which button is pressed (GameCanvas Event Handling) public int getKeyStates() Bit-finals of GameCanvas UP_PRESSED, DOWN_PRESSED, LEFT_PRESSED, RIGHT_PRESSED, FIRE_PRESSED, GAME_A_PRESSED, GAME_B_PRESSED, GAME_C_PRESSED, GAME_D_PRESSED
GameCanvas Example 3 class MyCanvas extends GameCanvas implements Runnable { public MyCanvas() { super(true); (new Thread(this).start()); } public void run() { while(true) { int ks = getKeyStates(); if ((ks & UP_PRESSED) != 0) moveUp(); else if((ks & DOWN_PRESSED) != 0) moveDown(); // Drawing... } } }
Layers You can use layers with the Game canvas. For example: background-layer (bottom) car-layer (front of the background) And when the user clicks right-command, the car layer is moved one pixel to the right (animation!) javax.microedition.lcdui.game.Layer
Layer-class Layer class is abstract and it has two concrete subclasses: 1) TiledLayer , 2) Sprite Layer's methods int getX() int getY() int getWidth() int getHeight() void setPosition(..) move(..)
Class Diagram {abstract} Layer int getX() int getY() int getWidth() int getHeight() void setPosition(..) move(..) Sprite TiledLayer
Mastering the layers Every layer (Sprite or TiledLayer) is put into a LayerManager . The LayerManager is eventually drawn to the screen. LayerManager's methods append(Layer l) insert(Layer l, int i) Layer getLayer(int i) paint(..)
Class Diagram * {abstract} Layer int getX() int getY() int getWidth() int getHeight() void setPosition(..) move(..) Sprite TiledLayer LayerManager append(Layer l) insert(Layer l, int i) Layer getLayer(int i) paint(..)
LayerManager: setViewWindow public void setViewWindow(int x, int y, int width, int height) What part of a big picture is shown on the screen:
Sprite - class Sprite classes constructors: public Sprite(Image i) public Sprite(Image i, int framewidth, int frameheight)
Example of Using Sprite and LayoutManager LayerManager l = new LayerManager(); Sprite s = new Sprite(myimage); s.setPosition(50,50); l.append(s); Graphics g = getGraphics(); l.paint(g,0,0); flushGraphics();
Sprite animation Make one image-file, which contains all the frames In the Sprite's constructor you define one frame's height and width After that you can use Sprite's nextFrame() method
Example Sprite x = new Sprite(image, 540/18, 30); layermanager.append(x); . . x.nextFrame();
With Threads.. public void run() { while(true){ int ks = getKeyStates(); if((ks & RIGHT_PRESSED) != 0){ mysprite.move(3,0); mysprite.nextFrame(); } } }
Influencing frames Changing sequence int sequence [] = {0, 15, 17}; mysprite.setFrameSequence(sequence) Jumping to another frame mysprite.setFrame(10);
Transformation It is possible to transform the sprite public void setTransform(int transform) Finals TRANS_NONE TRANS_ROT90 TRANS_MIRROR .. see the api In practice mysprite.setTransform(Sprite.TRANS_MIRROR)
Reference Pixel Reference pixel
Reference Pixel Reference pixel mysprite.defineReferencePixel(15,15);
Collisions Sprite collidesWith(Sprite s, boolean pixelLevel) collidesWith(TiledLayer s, boolean pixelLevel) collidesWith(Image s, int x, int y, boolean pixelLevel) PixelLevel The rect of the sprite or the "real pixels"
Example Sprite x = new Sprite(...); Sprite y = new Sprite(...); if(x.collidesWith(y, true)){ // CRASH! }
TiledLayer A TiledLayer is a visual element composed of a grid of cells that can be filled with a set of tile images. Rows and Columns TiledLayer(int columns, int rows, Image i, int tileWidth, int tileHeight);
Example TiledLayer a = new TiledLayer(4,2, picture, 32, 32); a.setCell(0,1,1); a.setCell(1,1,1), a.setCell(2,1,1); a.setCell(3,1,1); a.setCell(1,0,2); a.setCell(2,0,3); 0 1 2 3 0 1 1 2 3

MIDP: Game API

  • 1.
    MIDP: Game APIJussi Pohjolainen TAMK University of Applied Sciences
  • 2.
    Game API Itis easy to handle animation and graphics with the Game API All the classes can be found from javax.microedition.lcdui.game.*;
  • 3.
    GameCanvas Using thetraditional Canvas -class You inherit the Canvas and override paint-method. repaint() Event handling is done by using methods like keypressed, keyreleased Using the GameCanvas -class You inherit the GameCanvas-class There is no need for paint-method, you can draw anywhere! flushGraphics() Two ways of doing event handling
  • 4.
    Example of GameCanvasUsage class MyCanvas extends GameCanvas { public void anymethod(){ Graphics g = getGraphics(); // some drawing flushGraphics() } }
  • 5.
    Handling Events Constructorof GameCanvas protected GameCanvas(boolean suppressKeyEvents) You have to call this constructor in you own GameCanvas class.. => You have to give a boolean value.. true : use only GameCanvases own event handling false : in addition to GameCanvases own event handling use Canvases event handling
  • 6.
    GameCanvas Usage classMyCanvas extends GameCanvas { public MyCanvas() { // Let's use Game Canvas event handling! super(true); } public void anymethod() { // drawing.. } }
  • 7.
    Event Handling Youcan ask which button is pressed (GameCanvas Event Handling) public int getKeyStates() Bit-finals of GameCanvas UP_PRESSED, DOWN_PRESSED, LEFT_PRESSED, RIGHT_PRESSED, FIRE_PRESSED, GAME_A_PRESSED, GAME_B_PRESSED, GAME_C_PRESSED, GAME_D_PRESSED
  • 8.
    GameCanvas Example 3class MyCanvas extends GameCanvas implements Runnable { public MyCanvas() { super(true); (new Thread(this).start()); } public void run() { while(true) { int ks = getKeyStates(); if ((ks & UP_PRESSED) != 0) moveUp(); else if((ks & DOWN_PRESSED) != 0) moveDown(); // Drawing... } } }
  • 9.
    Layers You canuse layers with the Game canvas. For example: background-layer (bottom) car-layer (front of the background) And when the user clicks right-command, the car layer is moved one pixel to the right (animation!) javax.microedition.lcdui.game.Layer
  • 10.
    Layer-class Layer classis abstract and it has two concrete subclasses: 1) TiledLayer , 2) Sprite Layer's methods int getX() int getY() int getWidth() int getHeight() void setPosition(..) move(..)
  • 11.
    Class Diagram {abstract} Layerint getX() int getY() int getWidth() int getHeight() void setPosition(..) move(..) Sprite TiledLayer
  • 12.
    Mastering the layersEvery layer (Sprite or TiledLayer) is put into a LayerManager . The LayerManager is eventually drawn to the screen. LayerManager's methods append(Layer l) insert(Layer l, int i) Layer getLayer(int i) paint(..)
  • 13.
    Class Diagram *{abstract} Layer int getX() int getY() int getWidth() int getHeight() void setPosition(..) move(..) Sprite TiledLayer LayerManager append(Layer l) insert(Layer l, int i) Layer getLayer(int i) paint(..)
  • 14.
    LayerManager: setViewWindowpublic void setViewWindow(int x, int y, int width, int height) What part of a big picture is shown on the screen:
  • 15.
    Sprite - classSprite classes constructors: public Sprite(Image i) public Sprite(Image i, int framewidth, int frameheight)
  • 16.
    Example of UsingSprite and LayoutManager LayerManager l = new LayerManager(); Sprite s = new Sprite(myimage); s.setPosition(50,50); l.append(s); Graphics g = getGraphics(); l.paint(g,0,0); flushGraphics();
  • 17.
    Sprite animation Makeone image-file, which contains all the frames In the Sprite's constructor you define one frame's height and width After that you can use Sprite's nextFrame() method
  • 18.
    Example Sprite x= new Sprite(image, 540/18, 30); layermanager.append(x); . . x.nextFrame();
  • 19.
    With Threads.. publicvoid run() { while(true){ int ks = getKeyStates(); if((ks & RIGHT_PRESSED) != 0){ mysprite.move(3,0); mysprite.nextFrame(); } } }
  • 20.
    Influencing frames Changingsequence int sequence [] = {0, 15, 17}; mysprite.setFrameSequence(sequence) Jumping to another frame mysprite.setFrame(10);
  • 21.
    Transformation It ispossible to transform the sprite public void setTransform(int transform) Finals TRANS_NONE TRANS_ROT90 TRANS_MIRROR .. see the api In practice mysprite.setTransform(Sprite.TRANS_MIRROR)
  • 22.
  • 23.
    Reference Pixel Referencepixel mysprite.defineReferencePixel(15,15);
  • 24.
    Collisions Sprite collidesWith(Sprites, boolean pixelLevel) collidesWith(TiledLayer s, boolean pixelLevel) collidesWith(Image s, int x, int y, boolean pixelLevel) PixelLevel The rect of the sprite or the "real pixels"
  • 25.
    Example Sprite x= new Sprite(...); Sprite y = new Sprite(...); if(x.collidesWith(y, true)){ // CRASH! }
  • 26.
    TiledLayer A TiledLayeris a visual element composed of a grid of cells that can be filled with a set of tile images. Rows and Columns TiledLayer(int columns, int rows, Image i, int tileWidth, int tileHeight);
  • 27.
    Example TiledLayer a= new TiledLayer(4,2, picture, 32, 32); a.setCell(0,1,1); a.setCell(1,1,1), a.setCell(2,1,1); a.setCell(3,1,1); a.setCell(1,0,2); a.setCell(2,0,3); 0 1 2 3 0 1 1 2 3