-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGameFrame.java
executable file
·221 lines (202 loc) · 6.59 KB
/
GameFrame.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
220
221
import java.awt.Color;
import java.awt.Container;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* ライフゲーム用のFrameクラス
* @author Atsuya Sato
*/
public class GameFrame extends JFrame implements Runnable{
private int width;
private int height;
private boolean running = false;
//盤面に配置されたセルの配列
private ArrayList<LifeCell> cells;
//プリセットパターンを生成する際に使用。cellsを二次元配列にしたもの。
private LifeCell[][] cell_matrix;
/**
* Constructors
*/
public GameFrame(int width, int height){
super();
this.width = width;
this.height = height;
setSize(width,height);
cells = new ArrayList<LifeCell>();
}
public GameFrame(String title){
super(title);
cells = new ArrayList<LifeCell>();
}
/**
* セルサイズの変更
* @param cellSize
* @throws Exception
* @throws cellSizeがFrameSizeに合わない場合
*/
public void setCells(int cellSize) throws Exception {
if((width % cellSize != 0) || (width < cellSize)){
throw new Exception("セルのサイズがフレームの幅に合っていないか、セルサイズがフレームの幅より大きいです。");
}
if(height % cellSize != 0 || (height < cellSize)){
throw new Exception("セルのサイズがフレームの高さに合っていません、セルサイズがフレームの高さより大きいです。");
}
//縦の総セル数
int v_cell_cnt = height / cellSize;
//横の総セル数
int h_cell_cnt = width / cellSize;
//JFrameの高さには、タイトルバーが含まれてしまうため、heightを更新
setVisible(true);
//visible状態でないと、Insetsが取得不可
Insets insets = getInsets();
setVisible(false);
//セル用パネルの生成
JPanel p = new JPanel();
p.setLayout(null);
p.setBounds(0, 0, width + h_cell_cnt, height + v_cell_cnt);
p.setBackground(Color.black);
//ツールパネルの生成
JPanel toolPanel = new JPanel();
toolPanel.setLayout(null);
toolPanel.setBounds(0, p.getBounds().height, p.getBounds().width, 100);
toolPanel.setBackground(Color.white);
//パネルへのボタン追加
addButtonsOnPanel(toolPanel);
//フレームサイズの更新
int insets_height = insets.top + insets.bottom;
setResizable(true);
setSize(width + h_cell_cnt,height + insets_height + v_cell_cnt + toolPanel.getBounds().height);
setResizable(false);
cell_matrix = new LifeCell[v_cell_cnt][h_cell_cnt];
//セルの生成
for(int y = 0; y < v_cell_cnt; y ++){
for(int x = 0; x < h_cell_cnt; x++){
LifeCell cell = new LifeCell(new Rectangle(x * (cellSize + 1),y * (cellSize + 1),cellSize,cellSize));
p.add(cell);
cell_matrix[y][x] = cell;
cells.add(cell);
}
}
//セルの外周をセット。
for(int y = 0; y < v_cell_cnt ;y++){
for(int x = 0; x < h_cell_cnt ;x++){
LifeCell cell = cell_matrix[y][x];
int dx_min = -1;
int dy_min = -1;
int dx_max = 1;
int dy_max = 1;
//yが一番上
if(y == 0){ dy_min = 0; }
//xが一番左
if(x == 0){ dx_min = 0; }
//yが一番下
if(y == v_cell_cnt - 1){ dy_max = 0; }
//xが一番右
if(x == h_cell_cnt - 1){ dx_max = 0; }
for(int dx = dx_min; dx <= dx_max;dx++){
for(int dy = dy_min; dy <= dy_max;dy++){
if(!(dx == 0 && dy == 0)){
cell.addSurroundings(cell_matrix[y+dy][x+dx]);
}
}
}
}
}
Container contentPane = getContentPane();
contentPane.add(p);
contentPane.add(toolPanel);
}
/**
* ツールパネルへのボタン追加
*/
private void addButtonsOnPanel(JPanel panel){
//ボタンのアクションリスナー作成
ActionListener RunBtnAction = new ActionListener(){
public void actionPerformed(ActionEvent event){
//動作状態を切り替え
running = !running;
JButton btn = (JButton)event.getSource();
if(running){
btn.setText("Stop");
}else{
btn.setText("Run");
}
}
};
ActionListener ClearBtnAction = new ActionListener(){
public void actionPerformed(ActionEvent event){
//盤面全てを初期化
LifeCell.forceKillAll(cells);
}
};
ActionListener generateGliderBtnAction = new ActionListener(){
public void actionPerformed(ActionEvent event){
//グライダーのパターン生成
CellPattern.patternGenerator(cell_matrix, CellPattern.Pattern.GLIDER);
}
};
ActionListener generateSpaceShipBtnAction = new ActionListener(){
public void actionPerformed(ActionEvent event){
//宇宙船のパターン生成
CellPattern.patternGenerator(cell_matrix, CellPattern.Pattern.SPACESHIP);
}
};
ActionListener generateGalaxyBtnAction = new ActionListener(){
public void actionPerformed(ActionEvent event){
//銀河のパターン生成
CellPattern.patternGenerator(cell_matrix, CellPattern.Pattern.GALAXY);
}
};
//格納順を保持したいため、LinkedHashMapを使用
LinkedHashMap<String,ActionListener> btnSources = new LinkedHashMap<String,ActionListener>();
btnSources.put("Run", RunBtnAction);
btnSources.put("Clear", ClearBtnAction);
//スペーサー
btnSources.put("", null);
btnSources.put("グライダー", generateGliderBtnAction);
btnSources.put("宇宙船", generateSpaceShipBtnAction);
btnSources.put("銀河", generateGalaxyBtnAction);
//ボタン生成
int i = 0;
for(Map.Entry<String, ActionListener> btnSrc : btnSources.entrySet()){
//空文字の場合、インクリメントしてスキップ
if(btnSrc.getKey().equals("")) { i++; continue; }
JButton button = new JButton(btnSrc.getKey());
button.setBackground(Color.black);
button.setBounds(10 + i * 80,panel.getBounds().y, 80, 50);
button.addActionListener(btnSrc.getValue());
panel.add(button);
i++;
}
}
public void run(){
while(true){
//Startボタンが押されていない場合、処理を中断
while(!running){
try {
Thread.sleep(1000);
} catch (InterruptedException e) { e.printStackTrace(); }
}
try {
//更新速度
Thread.sleep(Const.SLEEP_TIME_MS);
} catch (InterruptedException e) { e.printStackTrace(); }
for(LifeCell cell : cells){
//周囲のセルの状況を確認
cell.checkSurroundings();
}
for(LifeCell cell : cells){
//世代交代(セルの塗り替え)
cell.generationalChange();
}
}
}
}