SlideShare a Scribd company logo
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

More Related Content

PDF
2Bytesprog2 course_2014_c9_graph
PDF
Enhancing UI/UX using Java animations
PDF
Intro to HTML5 Canvas
PDF
14multithreaded Graphics
PDF
Building a Visualization Language
PPTX
HTML5 Animation in Mobile Web Games
DOCX
Computer Graphics Practical
PPTX
Drawing with the HTML5 Canvas
2Bytesprog2 course_2014_c9_graph
Enhancing UI/UX using Java animations
Intro to HTML5 Canvas
14multithreaded Graphics
Building a Visualization Language
HTML5 Animation in Mobile Web Games
Computer Graphics Practical
Drawing with the HTML5 Canvas

What's hot (20)

PPT
Mobile Game and Application with J2ME - Collision Detection
PPT
Mobile Game and Application with J2ME
PDF
Computer graphics practical(jainam)
DOCX
Computer graphics
PPTX
Introduction to graphics programming in c
PDF
C Graphics Functions
DOCX
Computer graphics
PPTX
CSS3 2D/3D transform
PDF
OpenGL L06-Performance
KEY
2011/11/20 modul8 workshop at Apple Store Ginza
KEY
Exploring Canvas
PDF
Cg lab cse-v (1) (1)
PPT
Lecture on graphics
PDF
Tutorial flash
PPTX
PPTX
Graphics in C programming
PDF
Html5 canvas
KEY
Stupid Canvas Tricks
PPT
HTML5 Canvas
PPTX
HTML 5_Canvas
Mobile Game and Application with J2ME - Collision Detection
Mobile Game and Application with J2ME
Computer graphics practical(jainam)
Computer graphics
Introduction to graphics programming in c
C Graphics Functions
Computer graphics
CSS3 2D/3D transform
OpenGL L06-Performance
2011/11/20 modul8 workshop at Apple Store Ginza
Exploring Canvas
Cg lab cse-v (1) (1)
Lecture on graphics
Tutorial flash
Graphics in C programming
Html5 canvas
Stupid Canvas Tricks
HTML5 Canvas
HTML 5_Canvas
Ad

Viewers also liked (6)

PPT
MIDP: Form Custom and Image Items
PDF
iOS Selectors Blocks and Delegation
PDF
Intro to Dynamic Web Pages
PDF
Introduction to XML, XHTML and CSS
PPT
Intro To Programming Concepts
PPT
Java Arrays
MIDP: Form Custom and Image Items
iOS Selectors Blocks and Delegation
Intro to Dynamic Web Pages
Introduction to XML, XHTML and CSS
Intro To Programming Concepts
Java Arrays
Ad

Similar to MIDP: Game API (20)

PDF
Creating Games for Asha - platform
PPTX
Java ME - 05 - Game API
PPTX
Game Development for Nokia Asha Devices with Java ME #2
PPT
Scmad Chapter07
PDF
Introduction to 2D Game Development on Nokia Series 40 Asha Phones
PPT
Advanced Game Development with the Mobile 3D Graphics API
PDF
Developing games for Series 40 full-touch UI
PPTX
Developing Multi Platform Games using PlayN and TriplePlay Framework
PDF
libGDX: User Input and Frame by Frame Animation
PPTX
Game development with_lib_gdx
PDF
Implementing a Simple Game using libGDX
PPTX
Java ME - 03 - Low Level Graphics E
DOC
Java applet handouts
PPTX
Silverlight as a Gaming Platform
PPT
Java4-Graphics.ppt
PDF
Getting Started with 3D Game Development on Nokia Series 40 Asha Phones
PDF
Games 3 dl4-example
PDF
Tools for developing Android Games
DOCX
Work flow the pain is reel
Creating Games for Asha - platform
Java ME - 05 - Game API
Game Development for Nokia Asha Devices with Java ME #2
Scmad Chapter07
Introduction to 2D Game Development on Nokia Series 40 Asha Phones
Advanced Game Development with the Mobile 3D Graphics API
Developing games for Series 40 full-touch UI
Developing Multi Platform Games using PlayN and TriplePlay Framework
libGDX: User Input and Frame by Frame Animation
Game development with_lib_gdx
Implementing a Simple Game using libGDX
Java ME - 03 - Low Level Graphics E
Java applet handouts
Silverlight as a Gaming Platform
Java4-Graphics.ppt
Getting Started with 3D Game Development on Nokia Series 40 Asha Phones
Games 3 dl4-example
Tools for developing Android Games
Work flow the pain is reel

