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:
027: package javax.microedition.lcdui.game;
028:
029: import javax.microedition.lcdui.Image;
030: import javax.microedition.lcdui.Graphics;
031:
032: /**
033: * A Layer is an abstract class representing a visual element of a game.
034: * Each Layer has position (in terms of the upper-left corner of its visual
035: * bounds), width, height, and can be made visible or invisible.
036: * Layer subclasses must implement a {@link #paint(Graphics)} method so that
037: * they can be rendered.
038: * <p>
039: * The Layer's (x,y) position is always interpreted relative to the coordinate
040: * system of the Graphics object that is passed to the Layer's paint() method.
041: * This coordinate system is referred to as the <em>painter's</em> coordinate
042: * system. The initial location of a Layer is (0,0).
043: *
044: * <p>
045: **/
046:
047: public abstract class Layer {
048:
049: /**
050: * Creates a new Layer with the specified dimensions.
051: *
052: * This constructor is declared package scope to
053: * prevent developers from creating Layer subclasses
054: *
055: * By default, a Layer is visible and its upper-left
056: * corner is positioned at (0,0).
057: * @param width The width of the layer, in pixels
058: * @param height The height of the layer, in pixels
059: *
060: */
061: Layer(int width, int height) {
062: setWidthImpl(width);
063: setHeightImpl(height);
064: }
065:
066: /**
067: * Sets this Layer's position such that its upper-left corner
068: * is located at (x,y) in the painter's coordinate system.
069: * A Layer is located at (0,0) by default.
070: * <br>
071: * @param x the horizontal position
072: * @param y the vertical position
073: * @see #move
074: * @see #getX
075: * @see #getY
076: *
077: */
078: public void setPosition(int x, int y) {
079: this .x = x;
080: this .y = y;
081: }
082:
083: /**
084: * Moves this Layer by the specified horizontal and vertical distances.
085: * <br>
086: * The Layer's coordinates are subject to wrapping if the passed
087: * parameters will cause them to exceed beyond Integer.MAX_VALUE
088: * or Integer.MIN_VALUE.
089: * @param dx the distance to move along horizontal axis (positive
090: * to the right, negative to the left)
091: * @param dy the distance to move along vertical axis (positive
092: * down, negative up)
093: * @see #setPosition
094: * @see #getX
095: * @see #getY
096: *
097: */
098: public void move(int dx, int dy) {
099: x += dx;
100: y += dy;
101: }
102:
103: /**
104: * Gets the horizontal position of this Layer's upper-left corner
105: * in the painter's coordinate system.
106: * <p>
107: * @return the Layer's horizontal position.
108: * @see #getY
109: * @see #setPosition
110: * @see #move
111: *
112: */
113: public final int getX() {
114: return x;
115: }
116:
117: /**
118: * Gets the vertical position of this Layer's upper-left corner
119: * in the painter's coordinate system.
120: * <p>
121: * @return the Layer's vertical position.
122: * @see #getX
123: * @see #setPosition
124: * @see #move
125: *
126: */
127: public final int getY() {
128: return y;
129: }
130:
131: /**
132: * Gets the current width of this layer, in pixels.
133: * @return the width in pixels
134: * @see #getHeight
135: *
136: **/
137: public final int getWidth() {
138: return width;
139: }
140:
141: /**
142: * Gets the current height of this layer, in pixels.
143: * @return the height in pixels
144: * @see #getWidth
145: *
146: **/
147: public final int getHeight() {
148: return height;
149: }
150:
151: /**
152: * Sets the visibility of this Layer. A visible Layer is rendered when
153: * its {@link #paint(Graphics)} method is called; an invisible Layer is
154: * not rendered.
155: * @param visible <code>true</code> to make the <code>Layer</code> visible,
156: * <code>false</code> to make it invisible
157: * @see #isVisible
158: *
159: */
160: public void setVisible(boolean visible) {
161: this .visible = visible;
162: }
163:
164: /**
165: * Gets the visibility of this Layer.
166: * @return <code>true</code> if the <code>Layer</code> is visible,
167: * <code>false</code> if it is invisible.
168: * @see #setVisible
169: *
170: */
171: public final boolean isVisible() {
172: return visible;
173: }
174:
175: /**
176: * Paints this Layer if it is visible. The upper-left corner of the Layer
177: * is rendered at it's current (x,y) position relative to the origin of
178: * the provided Graphics object. Applications may make use of Graphics
179: * clipping and translation to control where the Layer is rendered and to
180: * limit the region that is rendered.
181: * <P>
182: * Implementations of this method are responsible for checking if this
183: * Layer is visible; this method does nothing if the Layer is not
184: * visible.
185: * <p>
186: * The attributes of the Graphics object (clip region, translation,
187: * drawing color, etc.) are not modified as a result of calling this
188: * method.
189: *
190: * @param g the graphics object for rendering the <code>Layer</code>
191: * @throws NullPointerException if <code>g</code> is <code>null</code>
192: */
193: public abstract void paint(Graphics g);
194:
195: /**
196: * Sets the current width of this layer, in pixels. The Layer's width is
197: * used to determine its bounds for rendering purposes.
198: * @param width The width in pixels
199: * @throws IllegalArgumentException if the specified width is less than 0
200: * @see #setHeightImpl
201: * @see #getHeight
202: * @see #getWidth
203: *
204: **/
205: void setWidthImpl(int width) {
206: if (width < 0) {
207: throw new IllegalArgumentException();
208: }
209: this .width = width;
210: }
211:
212: /**
213: * Sets the current height of this layer, in pixels. The Layer's height
214: * is used to determine its bounds for rendering purposes.
215: * @param height The height in pixels
216: * @throws IllegalArgumentException if the specified height is less than 0
217: * @see #setWidthImpl
218: * @see #getHeight
219: * @see #getWidth
220: *
221: **/
222: void setHeightImpl(int height) {
223: if (height < 0) {
224: throw new IllegalArgumentException();
225: }
226: this .height = height;
227: }
228:
229: /**
230: * position of layer in x offset
231: */
232: int x; // = 0;
233:
234: /**
235: * position of layer in y offset
236: */
237: int y; // = 0;
238:
239: /**
240: * width of layer
241: */
242: int width; // = 0;
243:
244: /**
245: * height of layer
246: */
247: int height; // = 0;
248:
249: /**
250: * If the Layer is visible it will be drawn when <code>paint</code>
251: * is called.
252: */
253: boolean visible = true;
254:
255: }
|