-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStarBoard2.java
More file actions
65 lines (54 loc) · 1.42 KB
/
StarBoard2.java
File metadata and controls
65 lines (54 loc) · 1.42 KB
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
import java.awt.*;
import java.util.*;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
// uses a utility timer with scheduled task instead of a listener timer
// should make games more accurate
// CHANGE #1
// remove ActionListener implementation
public class StarBoard2 extends JPanel
{
private Image star;
private Timer timer;
private int x, y;
public StarBoard2()
{
ImageIcon ii = new ImageIcon( getClass().getResource( "tako.png" ) );
star = ii.getImage();
// this jpanel component will use a buffer to paint
// all drawings will be done in memory first on the off-screen buffer
// then copy what's on the off-screen buffer to the screen
setDoubleBuffered( true );
x = 10;
y = 10;
// CHANGE #2:
// do scheduled task with a 100 ms initial delay and repeat every 10 ms
timer = new Timer();
timer.scheduleAtFixedRate( new ScheduleTask(), 100, 10 );
}
// all the drawing is done in this method
public void paint( Graphics g )
{
super.paint( g );
Graphics2D g2d = ( Graphics2D )g;
// do drawings
g2d.drawImage( star, x, y, this );
}
// CHANGE #3:
// create ScheduleTask class with run()
// move listener code inside run()
class ScheduleTask extends TimerTask
{
public void run()
{
x++;
y++;
if( y > 200 )
{
x = x - 90;
y = y - 90;
}
repaint();
}
}
}