More from Jussi Pohjolainen (20)

PDF
Moved to Speakerdeck
PDF
Java Web Services
PDF
Box2D and libGDX
PDF
libGDX: Screens, Fonts and Preferences
PDF
libGDX: Tiled Maps
PDF
Intro to Building Android Games using libGDX
PDF
Advanced JavaScript Development
PDF
Introduction to JavaScript
PDF
Introduction to AngularJS
PDF
libGDX: Scene2D
PDF
libGDX: Simple Frame Animation
PDF
libGDX: Simple Frame Animation
PDF
libGDX: User Input
PDF
Building Android games using LibGDX
PDF
Android Threading
PDF
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
PDF
Intro to Asha UI
PDF
Intro to Java ME and Asha Platform
PDF
Intro to PhoneGap
PDF
Quick Intro to JQuery and JQuery Mobile
Moved to Speakerdeck
Java Web Services
Box2D and libGDX
libGDX: Screens, Fonts and Preferences
libGDX: Tiled Maps
Intro to Building Android Games using libGDX
Advanced JavaScript Development
Introduction to JavaScript
Introduction to AngularJS
libGDX: Scene2D
libGDX: Simple Frame Animation
libGDX: Simple Frame Animation
libGDX: User Input
Building Android games using LibGDX
Android Threading
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Intro to Asha UI
Intro to Java ME and Asha Platform
Intro to PhoneGap
Quick Intro to JQuery and JQuery Mobile

Recently uploaded (20)

PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Cell Structure & Organelles in detailed.
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Insiders guide to clinical Medicine.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Classroom Observation Tools for Teachers
PPTX
master seminar digital applications in india
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Cell Types and Its function , kingdom of life
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Microbial diseases, their pathogenesis and prophylaxis
Cell Structure & Organelles in detailed.
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Insiders guide to clinical Medicine.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Classroom Observation Tools for Teachers
master seminar digital applications in india
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
O7-L3 Supply Chain Operations - ICLT Program
PPH.pptx obstetrics and gynecology in nursing
Cell Types and Its function , kingdom of life
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Pharma ospi slides which help in ospi learning
Complications of Minimal Access Surgery at WLH
Final Presentation General Medicine 03-08-2024.pptx
TR - Agricultural Crops Production NC III.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES

MIDP: Game API

  • 1. MIDP: Game API Jussi Pohjolainen TAMK University of Applied Sciences
  • 2. 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.*;
  • 3. 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
  • 4. Example of GameCanvas Usage class MyCanvas extends GameCanvas { public void anymethod(){ Graphics g = getGraphics(); // some drawing flushGraphics() } }
  • 5. 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
  • 6. GameCanvas Usage class MyCanvas extends GameCanvas { public MyCanvas() { // Let's use Game Canvas event handling! super(true); } public void anymethod() { // drawing.. } }
  • 7. 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
  • 8. 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... } } }
  • 9. 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
  • 10. 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(..)
  • 11. Class Diagram {abstract} Layer int getX() int getY() int getWidth() int getHeight() void setPosition(..) move(..) Sprite TiledLayer
  • 12. 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(..)
  • 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: setViewWindow public void setViewWindow(int x, int y, int width, int height) What part of a big picture is shown on the screen:
  • 15. Sprite - class Sprite classes constructors: public Sprite(Image i) public Sprite(Image i, int framewidth, int frameheight)
  • 16. 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();
  • 17. 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
  • 18. Example Sprite x = new Sprite(image, 540/18, 30); layermanager.append(x); . . x.nextFrame();
  • 19. With Threads.. public void run() { while(true){ int ks = getKeyStates(); if((ks & RIGHT_PRESSED) != 0){ mysprite.move(3,0); mysprite.nextFrame(); } } }
  • 20. Influencing frames Changing sequence int sequence [] = {0, 15, 17}; mysprite.setFrameSequence(sequence) Jumping to another frame mysprite.setFrame(10);
  • 21. 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)
  • 23. Reference Pixel Reference pixel mysprite.defineReferencePixel(15,15);
  • 24. 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"
  • 25. Example Sprite x = new Sprite(...); Sprite y = new Sprite(...); if(x.collidesWith(y, true)){ // CRASH! }
  • 26. 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);
  • 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