1 package se
.umu
.cs
.dit06ajnajs
.level
;
3 import java
.awt
.Graphics
;
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
;
14 * Abstract class MapSquare represents a square with a graphical representation
21 public abstract class MapSquare
extends Observable
implements Paintable
,
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
) {
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.
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);
63 * Returns the Images used by this MapSquare.
65 * @return The Images used by this MapSquare.
67 public Image
getImage() {
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
);
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
);
91 * Returns the center x position of this MapSquare.
93 * @return The center x position of this MapSquare.
95 public int getCenterX() {
100 * Returns the center y position of this MapSquare.
102 * @return The center y position of this MapSquare.
104 public int getCenterY() {
109 * Returns the x position of this MapSquare.
111 * @return The x position of this MapSquare.
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
) {
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.
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
) {
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
) {
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.
180 public Object
clone() {
182 return super.clone();
183 } catch (CloneNotSupportedException e
) {
185 throw new Error("Object " + this.getClass().getName()
186 + " is not Cloneable");
191 public String
toString() {
192 return "MapSquare: " + this.getClass().getName()
193 + " @("+ x
+", " + y
+")";