回答編集履歴

2

見直しキャンペーン中

2023/07/29 04:33

投稿

TN8001
TN8001

スコア9350

test CHANGED
@@ -1,601 +1,302 @@
1
1
  一気に編集されすぎて意味が分からないうえ文法エラーになっているので、当初のコードだとして回答します(長すぎるのもあれですが、短すぎはもっとイヤですね)
2
2
 
3
-
4
-
5
3
  yuki_javaさん自身も力技だと感じておられるようですが、どうせならスマートに解決したいですよね。
6
4
 
7
-
8
-
9
5
  今回のポイントは明らかに渦巻ですよね?
10
-
11
6
  課題か何かで「渦巻の数字を出力せよ」みたいなものもありそうですし、ヒントがないか検索してみます。
12
7
 
13
-
14
-
15
8
  「[java 数字 渦巻](https://www.google.co.jp/search?q=java+%E6%95%B0%E5%AD%97+%E6%B8%A6%E5%B7%BB)」で検索したらこちらが出ました。
16
-
17
9
  [2次元配列 に数値を並べる](http://miztools.so.land.to/java-aa/swing1_4/puzzle_array.html#rot_r)
18
10
 
19
-
20
-
21
11
  英語も直訳で「[java number swirl](https://www.google.com/search?q=java+number+swirl)」としたらこちらが出ました(Spiralなんですね~^^;
22
-
23
12
  [How To Create Spiral Of Numbers (Spiral Matrix) In Java?](https://javaconceptoftheday.com/how-to-create-spiral-of-numbers-matrix-in-java/)
24
13
 
25
-
26
-
27
14
  どちらも何をやっているかさっぱりわかりませんが、今回はxyの並びが好みな後者にしました^^;
28
-
29
15
  メソッドに切り出し、コンソール出力で動作確認をしてみましょう。
30
-
31
16
  ```Java
32
-
33
17
  public class Sample {
34
-
35
18
  public static void main(String[] args) {
36
-
37
19
  int[][] array = getSpiralMatrix(5);
38
20
 
39
-
40
-
41
21
  for (int[] a : array) {
42
-
43
22
  for (int i : a) {
44
-
45
23
  System.out.printf("%3d", i);
46
-
47
- }
24
+ }
48
-
49
25
  System.out.println();
50
-
51
- }
26
+ }
52
-
53
- }
27
+ }
54
-
55
-
56
28
 
57
29
  // [How To Create Spiral Of Numbers (Spiral Matrix) In Java?](https://javaconceptoftheday.com/how-to-create-spiral-of-numbers-matrix-in-java/)
58
-
59
30
  private static int[][] getSpiralMatrix(int n) {
60
-
61
31
  int[][] spiral = new int[n][n];
62
-
63
32
  int value = 1;
64
-
65
33
  int minCol = 0;
66
-
67
34
  int minRow = 0;
68
-
69
35
  int maxCol = n - 1;
70
-
71
36
  int maxRow = n - 1;
72
37
 
73
-
74
-
75
38
  while (value <= n * n) {
76
-
77
39
  for (int i = minCol; i <= maxCol; i++) {
78
-
79
40
  spiral[minRow][i] = value++;
80
-
81
- }
41
+ }
82
-
83
42
  for (int i = minRow + 1; i <= maxRow; i++) {
84
-
85
43
  spiral[i][maxCol] = value++;
86
-
87
- }
44
+ }
88
-
89
45
  for (int i = maxCol - 1; i >= minCol; i--) {
90
-
91
46
  spiral[maxRow][i] = value++;
92
-
93
- }
47
+ }
94
-
95
48
  for (int i = maxRow - 1; i >= minRow + 1; i--) {
96
-
97
49
  spiral[i][minCol] = value++;
98
-
99
- }
50
+ }
100
-
101
51
  minCol++; minRow++; maxCol--; maxRow--;
102
-
103
- }
52
+ }
104
-
105
-
106
53
 
107
54
  return spiral;
108
-
109
- }
55
+ }
110
-
111
- }
56
+ }
112
-
113
- ```
57
+ ```
114
-
115
-
116
-
58
+
117
- ```
59
+ ```
118
-
119
60
  1 2 3 4 5
120
-
121
61
  16 17 18 19 6
122
-
123
62
  15 24 25 20 7
124
-
125
63
  14 23 22 21 8
126
-
127
64
  13 12 11 10 9
128
-
129
- ```
65
+ ```
130
-
131
-
132
66
 
133
67
  左上から時計回りに数字が増えていくようですね。
134
68
 
135
-
136
-
137
69
  真ん中を1にするのは簡単ですね。そのうえで左右反転すればよさそうです。
138
-
139
70
  当然ググります(日本語ではいまいちだったので英語)
140
-
141
71
  [java 2nd dimension array reverse - Google 検索](https://www.google.com/search?q=java+2nd+dimension+array+reverse)
142
-
143
72
  [java - Reverse the rows of a 2d array - Stack Overflow](https://stackoverflow.com/questions/21920939/reverse-the-rows-of-a-2d-array)
144
73
 
145
-
146
-
147
74
  ```Java
148
-
149
75
  public class Sample {
150
-
151
76
  public static void main(String[] args) {
152
-
153
77
  int[][] array = getSpiralMatrix(5);
154
78
 
155
-
156
-
157
79
  // 真ん中始まり
158
-
159
80
  for (int y = 0; y < array.length; y++) {
160
-
161
81
  for (int x = 0; x < array[y].length; x++) {
162
-
163
82
  array[y][x] = (5 * 5 + 1) - array[y][x];
164
-
165
- }
83
+ }
166
-
167
- }
84
+ }
168
-
169
-
170
85
 
171
86
  // 左右反転
172
-
173
87
  // [java - Reverse the rows of a 2d array - Stack Overflow](https://stackoverflow.com/questions/21920939/reverse-the-rows-of-a-2d-array)
174
-
175
88
  for (int j = 0; j < array.length; j++) {
176
-
177
89
  for (int i = 0; i < array[j].length / 2; i++) {
178
-
179
90
  int temp = array[j][i];
180
-
181
91
  array[j][i] = array[j][array[j].length - i - 1];
182
-
183
92
  array[j][array[j].length - i - 1] = temp;
184
-
185
- }
93
+ }
186
-
187
- }
94
+ }
188
-
189
-
190
95
 
191
96
  for (int[] a : array) {
192
-
193
97
  for (int i : a) {
194
-
195
98
  System.out.printf("%3d", i);
196
-
197
- }
99
+ }
198
-
199
100
  System.out.println();
200
-
201
- }
101
+ }
202
-
203
- }
102
+ }
204
-
205
-
206
103
 
