-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.java
82 lines (63 loc) · 1.66 KB
/
Player.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
import java.awt.geom.Rectangle2D;
import java.awt.geom.Ellipse2D;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Color;
public class Player implements Runnable {
private static final int XSIZE = 35;
private static final int YSIZE = 10;
private JPanel panel;
private int x;
private int y;
private int dx;
private Color clr;
Graphics2D g2;
private Color backgroundColor;
public Player(JPanel p, int xPos, int yPos, Color clr) {
panel = p;
backgroundColor = panel.getBackground ();
x = xPos;
y = yPos;
dx = 5;
this.clr = clr;
}
@Override
public void run() {}
public void draw() {
Graphics g = panel.getGraphics ();
g2 = (Graphics2D) g;
Ellipse2D bat =new Ellipse2D.Double (x, y, XSIZE, YSIZE);
g2.setColor(this.clr);
g2.fill(bat);
}
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 Ellipse2D.Double(x, y, XSIZE, YSIZE));
}
public void moveLeft() {
if (!panel.isVisible()) return;
if (x - dx > 0) x = x - dx;
}
public void moveRight() {
Dimension dimension;
if (!panel.isVisible ()) return;
dimension = panel.getSize();
if (x + dx + XSIZE < dimension.width) x = x + dx;
}
public int getx() {
return this.x;
}
public int gety(){
return this.y;
}
public Player getPlayer() {
return this;
}
}