-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayField.java
93 lines (76 loc) · 1.81 KB
/
PlayField.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
//package bricks;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
class PlayField extends Canvas implements Runnable {
private final int _delay = 20;
private Thread _game;
private SpriteVector _sprites;
private Rectangle _bounds;
private Match _match;
private Image _offImg;
private Graphics _offGrfx;
public PlayField(Match m) {
_sprites = new SpriteVector();
_match = m;
}
public void paint(Graphics g) {
g.drawImage(_offImg, 0, 0, null);
}
public void update(Graphics g) {
if (_offGrfx == null) {
_offImg = createImage(getWidth(), getHeight());
_offGrfx = _offImg.getGraphics();
}
_offGrfx.clearRect(0, 0, getWidth(), getHeight());
_sprites.draw(_offGrfx);
drawMessage(_match.getMessage());
g.drawImage(_offImg, 0, 0, null);
}
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
long theStartTime = System.currentTimeMillis();
while (Thread.currentThread() == _game) {
_sprites.update();
repaint();
try {
theStartTime += _delay;
Thread.sleep(Math.max(0, theStartTime - System.currentTimeMillis()));
} catch (InterruptedException e) {
break;
}
}
}
public void start() {
if (_game == null) {
_bounds = new Rectangle(0,0,getWidth(),getHeight());
_game = new Thread(this);
_game.start();
}
}
void stop() {
if (_game != null) {
_game = null;
}
}
public Match getMatch() {
return _match;
}
public void addSprite(Sprite s) {
_sprites.add(s);
}
public void clean() {
//_sprites.removeAll();
_sprites.clear();//
}
public Sprite testCollision(Sprite s) {
return _sprites.testCollision(s);
}
private void drawMessage(String msg) {
_offGrfx.drawString(msg,getWidth()/2-20,getHeight()/2);
}
public Rectangle getBoundary() {
return _bounds;
}
}