207
104
  private static int[][] getSpiralMatrix(int n) { /* 同じなので省略 */ }
208
-
209
- }
105
+ }
210
-
211
- ```
106
+ ```
212
-
213
-
214
-
107
+
215
- ```
108
+ ```
216
-
217
109
  21 22 23 24 25
218
-
219
110
  20 7 8 9 10
220
-
221
111
  19 6 1 2 11
222
-
223
112
  18 5 4 3 12
224
-
225
113
  17 16 15 14 13
226
-
227
- ```
114
+ ```
228
-
229
-
230
115
 
231
116
  ---
232
117
 
233
-
234
-
235
118
  上記を超ふまえたうえで、GUIにするとこうなりました^^;
236
-
237
119
  この2次元配列を種にして(xyや番号はわかっている)、マスのクラス(`Cell`)を生成します。
238
120
 
239
-
240
-
241
121
  あくまでアウトラインですが、部品は揃ってると思いますので参考にしていただけたらと思います^^
242
122
 
243
-
244
-
245
123
  ```Java
246
-
247
124
  import java.awt.BasicStroke;
248
-
249
125
  import java.awt.Color;
250
-
251
126
  import java.awt.Dimension;
252
-
253
127
  import java.awt.Graphics;
254
-
255
128
  import java.awt.Graphics2D;
256
-
257
129
  import java.awt.Rectangle;
258
-
259
130
  import java.awt.Stroke;
260
-
261
131
  import java.util.ArrayList;
262
-
263
132
  import javax.swing.JButton;
264
-
265
133
  import javax.swing.JFrame;
266
-
267
134
  import javax.swing.JPanel;
268
135
 
269
136
 
270
-
271
-
272
-
273
137
  public class GameWindow extends JFrame {
274
-
275
138
  public static void main(String[] args) {
276
-
277
139
  new GameWindow().setVisible(true);
278
-
279
- }
140
+ }
280
-
281
-
282
141
 
283
142
  public GameWindow() {
284
-
285
143
  DrawCanvas panel = new DrawCanvas();
286
-
287
144
  // setSize(550, 600); // JFrame のサイズはタイトルバー・枠も含むので微妙にずれる
288
-
289
145
  panel.setPreferredSize(new Dimension(550, 600)); // 中身の推奨サイズを設定
290
-
291
146
  add(panel);
292
-
293
147
  pack();
294
148
 
295
-
296
-
297
149
  setResizable(false);
298
-
299
150
  setLocationRelativeTo(null);
300
-
301
151
  setDefaultCloseOperation(EXIT_ON_CLOSE);
302
-
303
- }
152
+ }
304
-
305
- }
153
+ }
306
-
307
-
308
154
 
309
155
  class Cell {
310
-
311
156
  int no;
312
-
313
157
  Rectangle rect;
314
-
315
158
  Color color;
316
159
 
317
-
318
-
319
160
  Cell(int no, Rectangle rect, Color color) {
320
-
321
161
  this.no = no;
322
-
323
162
  this.rect = rect;
324
-
325
163
  this.color = color;
326
-
327
- }
164
+ }
328
-
329
-
330
165
 
331
166
  void draw(Graphics2D g2) {
332
-
333
167
  g2.setColor(color);
334
-
335
168
  g2.fill(rect);
336
-
337
169
  g2.setColor(Color.BLACK);
338
-
339
170
  g2.draw(rect);
340
-
341
171
  g2.drawString(String.valueOf(no), rect.x + 5, rect.y + 15);
342
-
343
- }
172
+ }
344
-
345
- }
173
+ }
346
-
347
-
348
174
 
349
175
  class DrawCanvas extends JPanel {
350
-
351
176
  private final int[][] array = getSpiralMatrix(9);
352
-
353
177
  private final ArrayList<Cell> cells = new ArrayList<>();
354
178
 
355
-
356
-
357
179
  private int current = 1;
358
180
 
359
-
360
-
361
181
  DrawCanvas() {
362
-
363
182
  initialize();
364
183
 
365
-
366
-
367
184
  JButton go = new JButton("go");
368
-
369
185
  go.addActionListener(e -> { current++; repaint(); });
370
-
371
186
  add(go);
372
187
 
373
-
374
-
375
188
  JButton back = new JButton("back");
376
-
377
189
  back.addActionListener(e -> { current--; repaint(); });
378
-
379
190
  add(back);
380
191
 
381
-
382
-
383
192
  JButton rotation90 = new JButton("rotation90");
384
-
385
193
  rotation90.addActionListener(e -> { rotation(); repaint(); });
386
-
387
194
  add(rotation90);
388
195
 
389
-
390
-
391
196
  JButton rotation180 = new JButton("rotation180");
392
-
393
197
  rotation180.addActionListener(e -> { rotation(); rotation(); repaint(); });
394
-
395
198
  add(rotation180);
396
-
397
- }
199
+ }
398
-
399
-
400
200
 
401
201
  private void initialize() {
402
-
403
202
  for (int y = 0; y < array.length; y++) {
404
-
405
203
  for (int x = 0; x < array[y].length; x++) {
406
-
407
204
  array[y][x] = (array.length * array.length + 1) - array[y][x];
408
-
409
- }
205
+ }
410
-
411
- }
206
+ }
412
-
413
207
  // [java - Reverse the rows of a 2d array - Stack Overflow](https://stackoverflow.com/questions/21920939/reverse-the-rows-of-a-2d-array)
414
-
415
208
  for (int j = 0; j < array.length; j++) {
416
-
417
209
  for (int i = 0; i < array[j].length / 2; i++) {
418
-
419
210
  int temp = array[j][i];
420
-
421
211
  array[j][i] = array[j][array[j].length - i - 1];
422
-
423
212
  array[j][array[j].length - i - 1] = temp;
424
-
425
- }
213
+ }
426
-
427
- }
214
+ }
428
-
429
-
430
215
 
431
216
  // array を種にして Cell のリストを作る
432
-
433
217
  Color c = new Color(0, 170, 0);
434
-
435
218
  for (int y = 0; y < array.length; y++) {
436
-
437
219
  for (int x = 0; x < array[y].length; x++) {
438
-
439
220
  Rectangle r = new Rectangle(x * 50 + 50, y * 50 + 100, 50, 50);
440
-
441
221
  cells.add(new Cell(array[y][x], r, c));
442
-
443
- }
222
+ }
444
-
445
- }
223
+ }
446
-
447
224
  // 番号順に並び変えれば点線を描きやすい
448
-
449
225
  cells.sort((a, b) -> a.no - b.no);
450
-
451
- }
226
+ }
452
-
453
-
454
227
 
455
228
  private static final BasicStroke dashed = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1, new float[]{ 5 }, 0);
456
229
 
457
-
458
-
459
230
  public void paintComponent(Graphics g) {
460
-
461
231
  super.paintComponent(g);
462
232
 
463
-
464
-
465
233
  Graphics2D g2 = (Graphics2D) g;
466
-
467
234
  Stroke oldStroke = g2.getStroke();
468
235
 
469
-
470
-
471
236
  Cell before = null;
472
-
473
237
  for (Cell cell : cells) {
474
-
475
238
  cell.draw(g2);
476
239
 
477
-
478
-
479
240
  // 前のセルとの間に点線を引く
480
-
481
241
  if (before != null) {
482
-
483
242
  g2.setStroke(dashed);
484
-
485
243
  int x1 = before.rect.x + before.rect.width / 2;
486
-
487
244
  int y1 = before.rect.y + before.rect.height / 2;
488
-
489
245
  int x2 = cell.rect.x + cell.rect.width / 2;
490
-
491
246
  int y2 = cell.rect.y + cell.rect.height / 2;
492
-
493
247
  g2.drawLine(x1, y1, x2, y2);
494
-
495
248
  g2.setStroke(oldStroke);
496
-
497
- }
249
+ }
498
-
499
250
  before = cell;
500
251
 
501
-
502
-
503
252
  if (current == cell.no) {
504
-
505
253
  g2.setColor(Color.RED);
506
-
507
254
  Rectangle rect = cell.rect.getBounds();
508
-
509
255
  rect.grow(-15, -15); // 一回り小さい四角に
510
-
511
256
  g2.fill(rect);
512
-
513
- }
257
+ }
514
-
515
- }
258
+ }
516
-
517
- }
259
+ }
518
-
519
-
520
260
 
521
261
  // current を右90度回転 2次元配列の回転を応用(正方形のみ)
522
-
523
262
  private void rotation() {
524
-
525
263
  for (int j = 0; j < array.length; j++) {
526
-
527
264
  for (int i = 0; i < array[j].length; i++) {
528
-
529
265
  if (current == array[j][i]) {
530
-
531
266
  current = array[i][array.length - j - 1];
532
-
533
267
  return;
534
-
535
268
  }
536
-
537
- }
269
+ }
538
-
539
- }
270
+ }
540
-
541
- }
271
+ }
542
-
543
-
544
272
 
545
273
  // [How To Create Spiral Of Numbers (Spiral Matrix) In Java?](https://javaconceptoftheday.com/how-to-create-spiral-of-numbers-matrix-in-java/)
546
-
547
274
  private int[][] getSpiralMatrix(int n) {
548
-
549
275
  int[][] spiral = new int[n][n];
550
-
551
276
  int value = 1;
552
-
553
277
  int minCol = 0;
554
-
555
278
  int minRow = 0;
556
-
557
279
  int maxCol = n - 1;
558
-
559
280
  int maxRow = n - 1;
560
281
 
561
-
562
-
563
282
  while (value <= n * n) {
564
-
565
283
  for (int i = minCol; i <= maxCol; i++) {
566
-
567
284
  spiral[minRow][i] = value++;
568
-
569
- }
285
+ }
570
-
571
286
  for (int i = minRow + 1; i <= maxRow; i++) {
572
-
573
287
  spiral[i][maxCol] = value++;
574
-
575
- }
288
+ }
576
-
577
289
  for (int i = maxCol - 1; i >= minCol; i--) {
578
-
579
290
  spiral[maxRow][i] = value++;
580
-
581
- }
291
+ }
582
-
583
292
  for (int i = maxRow - 1; i >= minRow + 1; i--) {
584
-
585
293
  spiral[i][minCol] = value++;
586
-
587
- }
294
+ }
588
-
589
295
  minCol++; minRow++; maxCol--; maxRow--;
590
-
591
- }
296
+ }
592
-
593
-
594
297
 
595
298
  return spiral;
596
-
597
- }
299
+ }
598
-
599
- }
300
+ }
600
-
601
- ```
301
+ ```
302
+ ![アプリ画像](https://ddjkaamml8q8x.cloudfront.net/questions/2023-07-29/0c5498f1-939a-4b88-8bc6-aef62d072541.png)

1

(自分の意図と)逆だった^^;

2021/09/07 09:39

投稿

TN8001
TN8001

スコア9350

test CHANGED
@@ -446,7 +446,7 @@
446
446
 
447
447
  // 番号順に並び変えれば点線を描きやすい
448
448
 
449
- cells.sort((a, b) -> b.no - a.no);
449
+ cells.sort((a, b) -> a.no - b.no);
450
450
 
451
451
  }
452
452