/
UserInterface.java
112 lines (92 loc) · 2.78 KB
/
UserInterface.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package snake;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class UserInterface extends JPanel implements DisplayListener, KeyListener{
public static final int SCALE = 20;
private int m_map[][];
private GameBody m_body;
private boolean m_isBegin;
public UserInterface(int map[][]){
this.m_map = map;
this.m_isBegin = true;
this.m_body = new GameBody(this, map, 100);
this.setBackground(Color.WHITE);
this.repaint();
}
/*public void setGameBody(GameBody body){
this.m_body = body;
}*/
public void paint(Graphics g){
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
if(this.m_isBegin){
g2.drawString("press any key to start", 120, 190);
g2.drawString("press any key to stop / resume", 100, 210);
}
this.paintMap(g2);
}
private void paintMap(Graphics2D g2){
for(int y = 0; y < this.m_map.length; y++){
for(int x = 0; x < this.m_map[y].length; x++){
if(this.m_map[y][x] == GameBody.SNAKE ||
this.m_map[y][x] == GameBody.SNAKE_HEAD){
g2.drawRect(x * UserInterface.SCALE, y * UserInterface.SCALE,
UserInterface.SCALE, UserInterface.SCALE);
}
else if(this.m_map[y][x] == GameBody.FRUIT){
g2.fillOval(x * UserInterface.SCALE, y * UserInterface.SCALE,
UserInterface.SCALE, UserInterface.SCALE);
}
}
}
}
public Dimension getPreferredSize(){
return new Dimension(
this.m_map[0].length * UserInterface.SCALE,
this.m_map.length * UserInterface.SCALE);
}
public void update(GameEvent e){
this.m_map = e.getMap();
if(e.getState() == Snake.DEAD){
int respond = JOptionPane.showConfirmDialog(this, "Snake is DEAD!!\nPlay again?");
if(respond == JOptionPane.OK_OPTION){
this.m_isBegin = true;
this.m_body = new GameBody(this, this.m_map, 125);
System.gc();
}
else if(respond == JOptionPane.CANCEL_OPTION || respond == JOptionPane.NO_OPTION){
System.exit(0);
}
}
this.repaint();
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_UP){
this.m_body.turnSnakeDirection(SnakePoint.DIRECTION_UP);
}
else if(e.getKeyCode() == KeyEvent.VK_DOWN){
this.m_body.turnSnakeDirection(SnakePoint.DIRECTION_DOWN);
}
else if(e.getKeyCode() == KeyEvent.VK_LEFT){
this.m_body.turnSnakeDirection(SnakePoint.DIRECTION_LEFT);
}
else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
this.m_body.turnSnakeDirection(SnakePoint.DIRECTION_RIGHT);
}
else{
if(this.m_isBegin){
this.m_body.start();
}
else{
this.m_body.stop();
}
this.m_isBegin = !this.m_isBegin;
}
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
}