-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoal.java
93 lines (77 loc) · 1.88 KB
/
Goal.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
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Line2D;
import javax.swing.JPanel;
public class Goal extends Thread{
private static final int XSIZE = 100;
private static final int YSIZE = 15;
private JPanel panel;
private int x;
private int y;
private int dx;
private int dy;
boolean mvright;
Graphics2D g2;
private Color backgroundColor;
private Dimension dimension;
public Goal (JPanel p, int xPos, int yPos) {
panel = p;
backgroundColor = panel.getBackground ();
x = xPos;
y = yPos;
dx = 5;
dy = 0;
mvright = false;
}
public void draw () {
Graphics g = panel.getGraphics ();
g2 = (Graphics2D) g;
Rectangle goal = new Rectangle(x, y, XSIZE, YSIZE);
g2.setColor(Color.gray);
g2.fill(goal);
}
public Rectangle2D.Double getBoundingRectangle() {
return new Rectangle2D.Double (x, y, XSIZE, YSIZE);
}
public void erase () {
Graphics g = panel.getGraphics ();
g2 = (Graphics2D) g;
g2.setColor (backgroundColor);
g2.fill (new Rectangle(x, y, XSIZE, YSIZE));
}
public void move () {
if (!panel.isVisible ()) return;
dimension = panel.getSize();
if (((x + dx + XSIZE) < dimension.width)&&mvright==false){
x+= dx;
}
if((x + dx + XSIZE) == dimension.width){
mvright = true;
}
if (((x - dx) > 0)&&mvright==true){
x = x - dx;
}
if((x-dx)==0){
mvright=false;
}
}
public void run () {
try {
draw ();
while (true) {
erase();
move ();
draw();
sleep (50);
}
}
catch(InterruptedException e) {
System.out.println(e +" bro idk nah");
}
}
}