FileDocCategorySizeDatePackage
GameCanvasLFImpl.javaAPI DocphoneME MR2 API (J2ME)6307Wed May 02 18:00:24 BST 2007com.sun.midp.lcdui

GameCanvasLFImpl

public class GameCanvasLFImpl extends Object
This is the look & feel implementation for GameCanvas.

Fields Summary
javax.microedition.lcdui.game.GameCanvas
owner
The owner of this view.
private javax.microedition.lcdui.Image
offscreenBuffer
Currently every GameCanvas has one offscreen buffer Can be optimized so that we put a limit on number of offscreen buffers an application can have
private GraphicsAccess
graphicsAccess
Cached reference to GraphicsAccess instance
Constructors Summary
public GameCanvasLFImpl(javax.microedition.lcdui.game.GameCanvas c)
Create new implementation instance for the given GameCanvas

param
c GameCanvas instance to create the implementation for

        owner = c;
        graphicsAccess = GameMap.getGraphicsAccess();


        /* IMPL_NOTE: The initial off-screen buffer has the same width
         *   and height as the entire screen. Further resizing will not
         *   cause memory reallocation until new geometry is bigger than
         *   the current one. Screen rotation is one of the cases the
         *   reallocation is needed.
         *
         *   User can override the methods getWidth() and getHeight() of
         *   GameCanvas, so they should not be used for off-screen buffer
         *   initial allocation.
         */
        offscreenBuffer = Image.createImage(
            graphicsAccess.getScreenWidth(),
            graphicsAccess.getScreenHeight());
    
Methods Summary
public voiddrawBuffer(javax.microedition.lcdui.Graphics g)
Render the off-screen buffer content to the Graphics object

param
g the Graphics object to render off-screen buffer content

        // NullPointerException will be thrown in drawImage if g == null
       if (offscreenBuffer != null) {
            g.drawImage(offscreenBuffer, 0, 0,
                Graphics.TOP | Graphics.LEFT);
       }
    
public voidflushGraphics()
Flushes the off-screen buffer to the display. The size of the flushed area is equal to the size of the GameCanvas.

        DisplayAccess displayAccess = GameMap.getDisplayAccess(owner);
        if (displayAccess != null && offscreenBuffer != null) {
	        displayAccess.flush(owner, offscreenBuffer,
			      0, 0, owner.getWidth(), owner.getHeight());
        }
    
public voidflushGraphics(int x, int y, int width, int height)
Flushes the specified region of the off-screen buffer to the display.

param
x the left edge of the region to be flushed
param
y the top edge of the region to be flushed
param
width the width of the region to be flushed
param
height the height of the region to be flushed

        if (width < 1 || height < 1) {
	        return;
	    }

        DisplayAccess displayAccess = GameMap.getDisplayAccess(owner);
        if (displayAccess != null && offscreenBuffer != null) {
            displayAccess.flush(owner, offscreenBuffer,
    			  x, y,	width, height);
        }
    
public javax.microedition.lcdui.GraphicsgetGraphics()
Obtains the Graphics object for rendering a GameCanvas. The returned Graphics object renders to the off-screen buffer belonging to this GameCanvas. IMPL_NOTE: The dimensions of the Graphics object are explicitly set to GameCanvas size, since off-screen buffer larger than GameCanvas can be used, while some JSR clients need to translate the coordinates regarding the GameCanvas size. Anyway if GameCanvas has zero width or height, the Graphics dimensions are set to entire off-screen buffer.

return
the Graphics object that renders to current GameCanvas

        if (offscreenBuffer != null) {
            int w = owner.getWidth();
            int h = owner.getHeight();
            
            Graphics g = ((w <= 0) || (h <= 0)) ?
                offscreenBuffer.getGraphics() :
                graphicsAccess.getImageGraphics(offscreenBuffer, w, h);

            graphicsAccess.setGraphicsCreator(g, owner);
            return g;
        }
        
        return null;
    
public intgetKeyStates()
Gets the states of the physical game keys.

return
An integer containing the key state information (one bit per key), or 0 if the GameCanvas is not currently shown.

        DisplayAccess displayAccess = GameMap.getDisplayAccess(owner);
        if (displayAccess != null) {
            return displayAccess.getKeyMask();
        }
        return 0;
    
public voidlCallSizeChanged(int w, int h)
Handle screen size change event to update internal state of the GameCanvas accordingly

param
w new screen width
param
h new screen height

        // Resize off-screen buffer in the case it is not big enough only
        if (w > offscreenBuffer.getWidth() ||
                h > offscreenBuffer.getHeight()) {

            // OutOfMemoryError can be thrown
            graphicsAccess.resizeImage(
                offscreenBuffer, w, h, true);
        }