-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStarBoard.java
More file actions
53 lines (43 loc) · 1.08 KB
/
StarBoard.java
File metadata and controls
53 lines (43 loc) · 1.08 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class StarBoard extends JPanel implements ActionListener
{
private Image star;
private Timer timer;
private int x, y;
public StarBoard()
{
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;
// every 25ms the timmer will call the actionPerformed()
timer = new Timer( 25, this );
timer.start();
}
// 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 );
}
// required listener method
public void actionPerformed( ActionEvent e )
{
x++;
y++;
if( y > 200 )
{
x = x - 90;
y = y - 90;
}
repaint();
}
}