-
Notifications
You must be signed in to change notification settings - Fork 0
/
PoliceCar.java
62 lines (52 loc) · 1.36 KB
/
PoliceCar.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
package car_games;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;
import javax.swing.Timer;
public class PoliceCar extends JComponent
{
private final BufferedImage policeCarImage;
private int yPosition;
private final Timer timer;
public PoliceCar(int roadNumber, int velocityInMs, BufferedImage image)
{
setSize(100, 100);
setStartingPositionFromRoadNumber(roadNumber);
policeCarImage = image;
timer = new Timer(velocityInMs, (e) ->
{
yPosition += 10;
setLocation(getLocation().x, yPosition);
repaint();
});
}
private void setStartingPositionFromRoadNumber(int roadNumber)
{
switch (roadNumber) {
case 1:
roadNumber = 75;
break;
case 2:
roadNumber = 175;
break;
case 3:
roadNumber = 275;
break;
}
yPosition = -100;
setLocation(roadNumber, yPosition);
}
public void start()
{
timer.start();
}
public void stop() {
timer.stop();
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(policeCarImage, 0, 0, this);
}
}