-
Notifications
You must be signed in to change notification settings - Fork 0
/
MovingScreenObject.java
119 lines (104 loc) · 2.28 KB
/
MovingScreenObject.java
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
114
115
116
117
118
119
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
public abstract class MovingScreenObject extends ScreenObject
{
protected Vector vector;
protected Image image;
protected double angle;
/**
*
* @param location location of the moving screen object
* @param size size of the object
* @param i image of the object
*/
public MovingScreenObject(Point location, Rectangle size, Image i, double angle)
{
super(location, size);
image = i;
this.angle = angle;
}
/**
* Move the screen object
*/
public void move()
{
location.x += vector.getChangeX();
location.y += vector.getChangeY();
if (location.x > Screen.screenWidth) {
location.x -= Screen.screenWidth;
}
if (location.x < 0) {
location.x += Screen.screenWidth;
}
if (location.y > Screen.screenHeight) {
location.y -= Screen.screenHeight;
}
if (location.y < 0){
location.y += Screen.screenHeight;
}
}
/**
* Draw the screen object
*/
public void draw(Graphics g)
{
g.drawImage(image, location.x, location.y, size.width, size.height, null);
}
/**
* @return the arbitraryVector
*/
public Vector getVector() {
return vector;
}
/**
* @param vector the arbitraryVector to set
*/
public void setVector(Vector arbitraryVector) {
this.vector = arbitraryVector;
}
/**
* @return the arbitraryImage
*/
public Image getImage() {
return image;
}
/**
* @param arbitraryImage the arbitraryImage to set
*/
public void setArbitraryImage(Image arbitraryImage) {
this.image = arbitraryImage;
}
/**
* @return the angle
*/
public double getAngle() {
return angle;
}
/**
* @param angle the angle to set
*/
public void setAngle(double angle) {
this.angle = angle;
}
public void setImage(Image i) {
image = i;
}
/**
* Return true if the objects collide.
*
* @param obj The object whose position we're comparing.
* @return True if there is a collision; false, otherwise.
*/
public boolean collide(MovingScreenObject otherObj) {
Rectangle objRect = this.getSize();
Rectangle otherObjRect = otherObj.getSize();
objRect.setLocation(this.getLocation());
otherObjRect.setLocation(otherObj.getLocation());
if(otherObjRect.intersects(objRect)) {
return true;
}
return false;
}
}