Scmad Chapter07


Published on

Chapter 07 - MIDP Util and Game API
This material is only for studies purposes

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Scmad Chapter07

    1. 1. By Marcel Caraciolo Chapter 07– MIDP: Timers and GameCanvas SCMAD Certification 45mm 61mm
    2. 2. Agenda <ul><li>MIDP - Util </li></ul><ul><ul><li>Timers </li></ul></ul><ul><ul><li>User Interface API Classes </li></ul></ul><ul><ul><li>MIDP Classes </li></ul></ul><ul><li>MIDP – Game API </li></ul><ul><ul><li>Game User Interface </li></ul></ul><ul><ul><li>GameCanvas </li></ul></ul><ul><ul><li>Sprite </li></ul></ul><ul><ul><li>Layer </li></ul></ul><ul><ul><li>TiledLayer </li></ul></ul><ul><ul><li>LayerManager </li></ul></ul>
    3. 3. MIDP: java.util.Timer <ul><li>Timer: Registers an event to be executed at a specific time </li></ul><ul><li>on a different Thread </li></ul><ul><li>It can be scheduled to run: </li></ul><ul><ul><li>After some time ( schedule(task,delay) ) </li></ul></ul><ul><ul><li>At a specific time ( schedule(task,time) ) </li></ul></ul><ul><ul><li>After some time, then on a regular interval ( schedule(task,delay,period) ) </li></ul></ul><ul><ul><li>At a specific time, then on a regular interval ( schedule(task,firstTime,period) ) </li></ul></ul><ul><li>Every timer has a single Thread for all its executions, so if </li></ul><ul><li>an event execution time is greater than the interval time, </li></ul><ul><li>then the next execution is impacted </li></ul>
    4. 4. MIDP: java.util.Timer <ul><li>It may be canceled with cancel() </li></ul><ul><li>Execution can be: </li></ul><ul><ul><li>Fixed-delay: Scheduled with schedule(). The ‘n+1’ run is done “period” milliseconds after the beginning of “n” execution (delay on the beginning of an execution influences all the next, and the frequency is changed). Useful for animations and real-time user interaction. </li></ul></ul><ul><ul><li>Fixed-rate: Scheduled with scheduleAtFixedRate(). Executed is done according to the first one. A delay will make the next events run on a greater schedule to “catch up”. </li></ul></ul>
    5. 5. MIDP: java.util.TimerTask <ul><li>Task to be run by a Timer (similar to Thread’s Runnable) </li></ul><ul><li>You must code the run() abstract method </li></ul><ul><li>May call cancel() to stop its own timer (will cancel after the following executions, does not influence on its own execution). </li></ul>
    6. 6. Timer and TimerTask Example
    7. 7. Timer and TimerTask Example
    8. 8. MIDP: Font <ul><li>Represents fonts and acts as a factory </li></ul><ul><li>Static methods: </li></ul><ul><ul><li>getFont(specifierFont): Returns a font based on the specifier parameter : FONT_INPUT_TEXT (font for input labels) or FONT_STATIC_TEXT (font for labels) </li></ul></ul><ul><ul><li>getDefaultFont (): Default font system </li></ul></ul><ul><ul><li>getFont(face,style,size) : factory method: </li></ul></ul><ul><ul><ul><li>face: FACE_SYSTEM, FACE_MONOSPACE, </li></ul></ul></ul><ul><ul><ul><li>FACE_PROPORTIONAL </li></ul></ul></ul><ul><ul><ul><li>style: STYLE_PLAIN, STYLE_BOLD, STYLE_ITALIC, </li></ul></ul></ul><ul><ul><ul><li>STYLE_UNDERLINED </li></ul></ul></ul><ul><ul><ul><li>size: SIZE_SMALL, SIZE_MEDIUM, SIZE_LARGE </li></ul></ul></ul>
    9. 9. MIDP: Font <ul><li>Instance methods: </li></ul><ul><ul><li>getHeight(): Height of the font </li></ul></ul><ul><ul><li>getBaselinePosition(): Distance between top and </li></ul></ul><ul><ul><li>baseline </li></ul></ul><ul><ul><li>stringWidth(string): Width of the specifier string </li></ul></ul>
    10. 10. MIDP: Image <ul><li>Represents an image and works as a factory: </li></ul><ul><ul><li>Images are either mutable or immutable, depending on how they were created. Immutable images are usually loaded from binary resources. Mutable images are created black and drawn programmatically. If a mutable image is added to a resource (e.g. an alert or an image item) , a “Snapshot” from the image at this moment is used. </li></ul></ul><ul><ul><li>Mutable images do not support transparency </li></ul></ul><ul><ul><li>PNG standard and its transparency must be supported. Additional formats may be supported. </li></ul></ul><ul><ul><li>You can perform a programmatic double-buffering by creating a mutable image and painting to it first and later painting this image to a canvas. </li></ul></ul>
    11. 11. MIDP: Image <ul><li>Static methods: </li></ul><ul><ul><li>createImage(width,height): Creates a mutable image to be drawn </li></ul></ul><ul><ul><li>by the application. </li></ul></ul><ul><ul><li>createImage(Image): Creates a immutable image from any other </li></ul></ul><ul><ul><li>createImage(name): Creates an image from a specified resource (using getResourceAsStream to resolve the name) </li></ul></ul><ul><ul><li>createImage(byte[]image_data,imageOffset,imageLength): </li></ul></ul><ul><ul><li>Creates an image from the byte array. </li></ul></ul>
    12. 12. MIDP: Image <ul><li>Static methods: </li></ul><ul><ul><li>createImage(image,x,y,w,h,transform): Creates an image by “framing” the original and then applying the transformations. Transformations are defined by Sprite.TRANS_* (GAME_API) </li></ul></ul><ul><ul><li>createImage(inputStream): Creates an image from the stream </li></ul></ul><ul><ul><li>createRGBImage(rgb[], w, h, processAlpha): Creates an image from the int array on the 0xAARRGGBB format ( AA: transparency) </li></ul></ul>
    13. 13. Notes about classes and methods <ul><li>Static methods: </li></ul><ul><ul><li>Class.getResourceAsStream() may be used to load resources on the jar file, as long these resources are not Java classes. </li></ul></ul><ul><ul><li>Calling System.exit() throws a SecurityException since the MIDlet life cycle is controlled by the device. Termination requests from the application are made through MIDlet.notifyDestroyed() </li></ul></ul><ul><li>Instance methods: </li></ul><ul><ul><li>display.callSerially() executes a code on the user interface thread. </li></ul></ul><ul><ul><li>It’s really used since UI operations are thread-safe </li></ul></ul><ul><ul><li>display.getBestImageWidth/Height(imageType): Calculates the size of images shown on LIST_ELEMENT(list) , CHOICE_GROUP_ELEMENT (choice) and ALERT ( alert) </li></ul></ul>
    14. 14. Notes about classes and methods <ul><li>Instance methods: </li></ul><ul><ul><li>display.getBorderStyle(highlighted): Returns the border style ( SOLID/DOTTED) used by the widgets when the element is selected or not. </li></ul></ul><ul><ul><li>display.getColor(colorSpecifier): Return the color of some part of a component on the platform. You use the following specifiers: </li></ul></ul><ul><ul><ul><li>COLOR_BACKGROUND, COLOR_FOREGROUND </li></ul></ul></ul><ul><ul><ul><li>COLOR_HIGHLIGHTED_BACKGROUND </li></ul></ul></ul><ul><ul><ul><li>COLOR_HIGHLIGHTED_FOREGROUND </li></ul></ul></ul><ul><ul><ul><li>COLOR_BORDER, COLOR_HIGHLIGHTED_BORDER </li></ul></ul></ul>
    15. 15. Notes about classes and methods <ul><li>Instance methods: </li></ul><ul><ul><li>display.numAlphaLevels(): Number of transparency levels supported. Devices shall support at least two levels (totally transparent and totally opaque) </li></ul></ul><ul><li>Exceptions and most exceptions have only two </li></ul><ul><li>constructors: A non-args constructor and one with string </li></ul><ul><li>(message) parameter </li></ul><ul><li>Exceptions are not “nested” </li></ul>
    16. 16. Game User Interface <ul><li>Based on low level API. All features from low-level API are present on Game API </li></ul><ul><li>Classes at package </li></ul>
    17. 17. GameCanvas <ul><li>Extends Canvas. It has the same features with additional features to create real-time games </li></ul><ul><li>Regular Canvas is based on user-interface events. </li></ul><ul><li>GameCanvas is based on the game loop: </li></ul><ul><ul><li>Check inputs </li></ul></ul><ul><ul><li>Execute logic according to inputs and game state </li></ul></ul><ul><ul><li>Render outputs </li></ul></ul>
    18. 18. GameCanvas <ul><li>GameLoop is expected to execute at a constant rate, and does not depend on user input. </li></ul><ul><li>Differently from the regular canvas, a GameCanvas has its own buffer. Its Graphics may be used at any moment, not only inside paint() method. </li></ul><ul><li>Defines no abstract methods, but defines the “(boolean suppressKeyEvents)” constructor. When suppressKeyEvents is true, keyPressed , keyReleased, and keyRepeated methods are not called. This may increase performance. </li></ul>
    19. 19. GameCanvas: Methods <ul><li>getGraphics(): Return a graphics to paint on this canvas. Operations on this graphics are not shown until flushGraphics is called. A new instance of graphics is created EVERY TIME this method is called, so this call shall not be placed inside the game loop (it shall be called only once, outside the game loop, and reused for every loop). </li></ul><ul><li>flushGraphics() and flushGraphics(x,y,w,h): Sends the offscreen buffer to be displayed. Blocks until the operation is finished. When the parameters are informed, only the defined area is painted. </li></ul>
    20. 20. GameCanvas: Methods <ul><li>getKeyStates(): Returns an integer (bit mask) with all keys that were pressed since the last time the method was called. This method is expected to be called at every loop. Each bit represents a pressed key. To check if it was pressed, you use the following code: </li></ul>
    21. 21. Utility Classes for Graphics
    22. 22. Sprite <ul><li>Game elements (usually game characters) </li></ul><ul><li>Allows showing a sequence of images (to animate the character), transformations (rotating, mirroring), changing the position and collision detection. </li></ul>
    23. 23. Sprite <ul><li>Sprite(image,frameWidth,frameHeight): Creates a Sprite from an image. A single image defines a whole frame sequence. Each frame will have a specified size (all frames have the same size). The image width must be multiple of the frameWidth and the same must be true for the frameHeight. Frames may be organized in vertical, horizontal, or in “grid” ordering. Initially the sprite is visible, at (0,0) position, with default framesequence (0,1,2…). On the attached sample (sprite.png), frames are organized as below (the last position (5) is not used by the game): </li></ul><ul><li>0 1 2 </li></ul><ul><li>3 4 5 </li></ul>
    24. 24. Sprite <ul><li>Sprite(image): Same as calling Sprite (image, image.getWidth(), image.getHeight()). Creates a Sprite with a single frame, with the frame size </li></ul><ul><li>Sprite(sprite): Duplicates the sprite. </li></ul>
    25. 25. Sprite: methods <ul><li>paint(Graphics): Renders the current frame on the informed Graphics based on its position and state. </li></ul><ul><li>defineReferencePixel(x,y): Defines a reference point by which TRANSFORMATIONS will be based on when applied to the frame. This point may be placed outside the area frame’s area. By default it’s (0,0). </li></ul><ul><li>setRefPixel(x,y): Place the frame by its REFERENCE POINT at (x,y). </li></ul>
    26. 26. setRefPixelPosition example
    27. 27. Sprite: methods <ul><li>setFrameSequence(sequence[]): Defines the frame sequence which will be used when animating the sprite. Default is (0,1,2,…). </li></ul><ul><li>nextFrame(), prevFrame(): Switches to the next or previous frame. The frame list is circular (i.e. when nextFrame is called at the last frame the index returns to the first). </li></ul><ul><li>setFrame(sequenceIndex): Set i as current frame. The sequence index, not the frame index shall be used. </li></ul><ul><li>getFrameSequenceLength(): Frame sequence length. </li></ul><ul><li>getRawFrameCount(): Number of frames available on the sprite. </li></ul>
    28. 28. Sprite: setFrame(int sequenceIndex)
    29. 29. Sprite: methods <ul><li>setTransform(transform): Applies a transformation. </li></ul><ul><ul><li>TRANS_NONE : Does nothing </li></ul></ul><ul><ul><li>TRANS_ROT90 : Rotates 90 degrees clockwise </li></ul></ul><ul><ul><li>TRANS_ROT180 : Rotates 180 degrees clockwise </li></ul></ul><ul><ul><li>TRANS_ROT270 : Rotates 270 degrees clockwise </li></ul></ul><ul><ul><li>TRANS_MIRROR : Mirrors on the vertical axis </li></ul></ul><ul><ul><li>TRANS_MIRROR_ROT90: Mirrors and then rotates </li></ul></ul><ul><ul><li>TRANS_MIRROR_ROT180 : Mirrors and then rotates </li></ul></ul><ul><ul><li>TRANS_MIRROR_ROT270: Mirrors and then rotates </li></ul></ul><ul><li>Transformations are based on the reference point. They </li></ul><ul><li>are not acumulative and cannot be combined (e.g. calling </li></ul><ul><li>TRANS_ROT90 twice will not rotate the object with 180 </li></ul><ul><li>degrees, but only with 90). </li></ul>
    30. 30. setTransform Example
    31. 31. Sprite: methods <ul><li>collidesWith(sprite,pixelLevel), </li></ul><ul><li>collidesWith(tiledLayer, pixelLevel), </li></ul><ul><li>collidesWith(image,x,y,pixelLevel) </li></ul><ul><ul><li>Checks a collision between two objects. When pixelLevel is used, the objects will only colide if opaque pixels collide (which is much slower than checking rectangle collisions). Collisions only happen if both objects are visible. A collision can be checked between two sprites, between a sprite and a tiledLayer, and between a sprite and any image at (x,y). </li></ul></ul>
    32. 32. Sprite: methods <ul><li>defineCollisionRectangle(x,y,width,height): </li></ul><ul><ul><li>Defines the rectangle where collisions are considered. By default, it covers the whole frame size. For instance, if you have a jet that leaves some fog, the collision rectangle shall comprehend only the plane itself, and not the fog. Transformations applied to the sprite are also applied to the collision rectangle. </li></ul></ul>
    33. 33. Sprite: Collisions
    34. 34. Layer <ul><li>Sprite extends Layer </li></ul><ul><li>Layer are “objects placed on the game screen”: </li></ul><ul><ul><li>They all have position and size </li></ul></ul><ul><ul><li>Can be hidden </li></ul></ul><ul><ul><li>Can be painted on a graphics </li></ul></ul><ul><li>A Layer’s position is always considered from the object’s upper left corner. For Sprites a reference point is also defined to customize transformations and positioning. </li></ul>
    35. 35. Layer <ul><li>setPosition: Defines the layer position </li></ul><ul><li>move(dx,dy): Moves the position </li></ul><ul><li>getX(), getY(): Gets the position </li></ul><ul><li>getWidth(), getHeight(): Gets the size </li></ul><ul><li>setVisible(visible): Visits objects are rendered and collide. Invisible objects are not rendered and do not collide. </li></ul><ul><li>paint(Graphics): Renders the layer on a graphics, if the layer is visible. </li></ul>
    36. 36. TiledLayer <ul><li>It extends Layer </li></ul><ul><li>Defines a grid where frames are applied (like wall tiles): Useful for maps and backgrounds. </li></ul><ul><li>.Constructor: TiledLayer(columns,rows,image,tileWidth,tileHeight): Creates a grid with (columns x rows) tiles, and define frames with image/tileWidth/tileHeight. The same logic used to define frames for Sprites is used here. Every cell has the same size, which is the same as the tile’s size ( tileWidth, tileHeight). </li></ul>
    37. 37. TiledLayer
    38. 38. TiledLayer <ul><li>After the object is created, all its cells are blank. Tiles are set with setCell () and fillCells () </li></ul><ul><li>Like with Sprites, each frame has an index, but the indexes start at 1, while the Sprites they begin at 0. This is done because cells may be blank (setting 0 as index) and tiles can be animated (using negative indexes). </li></ul><ul><li>A tile may be static or animated. A static cell is set calling setCell with the tile’s index (positive value). Animated tiles are created by calling createAnimateTile , which returns the animated tile index. Indexes start with -1, and the indexes decreases as tiles are created. Updating the animated tile must be done manually by calling setAnimatedTile </li></ul>
    39. 39. TiledLayer: Methods <ul><li>setCell(col,row, tileIndex): Places a tile on a cell. If it’s greater than zero, a static cell is used. If it’s smaller than zero, an animated tile is used. If it’s zero, the cell is left blank. </li></ul><ul><li>fillCells(col,row,numCols,numRows,tileIndex): Similar to setCell, but fills a cell area </li></ul><ul><li>getCellWidth(), getCellHeight(): Gets the tile size </li></ul><ul><li>getColumns(), getRows(): Gets the grid size </li></ul><ul><li>paint(Graphics): Renders the grid on the graphics, on the position inherited from the Layer. </li></ul>
    40. 40. TiledLayer: Methods <ul><li>createAnimatedTile(staticTileIndex): Creates an animated tile and sets its first index to be shown. </li></ul><ul><li>setAnimatedTile(animatedTileIndex,staticTileIndex): Updates the frame on an animated tile previously created </li></ul><ul><li>getAnimatedTile(animatedTileIndex) :Returns which static tile is currently associated with the animated tile </li></ul><ul><li>setStaticTileSet(image,tileWidth, tileHeight): Changes the tile image. Similar to creating a new tiled layer with the same grid size </li></ul>
    41. 41. TiledLayer: Methods
    42. 42. LayerManager <ul><li>Controls a list of layers (sprite and tiled layers). Simplifies the rendering process controlling the sequence of objects that will be rendered. Elements are organized on a list. Smaller indexes are closer to the user, and append() adds to the END of the list. </li></ul><ul><li>Besides the layer list, it has a view window (x,y,w,h). It defines a window over which rendering is applied. This may be used to perform scrolling. (x,y) defines the rendering origin while the width and height define the window size. </li></ul><ul><li>A layer can not be placed twice on the same manager. When a item is re-inserted, it’s first removed and then set to the defined position. </li></ul>
    43. 43. LayerManager: Methods <ul><li>append(layer): Puts an object on the last position (i.e. most far from the user). </li></ul><ul><li>insert(layer,index): Inserts an object at ‘index’ position. The following objects are “pushed back” </li></ul><ul><li>getLayerAt(index): Returns the object at the specified position </li></ul><ul><li>getSize(): Returns the number of layers </li></ul>
    44. 44. LayerManager: Methods <ul><li>remove(layer): Removes a layer </li></ul><ul><li>setViewWindow(x,y,width,height): Defines the region from the “whole scenery” that will be rendered </li></ul><ul><li>paint(graphics,x,y): Renders all screen, on the correct sequence over the graphics. The (x,y) parameters define where the manager will be rendered on the graphics. </li></ul>
    45. 45. LayerManager: Example
    46. 46. Example Codes <ul><li>Some examples and MIDlets samples are available for reference and studying at this link: </li></ul><ul><ul><li> </li></ul></ul><ul><li>The source codes include: </li></ul><ul><ul><li>BGameMIDllet </li></ul></ul><ul><ul><li>GameMIDlet </li></ul></ul><ul><ul><li>LayerManagerMIDlet </li></ul></ul>
    47. 47. Future Work <ul><li>Next Chapter: </li></ul><ul><ul><li>MIDP – RMS </li></ul></ul><ul><ul><ul><li>Record Management System </li></ul></ul></ul><ul><ul><ul><li>Record Store </li></ul></ul></ul><ul><ul><ul><li>RecordListener </li></ul></ul></ul><ul><ul><ul><li>RecordEnumeration </li></ul></ul></ul><ul><ul><ul><li>RecordComparator </li></ul></ul></ul><ul><ul><ul><li>RecordFilter </li></ul></ul></ul><ul><ul><ul><li>Exceptions </li></ul></ul></ul>
    48. 48. References <ul><li>ALVES F. Eduardo. SCMAD Study Guide, </li></ul><ul><li>27/04/2008. </li></ul><ul><li>JAKL Andreas, Java Platform, Micro Edition Part </li></ul><ul><li>01 slides, 12/2007. </li></ul><ul><li>Sun Certification Mobile Application Developer </li></ul><ul><li>Website: []. </li></ul>