-
Notifications
You must be signed in to change notification settings - Fork 0
/
LifeGame.java
219 lines (207 loc) · 6.01 KB
/
LifeGame.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package GameOfLife;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class LifeGame extends JFrame implements MouseMotionListener{
private final World world;
//JButton button = new JButton ("button");
static JMenu location=new JMenu();
public LifeGame(int rows,int columns)
{
world=new World(rows, columns);
world.setBackground(Color.LIGHT_GRAY);
new Thread(world).start();
add(world);
}
public static void main(String[]args)
{
LifeGame frame=new LifeGame(40, 50);
frame.addMouseMotionListener(frame);
JMenuBar menu=new JMenuBar();
frame.setJMenuBar(menu);
JMenu options =new JMenu("Options");
menu.add(options);
JMenu changeSpeed=new JMenu("ChangeSpeed");
menu.add(changeSpeed);
JMenu other = new JMenu("Other");
menu.add(other);
// JMenuItem arrow=options.add("Arrow");
// arrow.addActionListener(frame.new ArrowActionListener());
// JMenuItem square=options.add("Square");
// square.addActionListener(frame.new SquareActionListener());
JMenuItem start=options.add("Start");
start.addActionListener(frame.new StartActionListener());
JMenuItem random=options.add("Random");
random.addActionListener(frame.new RandomActionListener());
JMenuItem stop=options.add("Stop");
stop.addActionListener(frame.new StopActionListener());
JMenuItem pause=options.add("Pause");
pause.addActionListener(frame.new PauseActionListener());
JMenuItem doityourself=options.add("Add");
doityourself.addActionListener(frame.new DIYActionListener());
JMenuItem clean=options.add("Kill");
clean.addActionListener(frame.new CleanActionListener());
JMenuItem slow=changeSpeed.add("Slow");
slow.addActionListener(frame.new SlowActionListener());
JMenuItem fast=changeSpeed.add("Fast");
fast.addActionListener(frame.new FastActionListener());
JMenuItem hyper=changeSpeed.add("Hyper");
hyper.addActionListener(frame.new HyperActionListener());
JMenuItem help=other.add("Help");
help.addActionListener(frame.new HelpActionListener());
JMenuItem about=other.add("About");
about.addActionListener(frame.new AboutActionListener());
//about.addActionListener(frame.new AboutActionListener());
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1007,859);
//frame.setSize(1207,859);
//JButton button = new JButton ("button");
//frame.button.setBounds(108,100,100,100);
//frame.add(frame.button,BorderLayout.EAST);
frame.setTitle("Game of Life");
frame.setVisible(true);
frame.setResizable(false);
}
// class ArrowActionListener implements ActionListener
// {
// public void actionPerformed(ActionEvent e)
// {
// world.diy=false;
// world.setArrow();
// }
// }
// class SquareActionListener implements ActionListener
// {
// public void actionPerformed(ActionEvent e)
// {
// world.diy=false;
// world.setSquare();
// }
// }
class RandomActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
world.diy=false;
world.clean=false;
world.setBackground(Color.LIGHT_GRAY);
//world.setStop();
world.setRandom();
}
}
class StartActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//world.begintime=System.currentTimeMillis();
world.setBackground(Color.LIGHT_GRAY);
world.diy=false;
world.clean=false;
world.setShape();
}
}
class StopActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//world.time=0;
world.setBackground(Color.LIGHT_GRAY);
world.diy=false;
world.clean=false;
world.setStop();
}
}
class PauseActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
world.setBackground(Color.LIGHT_GRAY);
world.diy=false;
world.clean=false;
world.setPause();
}
}
class SlowActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
world.changeSpeedSlow();
}
}
class FastActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
world.changeSpeedFast();
}
}
class HyperActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
world.changeSpeedHyper();
}
}
class HelpActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "这是生命游戏!!!\n生命游戏是英国数学家约翰·何顿·康威在1970年发明的细胞自动机\n "+"1.如果一个细胞周围有3个细胞为生,则该细胞为生;\n"
+"2. 如果一个细胞周围有2个细胞为生,则该细胞的生死状态保持不变;\n"
+"3. 在其它情况下,该细胞为死。");
}
}
class AboutActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "游戏作者:沈毅、莫永佳、郑鹏飞");
}
}
class CleanActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
world.setPause();
world.clean=true;
world.diy=false;
world.setBackground(Color.orange);
}
}
class DIYActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
world.setPause();
world.diy=true;
world.clean=false;
world.setBackground(Color.cyan);
}
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
if(world.diy){
int x=e.getX();
int y=e.getY();
//button.setText("x:"+x+"y:"+y);
World.pauseshape[(y-50)/20][x/20]=1;
world.setDiy();
}
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
if(world.clean){
int x=e.getX();
int y=e.getY();
//button.setText("x:"+x+"y:"+y);
World.pauseshape[(y-50)/20][x/20]=0;
world.setDiy();
}
}
}