Javadoc, small bugfix in levels.xml
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / level / MapSquare.java
blobb0da13a9e2c32f32024fa6c1abc99a51908b1f0f
1 package se.umu.cs.dit06ajnajs.level;
3 import java.awt.Graphics;
4 import java.awt.Image;
5 import java.awt.Point;
6 import java.util.Observable;
8 import se.umu.cs.dit06ajnajs.Clickable;
9 import se.umu.cs.dit06ajnajs.Paintable;
10 import se.umu.cs.dit06ajnajs.AntiTD;
11 import java.awt.Rectangle;
13 /**
14 * Abstract class MapSquare represents a square with a graphical representation
15 * and a position.
17 * @author dit06ajs
18 * @author dit06ajn
19 * @version 1.0
21 public abstract class MapSquare extends Observable implements Paintable,
22 Cloneable,
23 Clickable {
24 private int x;
25 private int y;
26 private int centerX;
27 private int centerY;
29 private Image image;
31 /**
32 * Creates new instans with position and image
33 * @param x the x coordinate of the square
34 * @param y the y coordinate of the square
35 * @param image the image of the square
37 public MapSquare(int x, int y, Image image) {
38 setX(x);
39 setY(y);
40 this.image = image;
43 /**
44 * Used to initialize fields that should be individually set for every
45 * MapSquare, since clone() is shallow.
47 * Empty method, ovveridden by subclass to implement behaviour.
49 public void init() {
53 /**
54 * Paints this MapSquares Image to the specified Grahics object.
56 * @param g The Graphics to paint to.
58 public void paint(Graphics g) {
59 g.drawImage(image, x, y, null);
62 /**
63 * Returns the Images used by this MapSquare.
65 * @return The Images used by this MapSquare.
67 public Image getImage() {
68 return this.image;
71 /**
72 * Returns the top left point of this MapSquare.
74 * @return The top left point of this MapSquare.
76 public Point getPosition() {
77 Point p = new Point(x, y);
78 return p;
81 /**
82 * Returns the center position of this MapSquare.
84 * @return The center position of this MapSquare.
86 public Point getCenterPoint() {
87 return new Point(this.centerX, this.centerY);
90 /**
91 * Returns the center x position of this MapSquare.
93 * @return The center x position of this MapSquare.
95 public int getCenterX() {
96 return this.centerX;
99 /**
100 * Returns the center y position of this MapSquare.
102 * @return The center y position of this MapSquare.
104 public int getCenterY() {
105 return this.centerY;
109 * Returns the x position of this MapSquare.
111 * @return The x position of this MapSquare.
113 public int getX() {
114 return x;
118 * Sets the x position and recalculates center x position of this MapSquare.
120 * @param x The new x center position.
122 public void setX(int x) {
123 this.x = x;
124 this.centerX = this.x + (int) Math.round(AntiTD.SQUARE_SIZE/2.0);
128 * Returns the center y position of this MapSquare.
130 * @return The center y position of this MapSquare.
132 public int getY() {
133 return y;
137 * Sets the y position and recalculates center x position of this MapSquare.
139 * @param y The new y center position.
141 public void setY(int y) {
142 this.y = y;
143 this.centerY = this.y + (int) Math.round(AntiTD.SQUARE_SIZE/2.0);
147 * Sets the Image of this MapSquare.
149 * @param img The Image of this MapSquare.
151 public void setImage(Image img) {
152 image = img;
156 * Empty method, ovveridden by subclass to implement behaviour when a user
157 * clicks this MapSquare.
159 public void click() {
163 * Checks if the specified x and y position is inside of bounds.
165 * @param x The x-value to check.
166 * @param y The y-value to check.
167 * @return If the specified x and y position is inside of bounds.
169 public boolean contains(int x, int y) {
170 return new Rectangle(this.x, this.y,
171 AntiTD.SQUARE_SIZE, AntiTD.SQUARE_SIZE).contains(x, y);
175 * Attemts to clone this MapSquare.
177 * @return A new instance of the same type as the instantiated MapSquare.
179 @Override
180 public Object clone() {
181 try {
182 return super.clone();
183 } catch (CloneNotSupportedException e) {
184 e.printStackTrace();
185 throw new Error("Object " + this.getClass().getName()
186 + " is not Cloneable");
190 @Override
191 public String toString() {
192 return "MapSquare: " + this.getClass().getName()
193 + " @("+ x +", " + y +")";