-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakePanel.java
More file actions
53 lines (48 loc) · 1.47 KB
/
SnakePanel.java
File metadata and controls
53 lines (48 loc) · 1.47 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 javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
class SnakePanel extends JPanel implements ActionListener{
private SnakeBoard board;
private JButton restartButton, ghostButton;
public JLabel score;
private class Button extends JPanel{
Button(){
setLayout (new BoxLayout (this, BoxLayout.X_AXIS));
restartButton = new JButton ("Start/Restart");
add(restartButton);
add(Box.createHorizontalStrut(25));
setBorder( new EmptyBorder(50,0,0,0));
restartButton.setEnabled(true);
restartButton.setFocusable(false);
ghostButton = new JButton ("Ghost");
add(ghostButton);
add(Box.createHorizontalStrut(25));
setBorder( new EmptyBorder(50,0,0,0));
ghostButton.setEnabled(true);
ghostButton.setFocusable(false);
}
}
public SnakePanel(){
super();
setLayout (new BoxLayout(this, BoxLayout.Y_AXIS));
add (score = new JLabel());
score.setFont(new Font ("Serif", Font.BOLD, 20));
score.setForeground(Color.BLUE);
score.setAlignmentX(0.5f);
add ( board = new SnakeBoard(this));
add(new Button());
restartButton.addActionListener(this);
ghostButton.addActionListener(this);
setBorder( new EmptyBorder(50,50,50,50));
}
public void actionPerformed (ActionEvent e){
if (e.getSource().equals(restartButton)){
board.gameStart();
}
if (e.getSource().equals(ghostButton)){
board.ghost = !board.ghost;
board.setGhost();
}
}
}