-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGameObject.java
More file actions
113 lines (103 loc) · 3.15 KB
/
GameObject.java
File metadata and controls
113 lines (103 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import java.awt.*;
import javax.swing.JComponent;
/**
* An abstract class for an object which can be added to an instance of
* <code>Game</code><br>
* <br>
* The <code>act</code> method can be implemented to provide a behavior for
* the object, and will be called every millisecond automatically by a <code>
* Game</code> it has been added to.<br>
* @see Game#add
*/
public abstract class GameObject extends JComponent {
Color c = Color.red;
/**
* Sets the pixel width and height of the object
*
* @param width a width in pixels
* @param height a height in pixels
*/
public void setSize(int width, int height) {
super.setSize(width, height);
}
/**
* Gets the x component of the coordinate of the upper left corner of
* this object
*
* The coordinate is relative to the playing field, with <code>0</code>
* being the far left of the field, and positive values moving toward the right
* of the field
*/
public int getX() {
return getLocation().x;
}
/**
* Gets the y component of the coordinate of the upper left corner of
* this object
*
* The coordinate is relative to the playing field, with <code>0</code>
* being the top of the field, and positive values moving toward the
* bottom of the field
*/
public int getY() {
return getLocation().y;
}
/**
* Sets the x (horizontal) position of this object
*
* Setting the x position will not affect the y position
*
* @param x the x position of the upper left corner of this object
*/
public void setX(int x) {
super.setLocation(x, getLocation().y);
}
/**
* Sets the y (vertical) position of this object
*
* Setting the y position will not affect the x position
*
* @param y the y position of the upper left corner of this object
*/
public void setY(int y) {
super.setLocation(getLocation().x, y);
}
/**
* Sets the color of this object
*
* @param c the color of this object
* @see java.awt.Color
*/
public void setColor(Color c) {
this.c = c;
}
/**
* Paints the object on the screen. This is called automatically.
*
* Child classes should not implement or override this method.
*/
public void paint(Graphics g) {
Rectangle r = getBounds();
g.setColor(c);
g.fillRect(0, 0, (int)r.getWidth(), (int)r.getHeight());
}
/**
* Returns <code>true</code> if this object collides with another
* <code>GameObject</code>
*
* @param o the <code>GameObject</code> to test for collision
* @return <code>true</code> if collision occurs
*/
public boolean collides(GameObject o) {
return getBounds().intersects(o.getBounds());
}
/**
* This method should be implemented to provide a <i>behavior</i>
* for this object.
*
* The <code>Game</code> will automatically call this method every
* millisecond. It can be implemented to provide basic behavior for
* an object, such as movement.
*/
public abstract void act();
}