001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.midp.lcdui;
027:
028: import javax.microedition.lcdui.Image;
029: import javax.microedition.lcdui.Graphics;
030: import javax.microedition.lcdui.game.GameCanvas;
031:
032: /**
033: * This is the look & feel implementation for GameCanvas.
034: */
035: public class GameCanvasLFImpl {
036:
037: /**
038: * The owner of this view.
039: */
040: GameCanvas owner;
041:
042: /**
043: * Currently every GameCanvas has one offscreen buffer
044: * Can be optimized so that we put a limit on number of
045: * offscreen buffers an application can have
046: */
047: private Image offscreenBuffer;
048:
049: /** Cached reference to GraphicsAccess instance */
050: private GraphicsAccess graphicsAccess;
051:
052: /**
053: * Create new implementation instance for the given GameCanvas
054: * @param c GameCanvas instance to create the implementation for
055: */
056: public GameCanvasLFImpl(GameCanvas c) {
057: owner = c;
058: graphicsAccess = GameMap.getGraphicsAccess();
059:
060: /* IMPL_NOTE: The initial off-screen buffer has the same width
061: * and height as the entire screen. Further resizing will not
062: * cause memory reallocation until new geometry is bigger than
063: * the current one. Screen rotation is one of the cases the
064: * reallocation is needed.
065: *
066: * User can override the methods getWidth() and getHeight() of
067: * GameCanvas, so they should not be used for off-screen buffer
068: * initial allocation.
069: */
070: offscreenBuffer = Image.createImage(graphicsAccess
071: .getScreenWidth(), graphicsAccess.getScreenHeight());
072: }
073:
074: /**
075: * Handle screen size change event to update internal
076: * state of the GameCanvas accordingly
077: *
078: * @param w new screen width
079: * @param h new screen height
080: */
081: public void lCallSizeChanged(int w, int h) {
082: // Resize off-screen buffer in the case it is not big enough only
083: if (w > offscreenBuffer.getWidth()
084: || h > offscreenBuffer.getHeight()) {
085:
086: // OutOfMemoryError can be thrown
087: graphicsAccess.resizeImage(offscreenBuffer, w, h, true);
088: }
089: }
090:
091: /**
092: * Obtains the Graphics object for rendering a GameCanvas. The returned
093: * Graphics object renders to the off-screen buffer belonging to this
094: * GameCanvas.
095: *
096: * IMPL_NOTE: The dimensions of the Graphics object are explicitly
097: * set to GameCanvas size, since off-screen buffer larger than
098: * GameCanvas can be used, while some JSR clients need to translate
099: * the coordinates regarding the GameCanvas size.
100: * Anyway if GameCanvas has zero width or height, the Graphics
101: * dimensions are set to entire off-screen buffer.
102: *
103: * @return the Graphics object that renders to current GameCanvas
104: */
105: public Graphics getGraphics() {
106: if (offscreenBuffer != null) {
107: int w = owner.getWidth();
108: int h = owner.getHeight();
109:
110: Graphics g = ((w <= 0) || (h <= 0)) ? offscreenBuffer
111: .getGraphics() : graphicsAccess.getImageGraphics(
112: offscreenBuffer, w, h);
113:
114: graphicsAccess.setGraphicsCreator(g, owner);
115: return g;
116: }
117:
118: return null;
119: }
120:
121: /**
122: * Render the off-screen buffer content to the Graphics object
123: * @param g the Graphics object to render off-screen buffer content
124: */
125: public void drawBuffer(Graphics g) {
126: // NullPointerException will be thrown in drawImage if g == null
127: if (offscreenBuffer != null) {
128: g.drawImage(offscreenBuffer, 0, 0, Graphics.TOP
129: | Graphics.LEFT);
130: }
131: }
132:
133: /**
134: * Flushes the off-screen buffer to the display. The size
135: * of the flushed area is equal to the size of the GameCanvas.
136: */
137: public void flushGraphics() {
138: DisplayAccess displayAccess = GameMap.getDisplayAccess(owner);
139: if (displayAccess != null && offscreenBuffer != null) {
140: displayAccess.flush(owner, offscreenBuffer, 0, 0, owner
141: .getWidth(), owner.getHeight());
142: }
143: }
144:
145: /**
146: * Flushes the specified region of the off-screen buffer to the display.
147: * @param x the left edge of the region to be flushed
148: * @param y the top edge of the region to be flushed
149: * @param width the width of the region to be flushed
150: * @param height the height of the region to be flushed
151: */
152: public void flushGraphics(int x, int y, int width, int height) {
153: if (width < 1 || height < 1) {
154: return;
155: }
156:
157: DisplayAccess displayAccess = GameMap.getDisplayAccess(owner);
158: if (displayAccess != null && offscreenBuffer != null) {
159: displayAccess.flush(owner, offscreenBuffer, x, y, width,
160: height);
161: }
162: }
163:
164: /**
165: * Gets the states of the physical game keys.
166: * @return An integer containing the key state information (one bit per
167: * key), or 0 if the GameCanvas is not currently shown.
168: */
169: public int getKeyStates() {
170: DisplayAccess displayAccess = GameMap.getDisplayAccess(owner);
171: if (displayAccess != null) {
172: return displayAccess.getKeyMask();
173: }
174: return 0;
175: }
176: }
|