View Javadoc

1   package org.molwind.view;
2   
3   /*
4    * This file is part of Molwind.
5    *
6    * Molwind is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Molwind is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Molwind. If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  import java.awt.Color;
21  import java.awt.Graphics;
22  import java.awt.image.BufferedImage;
23  import java.io.IOException;
24  import java.io.OutputStream;
25  import java.util.ArrayList;
26  import java.util.Iterator;
27  
28  import org.molwind.model.LayeredPosition;
29  import org.molwind.model.WorldEntity;
30  
31  /**
32   * AbstractTile holds the entities visible on the tile.
33   *
34   * @author <a href="mailto:oliver.karch@molwind.org">Oliver Karch</a>
35   * @version 1.0
36   */
37  public abstract class AbstractTile implements Tile {
38  
39      private LayeredPosition position;
40      private ArrayList entities;
41      private String imageType;
42  
43      private static BufferedImage emptyImage;
44  
45      protected AbstractTile(final LayeredPosition newPosition) {
46          this.position = newPosition;
47          this.entities = new ArrayList();
48          this.imageType = "JPG";
49      }
50  
51      protected BufferedImage getEmptyImage() {
52          if (emptyImage == null) {
53              emptyImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
54              Graphics graphic = emptyImage.getGraphics();
55              graphic.setColor(Color.white);
56              graphic.fillRect(0, 0, 1, 1);
57          }
58  
59          return emptyImage;
60      }
61  
62  
63      /**
64       * Get the ImageType value.
65       *
66       * @return
67       *      the ImageType value
68       */
69      public String getImageType() {
70          return imageType;
71      }
72  
73      /**
74       * Set the ImageType value.
75       *
76       * @param newImageType
77       *      the new ImageType value
78       */
79      public void setImageType(final String newImageType) {
80          this.imageType = newImageType;
81      }
82  
83      /**
84       * Writes the tile's image to the given output stream.
85       *
86       * @param output
87       *      the output stream
88       * @throws java.io.IOException
89       *      is thrown upon i/o error
90       */
91      public abstract void writeTo(OutputStream output)
92      throws IOException;
93  
94      /**
95       * Get the Position value.
96       *
97       * @return
98       *      the Position value
99       */
100     public LayeredPosition getPosition() {
101         return position;
102     }
103 
104     /**
105      * Set the Position value.
106      *
107      * @param newPosition
108      *      the new Position value
109      */
110     public void setPosition(final LayeredPosition newPosition) {
111         this.position = newPosition;
112     }
113 
114     /**
115      * Adds entities to this tile.
116      *
117      * @param it
118      *      a collection of entities
119      */
120     public void addEntities(final Iterator it) {
121         while (it.hasNext()) {
122             WorldEntity entity = (WorldEntity) it.next();
123             entities.add(entity);
124         }
125     }
126 
127     /**
128      * Returns an iterator over the entities of this tile.
129      *
130      * @return
131      *      an entity iterator
132      */
133     public Iterator getEntityIterator() {
134         return entities.iterator();
135     }
136 
137     /**
138      * Returns the number of entities on this tile.
139      *
140      * @return
141      *      the number of entities
142      */
143     public int getEntityCount() {
144         return entities.size();
145     }
146 
147 }