回答編集履歴
1
コードの改良
test
CHANGED
@@ -111,3 +111,155 @@
|
|
111
111
|
}
|
112
112
|
|
113
113
|
```
|
114
|
+
|
115
|
+
**追記**
|
116
|
+
|
117
|
+
ファイルを Main.java と Board.java に分割できるように
|
118
|
+
|
119
|
+
class Board を独立させました。
|
120
|
+
|
121
|
+
マウスボタンをクリックしたときの処理を追加してみました。
|
122
|
+
|
123
|
+
```Java
|
124
|
+
|
125
|
+
import java.awt.Graphics;
|
126
|
+
|
127
|
+
import java.awt.Color;
|
128
|
+
|
129
|
+
import java.awt.event.MouseAdapter;
|
130
|
+
|
131
|
+
import java.awt.event.MouseEvent;
|
132
|
+
|
133
|
+
import javax.swing.JFrame;
|
134
|
+
|
135
|
+
import javax.swing.JPanel;
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
class Main extends JFrame {
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
public static void main(String[] args) { new Main(); }
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
Main() {
|
148
|
+
|
149
|
+
setSize(650, 680);
|
150
|
+
|
151
|
+
setTitle("Othello");
|
152
|
+
|
153
|
+
setLocationRelativeTo(null);
|
154
|
+
|
155
|
+
setResizable(false);
|
156
|
+
|
157
|
+
setDefaultCloseOperation(EXIT_ON_CLOSE); // [x]ボタンで終了
|
158
|
+
|
159
|
+
add(new Board()); // 描画領域の追加
|
160
|
+
|
161
|
+
setVisible(true);
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
class Board extends JPanel {
|
170
|
+
|
171
|
+
static final int blank = 0, black = 1, white = 2;
|
172
|
+
|
173
|
+
static final int width = 8, height = 8;
|
174
|
+
|
175
|
+
static final int gs = 80;
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
int[][] board = new int[height][width];
|
180
|
+
|
181
|
+
int turn = black;
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
Board() {
|
186
|
+
|
187
|
+
for (int i = 0; i < height; i++)
|
188
|
+
|
189
|
+
for (int j = 0; j < width; j++) board[i][j] = blank;
|
190
|
+
|
191
|
+
board[3][3] = board[4][4] = white;
|
192
|
+
|
193
|
+
board[3][4] = board[4][3] = black;
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
addMouseListener(new MouseAdapter() {
|
198
|
+
|
199
|
+
public void mousePressed(MouseEvent e) {
|
200
|
+
|
201
|
+
int x = e.getX() / gs, y = e.getY() / gs;
|
202
|
+
|
203
|
+
if (x < 8 && y < 8) {
|
204
|
+
|
205
|
+
board[x][y] = turn;
|
206
|
+
|
207
|
+
turn = black + white - turn;
|
208
|
+
|
209
|
+
repaint();
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
});
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
public void paintComponent(Graphics g) {
|
222
|
+
|
223
|
+
g.setColor(new Color(0, 128, 128)); // 色を指定
|
224
|
+
|
225
|
+
g.fillRect(0, 0, gs * width, gs * height); // 全体を塗りつぶす
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
g.setColor(Color.black);
|
230
|
+
|
231
|
+
for (int i = 0; i < height; i++)
|
232
|
+
|
233
|
+
g.drawLine(gs * i, 0, gs * i, gs * height); // 縦線を引く
|
234
|
+
|
235
|
+
for (int i = 0; i < width; i++)
|
236
|
+
|
237
|
+
g.drawLine(0, gs * i, gs * width, gs * i); // 横線を引く
|
238
|
+
|
239
|
+
g.drawRect(0, 0, gs * width, gs * height); // 外枠(矩形)を描画
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
for (int i = 0; i < height; i++) {
|
244
|
+
|
245
|
+
for (int j = 0; j < width; j++) {
|
246
|
+
|
247
|
+
int piece = board[i][j];
|
248
|
+
|
249
|
+
if (piece != blank) {
|
250
|
+
|
251
|
+
g.setColor(piece == black ? Color.black : Color.white);
|
252
|
+
|
253
|
+
g.fillOval(gs * i + 4, gs * j + 4, gs - 8, gs - 8);
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
```
|