Source Code Cross Referenced for TiledLayer.java in  » 6.0-JDK-Modules » j2me » javax » microedition » lcdui » game » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » javax.microedition.lcdui.game 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 TiledLayer is a visual element composed of a grid of cells that
034:         * can be filled with a set of
035:         * tile images.  This class allows large virtual layers to be created
036:         * without the need for an
037:         * extremely large Image.  This technique is commonly used in 2D
038:         * gaming platforms to create
039:         * very large scrolling backgrounds,
040:         * <P>
041:         * <h3>Tiles</h3>
042:         * The tiles used to fill the TiledLayer's cells are provided in a
043:         * single Image object which
044:         * may be mutable or immutable.  The Image is broken up into a series
045:         * of equally-sized tiles;
046:         * the tile size is specified along with the Image.  As shown in the
047:         * figure below, the same
048:         * tile set can be stored in several different arrangements depending
049:         * on what is the most
050:         * convenient for the game developer.  
051:         * <br>
052:         * <center><img src="doc-files/tiles.gif" width=588 height=412
053:         *   ALT="Tiles"></center>
054:         * <br>
055:         * Each tile is assigned a unique index number.  The tile located in
056:         * the upper-left corner
057:         * of the Image is assigned an index of 1.  The remaining tiles are
058:         * then numbered consecutively
059:         * in row-major order (indices are assigned across the first row, then
060:         * the second row, and so on).
061:         * These tiles are regarded as <em>static tiles</em> because there is
062:         * a fixed link between
063:         * the tile and the image data associated with it.
064:         * <P> 
065:         * A static tile set is created when the TiledLayer is instantiated;
066:         * it can also be updated
067:         * at any time using the {@link #setStaticTileSet} method.
068:         * <P>
069:         * In addition to the static tile set, the developer can also define
070:         * several <em>animated tiles</em>.
071:         * An animated tile is a virtual tile that is dynamically associated
072:         * with a static tile; the appearance
073:         * of an animated tile will be that of the static tile that it is
074:         * currently associated with.
075:         * <P>
076:         * Animated tiles allow the developer to change the appearance of a
077:         * group of cells
078:         * very easily.  With the group of cells all filled with the animated
079:         * tile, the appearance
080:         * of the entire group can be changed by simply changing the static
081:         * tile associated with the
082:         * animated tile.  This technique is very useful for animating large
083:         * repeating areas without
084:         * having to explicitly change the contents of numerous cells.
085:         * <P>
086:         * Animated tiles are created using the {@link #createAnimatedTile}
087:         * method, which returns the
088:         * index to be used for the new animated tile.  The animated tile
089:         * indices are always negative
090:         * and consecutive, beginning with -1.  Once created, the static tile
091:         * associated with an
092:         * animated tile can be changed using the {@link #setAnimatedTile}
093:         * method.
094:         * <P>
095:         * <h3>Cells</h3>
096:         * The TiledLayer's grid is made up of equally sized cells; the number
097:         * of rows and
098:         * columns in the grid are specified in the constructor, and the
099:         * physical size of the cells
100:         * is defined by the size of the tiles.  
101:         * <P>
102:         * The contents of each cell is specified by means of a tile index; a
103:         * positive tile index refers
104:         * to a static tile, and a negative tile index refers to an animated
105:         * tile.  A tile index of 0
106:         * indicates that the cell is empty; an empty cell is fully
107:         * transparent and nothing is drawn
108:         * in that area by the TiledLayer.  By default, all cells contain tile
109:         * index 0.
110:         * <P>
111:         * The contents of cells may be changed using {@link #setCell} and
112:         * {@link #fillCells}.  Several
113:         * cells may contain the same tile; however, a single cell cannot
114:         * contain more than one tile.
115:         * The following example illustrates how a simple background can be
116:         * created using a TiledLayer.
117:         * <br>
118:         * <center><img src="doc-files/grid.gif" width=735 height=193
119:         * ALT="TiledLayer Grid"></center>
120:         * <br>
121:         * In this example, the area of water is filled with an animated tile
122:         * having an index of -1, which
123:         * is initially associated with static tile 5.  The entire area of
124:         * water may be animated by simply
125:         * changing the associated static tile using <code>setAnimatedTile(-1,
126:         * 7)</code>.
127:         * <br>
128:         * <center><img src="doc-files/grid2.gif" width=735 height=193
129:         * ALT="TiledLayer Grid 2"></center>
130:         * <br>
131:         * <P>
132:         * <h3>Rendering a TiledLayer</h3>
133:         * A TiledLayer can be rendered by manually calling its paint method;
134:         * it can also be rendered
135:         * automatically using a LayerManager object.
136:         * <P>
137:         * The paint method will attempt to render the entire TiledLayer
138:         * subject to the
139:         * clip region of the Graphics object; the upper left corner of the
140:         * TiledLayer is rendered at
141:         * its current (x,y) position relative to the Graphics object's
142:         * origin.  The rendered region
143:         * may be controlled by setting the clip region of the Graphics object
144:         * accordingly.
145:         * <P>
146:         */
147:        public class TiledLayer extends Layer {
148:
149:            /**
150:             * Creates a new TiledLayer.  <p>
151:             *
152:             * The TiledLayer's grid will be <code>rows</code> cells high and
153:             * <code>columns</code> cells wide.  All cells in the grid are initially
154:             * empty (i.e. they contain tile index 0).  The contents of the grid may
155:             * be modified through the use of {@link #setCell} and {@link #fillCells}.
156:             * <P>
157:             * The static tile set for the TiledLayer is created from the specified
158:             * Image with each tile having the dimensions of tileWidth x tileHeight.
159:             * The width of the source image must be an integer multiple of
160:             * the tile width, and the height of the source image must be an integer
161:             * multiple of the tile height; otherwise, an IllegalArgumentException
162:             * is thrown;<p>
163:             *
164:             * The entire static tile set can be changed using 
165:             * {@link  #setStaticTileSet(Image, int, int)}.
166:             * These methods should be used sparingly since they are both
167:             * memory and time consuming.
168:             * Where possible, animated tiles should be used instead to
169:             * animate tile appearance.<p>
170:             *
171:             * @param columns the width of the <code>TiledLayer</code>,
172:             * expressed as a number of cells
173:             * @param rows the height of the <code>TiledLayer</code>,
174:             * expressed as a number of cells
175:             * @param image the <code>Image</code> to use for creating
176:             *  the static tile set
177:             * @param tileWidth the width in pixels of a single tile
178:             * @param tileHeight the height in pixels of a single tile
179:             * @throws NullPointerException if <code>image</code> is <code>null</code>
180:             * @throws IllegalArgumentException if the number of <code>rows</code>
181:             *  or <code>columns</code> is less than <code>1</code>
182:             * @throws IllegalArgumentException if <code>tileHeight</code>
183:             *  or <code>tileWidth</code> is less than <code>1</code>
184:             * @throws IllegalArgumentException if the <code>image</code>
185:             *  width is not an integer multiple of the <code>tileWidth</code>
186:             * @throws IllegalArgumentException if the <code>image</code>
187:             * height is not an integer multiple of the <code>tileHeight</code>
188:             */
189:            public TiledLayer(int columns, int rows, Image image,
190:                    int tileWidth, int tileHeight) {
191:                // IllegalArgumentException will be thrown 
192:                // in the Layer super-class constructor
193:                super (columns < 1 || tileWidth < 1 ? -1 : columns * tileWidth,
194:                        rows < 1 || tileHeight < 1 ? -1 : rows * tileHeight);
195:
196:                // if img is null img.getWidth() will throw NullPointerException
197:                if (((image.getWidth() % tileWidth) != 0)
198:                        || ((image.getHeight() % tileHeight) != 0)) {
199:                    throw new IllegalArgumentException();
200:                }
201:                this .columns = columns;
202:                this .rows = rows;
203:
204:                cellMatrix = new int[rows][columns];
205:
206:                int noOfFrames = (image.getWidth() / tileWidth)
207:                        * (image.getHeight() / tileHeight);
208:                // the zero th index is left empty for transparent tile
209:                // so it is passed in  createStaticSet as noOfFrames + 1
210:                // Also maintain static indices is true
211:                // all elements of cellMatrix[][] 
212:                // are set to zero by new, so maintainIndices = true
213:                createStaticSet(image, noOfFrames + 1, tileWidth, tileHeight,
214:                        true);
215:            }
216:
217:            /**
218:             * Creates a new animated tile and returns the index that refers
219:             * to the new animated tile.  It is initially associated with
220:             * the specified tile index (either a static tile or 0).
221:             * <P>
222:             * The indices for animated tiles are always negative.  The first
223:             * animated tile shall have the index -1, the second, -2, etc.  
224:             *
225:             * @param staticTileIndex the index of the associated tile 
226:             * (must be <code>0</code> or a valid static tile index)
227:             * @return the index of newly created animated tile
228:             * @throws IndexOutOfBoundsException if the 
229:             * <code>staticTileIndex</code> is invalid
230:             */
231:            public int createAnimatedTile(int staticTileIndex) {
232:                // checks static tile 
233:                if (staticTileIndex < 0 || staticTileIndex >= numberOfTiles) {
234:                    throw new IndexOutOfBoundsException();
235:                }
236:
237:                if (anim_to_static == null) {
238:                    anim_to_static = new int[4];
239:                    numOfAnimTiles = 1;
240:                } else if (numOfAnimTiles == anim_to_static.length) {
241:                    // grow anim_to_static table if needed 
242:                    int new_anim_tbl[] = new int[anim_to_static.length * 2];
243:                    System.arraycopy(anim_to_static, 0, new_anim_tbl, 0,
244:                            anim_to_static.length);
245:                    anim_to_static = new_anim_tbl;
246:                }
247:                anim_to_static[numOfAnimTiles] = staticTileIndex;
248:                numOfAnimTiles++;
249:                return (-(numOfAnimTiles - 1));
250:            }
251:
252:            /**
253:             * Associates an animated tile with the specified static tile.  <p>
254:             *
255:             * @param animatedTileIndex the index of the animated tile
256:             * @param staticTileIndex the index of the associated tile
257:             * (must be <code>0</code> or a valid static tile index)
258:             * @throws IndexOutOfBoundsException if the 
259:             * <code>staticTileIndex</code> is invalid
260:             * @throws IndexOutOfBoundsException if the animated tile index
261:             * is invalid
262:             * @see #getAnimatedTile
263:             *
264:             */
265:            public void setAnimatedTile(int animatedTileIndex,
266:                    int staticTileIndex) {
267:                // checks static tile
268:                if (staticTileIndex < 0 || staticTileIndex >= numberOfTiles) {
269:                    throw new IndexOutOfBoundsException();
270:                }
271:                // do animated tile index check
272:                animatedTileIndex = -animatedTileIndex;
273:                if (anim_to_static == null || animatedTileIndex <= 0
274:                        || animatedTileIndex >= numOfAnimTiles) {
275:                    throw new IndexOutOfBoundsException();
276:                }
277:
278:                anim_to_static[animatedTileIndex] = staticTileIndex;
279:
280:            }
281:
282:            /**
283:             * Gets the tile referenced by an animated tile.  <p>
284:             *
285:             * Returns the tile index currently associated with the
286:             * animated tile.
287:             *
288:             * @param animatedTileIndex the index of the animated tile
289:             * @return the index of the tile reference by the animated tile
290:             * @throws IndexOutOfBoundsException if the animated tile index
291:             * is invalid
292:             * @see #setAnimatedTile
293:             */
294:            public int getAnimatedTile(int animatedTileIndex) {
295:                animatedTileIndex = -animatedTileIndex;
296:                if (anim_to_static == null || animatedTileIndex <= 0
297:                        || animatedTileIndex >= numOfAnimTiles) {
298:                    throw new IndexOutOfBoundsException();
299:                }
300:
301:                return anim_to_static[animatedTileIndex];
302:            }
303:
304:            /**
305:             * Sets the contents of a cell.  <P>
306:             *
307:             * The contents may be set to a static tile index, an animated
308:             * tile index, or it may be left empty (index 0)
309:             * @param col the column of cell to set
310:             * @param row the row of cell to set
311:             * @param tileIndex the index of tile to place in cell
312:             * @throws IndexOutOfBoundsException if there is no tile with index
313:             *         <code>tileIndex</code>
314:             * @throws IndexOutOfBoundsException if <code>row</code> or
315:             *         <code>col</code> is outside the bounds of the 
316:             *         <code>TiledLayer</code> grid
317:             * @see #getCell
318:             * @see #fillCells
319:             */
320:            public void setCell(int col, int row, int tileIndex) {
321:
322:                if (col < 0 || col >= this .columns || row < 0
323:                        || row >= this .rows) {
324:                    throw new IndexOutOfBoundsException();
325:                }
326:
327:                if (tileIndex > 0) {
328:                    // do checks for static tile 
329:                    if (tileIndex >= numberOfTiles) {
330:                        throw new IndexOutOfBoundsException();
331:                    }
332:                } else if (tileIndex < 0) {
333:                    // do animated tile index check
334:                    if (anim_to_static == null
335:                            || (-tileIndex) >= numOfAnimTiles) {
336:                        throw new IndexOutOfBoundsException();
337:                    }
338:                }
339:
340:                cellMatrix[row][col] = tileIndex;
341:
342:            }
343:
344:            /**
345:             * Gets the contents of a cell.  <p>
346:             *
347:             * Gets the index of the static or animated tile currently displayed in
348:             * a cell.  The returned index will be 0 if the cell is empty.
349:             *
350:             * @param col the column of cell to check
351:             * @param row the row of cell to check
352:             * @return the index of tile in cell
353:             * @throws IndexOutOfBoundsException if <code>row</code> or
354:             *         <code>col</code> is outside the bounds of the 
355:             *         <code>TiledLayer</code> grid
356:             * @see #setCell
357:             * @see #fillCells
358:             */
359:            public int getCell(int col, int row) {
360:                if (col < 0 || col >= this .columns || row < 0
361:                        || row >= this .rows) {
362:                    throw new IndexOutOfBoundsException();
363:                }
364:                return cellMatrix[row][col];
365:            }
366:
367:            /**
368:             * Fills a region cells with the specific tile.  The cells may be filled
369:             * with a static tile index, an animated tile index, or they may be left 
370:             * empty (index <code>0</code>). 
371:             *
372:             * @param col the column of top-left cell in the region
373:             * @param row the row of top-left cell in the region
374:             * @param numCols the number of columns in the region
375:             * @param numRows the number of rows in the region
376:             * @param tileIndex the Index of the tile to place in all cells in the 
377:             * specified region
378:             * @throws IndexOutOfBoundsException if the rectangular region
379:             *         defined by the parameters extends beyond the bounds of the
380:             *         <code>TiledLayer</code> grid
381:             * @throws IllegalArgumentException if <code>numCols</code> is less
382:             * than zero
383:             * @throws IllegalArgumentException if <code>numRows</code> is less
384:             * than zero
385:             * @throws IndexOutOfBoundsException if there is no tile with
386:             *         index <code>tileIndex</code>
387:             * @see #setCell
388:             * @see #getCell
389:             */
390:            public void fillCells(int col, int row, int numCols, int numRows,
391:                    int tileIndex) {
392:
393:                if (numCols < 0 || numRows < 0) {
394:                    throw new IllegalArgumentException();
395:                }
396:
397:                if (col < 0 || col >= this .columns || row < 0
398:                        || row >= this .rows || col + numCols > this .columns
399:                        || row + numRows > this .rows) {
400:                    throw new IndexOutOfBoundsException();
401:                }
402:
403:                if (tileIndex > 0) {
404:                    // do checks for static tile 
405:                    if (tileIndex >= numberOfTiles) {
406:                        throw new IndexOutOfBoundsException();
407:                    }
408:                } else if (tileIndex < 0) {
409:                    // do animated tile index check
410:                    if (anim_to_static == null
411:                            || (-tileIndex) >= numOfAnimTiles) {
412:                        throw new IndexOutOfBoundsException();
413:                    }
414:                }
415:
416:                for (int rowCount = row; rowCount < row + numRows; rowCount++) {
417:                    for (int columnCount = col; columnCount < col + numCols; columnCount++) {
418:                        cellMatrix[rowCount][columnCount] = tileIndex;
419:                    }
420:                }
421:            }
422:
423:            /**
424:             * Gets the width of a single cell, in pixels.
425:             * @return the width in pixels of a single cell in the 
426:             * <code>TiledLayer</code> grid
427:             */
428:            public final int getCellWidth() {
429:                return cellWidth;
430:            }
431:
432:            /**
433:             * Gets the height of a single cell, in pixels.
434:             * @return the height in pixels of a single cell in the 
435:             * <code>TiledLayer</code> grid
436:             */
437:            public final int getCellHeight() {
438:                return cellHeight;
439:            }
440:
441:            /**
442:             * Gets the number of columns in the TiledLayer grid. 
443:             * The overall width of the TiledLayer, in pixels, 
444:             * may be obtained by calling {@link #getWidth}.
445:             * @return the width in columns of the 
446:             * <code>TiledLayer</code> grid
447:             */
448:            public final int getColumns() {
449:                return columns;
450:            }
451:
452:            /**
453:             * Gets the number of rows in the TiledLayer grid.  The overall
454:             * height of the TiledLayer, in pixels, may be obtained by
455:             * calling {@link #getHeight}.
456:             * @return the height in rows of the 
457:             * <code>TiledLayer</code> grid
458:             */
459:            public final int getRows() {
460:                return rows;
461:            }
462:
463:            /**
464:             * Change the static tile set.  <p>
465:             *
466:             * Replaces the current static tile set with a new static tile set.
467:             * See the constructor {@link #TiledLayer(int, int, Image, int, int)}
468:             * for information on how the tiles are created from the
469:             * image.<p>
470:             *
471:             * If the new static tile set has as many or more tiles than the
472:             * previous static tile set,
473:             * the the animated tiles and cell contents will be preserve.  If
474:             * not, the contents of
475:             * the grid will be cleared (all cells will contain index 0) and
476:             * all animated tiles
477:             * will be deleted.
478:             * <P>
479:             * @param image the <code>Image</code> to use for creating the
480:             * static tile set
481:             * @param tileWidth the width in pixels of a single tile
482:             * @param tileHeight the height in pixels of a single tile
483:             * @throws NullPointerException if <code>image</code> is <code>null</code>
484:             * @throws IllegalArgumentException if <code>tileHeight</code>
485:             *  or <code>tileWidth</code> is less than <code>1</code>
486:             * @throws IllegalArgumentException if the <code>image</code>
487:             *  width is not an integer  multiple of the <code>tileWidth</code>
488:             * @throws IllegalArgumentException if the <code>image</code>
489:             *  height is not an integer  multiple of the <code>tileHeight</code>
490:             */
491:            public void setStaticTileSet(Image image, int tileWidth,
492:                    int tileHeight) {
493:                // if img is null img.getWidth() will throw NullPointerException
494:                if (tileWidth < 1 || tileHeight < 1
495:                        || ((image.getWidth() % tileWidth) != 0)
496:                        || ((image.getHeight() % tileHeight) != 0)) {
497:                    throw new IllegalArgumentException();
498:                }
499:                setWidthImpl(columns * tileWidth);
500:                setHeightImpl(rows * tileHeight);
501:
502:                int noOfFrames = (image.getWidth() / tileWidth)
503:                        * (image.getHeight() / tileHeight);
504:
505:                // the zero th index is left empty for transparent tile
506:                // so it is passed in  createStaticSet as noOfFrames + 1
507:
508:                if (noOfFrames >= (numberOfTiles - 1)) {
509:                    // maintain static indices
510:                    createStaticSet(image, noOfFrames + 1, tileWidth,
511:                            tileHeight, true);
512:                } else {
513:                    createStaticSet(image, noOfFrames + 1, tileWidth,
514:                            tileHeight, false);
515:                }
516:            }
517:
518:            /**
519:             * Draws the TiledLayer.  
520:             *
521:             * The entire TiledLayer is rendered subject to the clip region of
522:             * the Graphics object.
523:             * The TiledLayer's upper left corner is rendered at the
524:             * TiledLayer's current
525:             * position relative to the origin of the Graphics object.   The current
526:             * position of the TiledLayer's upper-left corner can be retrieved by 
527:             * calling {@link #getX()} and {@link #getY()}.
528:             * The appropriate use of a clip region and/or translation allows
529:             * an arbitrary region
530:             * of the TiledLayer to be rendered.
531:             * <p>
532:             * If the TiledLayer's Image is mutable, the TiledLayer is rendered 
533:             * using the current contents of the Image.
534:             * @param g the graphics object to draw the <code>TiledLayer</code>
535:             * @throws NullPointerException if <code>g</code> is <code>null</code>
536:             */
537:            public final void paint(Graphics g) {
538:
539:                if (g == null) {
540:                    throw new NullPointerException();
541:                }
542:
543:                if (visible) {
544:                    int startColumn = 0;
545:                    int endColumn = this .columns;
546:                    int startRow = 0;
547:                    int endRow = this .rows;
548:
549:                    // calculate the number of columns left of the clip
550:                    int number = (g.getClipX() - this .x) / cellWidth;
551:                    if (number > 0) {
552:                        startColumn = number;
553:                    }
554:
555:                    // calculate the number of columns right of the clip
556:                    int endX = this .x + (this .columns * cellWidth);
557:                    int endClipX = g.getClipX() + g.getClipWidth();
558:                    number = (endX - endClipX) / cellWidth;
559:                    if (number > 0) {
560:                        endColumn -= number;
561:                    }
562:
563:                    // calculate the number of rows above the clip
564:                    number = (g.getClipY() - this .y) / cellHeight;
565:                    if (number > 0) {
566:                        startRow = number;
567:                    }
568:
569:                    // calculate the number of rows below the clip
570:                    int endY = this .y + (this .rows * cellHeight);
571:                    int endClipY = g.getClipY() + g.getClipHeight();
572:                    number = (endY - endClipY) / cellHeight;
573:                    if (number > 0) {
574:                        endRow -= number;
575:                    }
576:
577:                    // paint all visible cells
578:                    int tileIndex = 0;
579:
580:                    // y-coordinate
581:                    int ty = this .y + (startRow * cellHeight);
582:                    for (int row = startRow; row < endRow; row++, ty += cellHeight) {
583:
584:                        // reset the x-coordinate at the beginning of every row
585:                        // x-coordinate to draw tile into
586:                        int tx = this .x + (startColumn * cellWidth);
587:                        for (int column = startColumn; column < endColumn; column++, tx += cellWidth) {
588:
589:                            tileIndex = cellMatrix[row][column];
590:                            // check the indices 
591:                            // if animated get the corresponding 
592:                            // static index from anim_to_static table
593:                            if (tileIndex == 0) { // transparent tile
594:                                continue;
595:                            } else if (tileIndex < 0) {
596:                                tileIndex = getAnimatedTile(tileIndex);
597:                            }
598:
599:                            g.drawRegion(sourceImage, tileSetX[tileIndex],
600:                                    tileSetY[tileIndex], cellWidth, cellHeight,
601:                                    Sprite.TRANS_NONE, tx, ty, Graphics.TOP
602:                                            | Graphics.LEFT);
603:
604:                        }
605:                    }
606:                }
607:            }
608:
609:            // private implementation
610:
611:            /**
612:             * create the Image Array.
613:             *
614:             * @param image Image to use for creating the static tile set
615:             * @param noOfFrames total number of frames
616:             * @param tileWidth The width, in pixels, of a single tile
617:             * @param tileHeight The height, in pixels, of a single tile
618:             * @param maintainIndices 
619:             */
620:
621:            private void createStaticSet(Image image, int noOfFrames,
622:                    int tileWidth, int tileHeight, boolean maintainIndices) {
623:
624:                cellWidth = tileWidth;
625:                cellHeight = tileHeight;
626:
627:                int imageW = image.getWidth();
628:                int imageH = image.getHeight();
629:
630:                sourceImage = image;
631:
632:                numberOfTiles = noOfFrames;
633:                tileSetX = new int[numberOfTiles];
634:                tileSetY = new int[numberOfTiles];
635:
636:                if (!maintainIndices) {
637:                    // populate cell matrix, all the indices are 0 to begin with
638:                    for (rows = 0; rows < cellMatrix.length; rows++) {
639:                        int totalCols = cellMatrix[rows].length;
640:                        for (columns = 0; columns < totalCols; columns++) {
641:                            cellMatrix[rows][columns] = 0;
642:                        }
643:                    }
644:                    // delete animated tiles
645:                    anim_to_static = null;
646:                }
647:
648:                int currentTile = 1;
649:
650:                for (int locY = 0; locY < imageH; locY += tileHeight) {
651:                    for (int locX = 0; locX < imageW; locX += tileWidth) {
652:
653:                        tileSetX[currentTile] = locX;
654:                        tileSetY[currentTile] = locY;
655:
656:                        currentTile++;
657:                    }
658:                }
659:            }
660:
661:            /** 
662:             * the overall height of the TiledLayer grid
663:             */
664:            private int cellHeight; // = 0;
665:            /** 
666:             * the overall cell width of the TiledLayer grid
667:             */
668:            private int cellWidth; // = 0;
669:
670:            /** 
671:             * The num of rows of the TiledLayer grid.
672:             */
673:            private int rows; // = 0;
674:
675:            /** 
676:             * the num of columns in the TiledLayer grid
677:             */
678:            private int columns; // = 0;
679:
680:            /** 
681:             * int array for storing row and column of cell
682:             *
683:             * it contains the tile Index for both static and animated tiles
684:             */
685:            private int[][] cellMatrix; // = null;
686:
687:            /**
688:             * Source image for tiles
689:             */
690:            // package access as it is used by Pixel level Collision
691:            // detection with a Sprite
692:            Image sourceImage; // = null;
693:
694:            /**
695:             * no. of tiles
696:             */
697:            private int numberOfTiles; // = 0;
698:
699:            /**
700:             * X co-ordinate definitions for individual frames into the source image
701:             */
702:            // package access as it is used by Pixel level Collision
703:            // detection with a Sprite
704:            int[] tileSetX;
705:
706:            /**
707:             * Y co-ordinate definitions for individual frames into the source image
708:             */
709:            // package access as it is used by Pixel level Collision
710:            // detection with a Sprite
711:            int[] tileSetY;
712:
713:            /** 
714:             * Table to map from animated Index to static Index
715:             * 0th location is unused.
716:             * anim --> static Index
717:             * -1 --> 21
718:             * -2 --> 34
719:             * -3 --> 45
720:             * for now keep 0 the location of the table empty instead of computing
721:             * -index make index +ve and access this Table.
722:             *  
723:             */
724:            private int[] anim_to_static; // = null;
725:
726:            /** 
727:             * total number of animated tiles. This variable is also used as 
728:             * index in the above table to add new entries to the anim_to_static table.
729:             * initialized to 1 when table is created.
730:             */
731:            private int numOfAnimTiles; // = 0
732:
733:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.