回答編集履歴
2
見直しキャンペーン中
    
        answer	
    CHANGED
    
    | @@ -1,301 +1,302 @@ | |
| 1 | 
            -
            一気に編集されすぎて意味が分からないうえ文法エラーになっているので、当初のコードだとして回答します(長すぎるのもあれですが、短すぎはもっとイヤですね)
         | 
| 2 | 
            -
             | 
| 3 | 
            -
            yuki_javaさん自身も力技だと感じておられるようですが、どうせならスマートに解決したいですよね。
         | 
| 4 | 
            -
             | 
| 5 | 
            -
            今回のポイントは明らかに渦巻ですよね?
         | 
| 6 | 
            -
            課題か何かで「渦巻の数字を出力せよ」みたいなものもありそうですし、ヒントがないか検索してみます。
         | 
| 7 | 
            -
             | 
| 8 | 
            -
            「[java 数字 渦巻](https://www.google.co.jp/search?q=java+%E6%95%B0%E5%AD%97+%E6%B8%A6%E5%B7%BB)」で検索したらこちらが出ました。
         | 
| 9 | 
            -
            [2次元配列 に数値を並べる](http://miztools.so.land.to/java-aa/swing1_4/puzzle_array.html#rot_r)
         | 
| 10 | 
            -
             | 
| 11 | 
            -
            英語も直訳で「[java number swirl](https://www.google.com/search?q=java+number+swirl)」としたらこちらが出ました(Spiralなんですね~^^;
         | 
| 12 | 
            -
            [How To Create Spiral Of Numbers (Spiral Matrix) In Java?](https://javaconceptoftheday.com/how-to-create-spiral-of-numbers-matrix-in-java/)
         | 
| 13 | 
            -
             | 
| 14 | 
            -
            どちらも何をやっているかさっぱりわかりませんが、今回はxyの並びが好みな後者にしました^^;
         | 
| 15 | 
            -
            メソッドに切り出し、コンソール出力で動作確認をしてみましょう。
         | 
| 16 | 
            -
            ```Java
         | 
| 17 | 
            -
            public class Sample {
         | 
| 18 | 
            -
                public static void main(String[] args) {
         | 
| 19 | 
            -
                    int[][] array = getSpiralMatrix(5);
         | 
| 20 | 
            -
             | 
| 21 | 
            -
                    for (int[] a : array) {
         | 
| 22 | 
            -
                        for (int i : a) {
         | 
| 23 | 
            -
                            System.out.printf("%3d", i);
         | 
| 24 | 
            -
                        }
         | 
| 25 | 
            -
                        System.out.println();
         | 
| 26 | 
            -
                    }
         | 
| 27 | 
            -
                }
         | 
| 28 | 
            -
             | 
| 29 | 
            -
                // [How To Create Spiral Of Numbers (Spiral Matrix) In Java?](https://javaconceptoftheday.com/how-to-create-spiral-of-numbers-matrix-in-java/)
         | 
| 30 | 
            -
                private static int[][] getSpiralMatrix(int n) {
         | 
| 31 | 
            -
                    int[][] spiral = new int[n][n];
         | 
| 32 | 
            -
                    int value = 1;
         | 
| 33 | 
            -
                    int minCol = 0;
         | 
| 34 | 
            -
                    int minRow = 0;
         | 
| 35 | 
            -
                    int maxCol = n - 1;
         | 
| 36 | 
            -
                    int maxRow = n - 1;
         | 
| 37 | 
            -
             | 
| 38 | 
            -
                    while (value <= n * n) {
         | 
| 39 | 
            -
                        for (int i = minCol; i <= maxCol; i++) {
         | 
| 40 | 
            -
                            spiral[minRow][i] = value++;
         | 
| 41 | 
            -
                        }
         | 
| 42 | 
            -
                        for (int i = minRow + 1; i <= maxRow; i++) {
         | 
| 43 | 
            -
                            spiral[i][maxCol] = value++;
         | 
| 44 | 
            -
                        }
         | 
| 45 | 
            -
                        for (int i = maxCol - 1; i >= minCol; i--) {
         | 
| 46 | 
            -
                            spiral[maxRow][i] = value++;
         | 
| 47 | 
            -
                        }
         | 
| 48 | 
            -
                        for (int i = maxRow - 1; i >= minRow + 1; i--) {
         | 
| 49 | 
            -
                            spiral[i][minCol] = value++;
         | 
| 50 | 
            -
                        }
         | 
| 51 | 
            -
                        minCol++; minRow++; maxCol--; maxRow--;
         | 
| 52 | 
            -
                    }
         | 
| 53 | 
            -
             | 
| 54 | 
            -
                    return spiral;
         | 
| 55 | 
            -
                }
         | 
| 56 | 
            -
            }
         | 
| 57 | 
            -
            ```
         | 
| 58 | 
            -
             | 
| 59 | 
            -
            ```
         | 
| 60 | 
            -
              1  2  3  4  5
         | 
| 61 | 
            -
             16 17 18 19  6
         | 
| 62 | 
            -
             15 24 25 20  7
         | 
| 63 | 
            -
             14 23 22 21  8
         | 
| 64 | 
            -
             13 12 11 10  9
         | 
| 65 | 
            -
            ```
         | 
| 66 | 
            -
             | 
| 67 | 
            -
            左上から時計回りに数字が増えていくようですね。
         | 
| 68 | 
            -
             | 
| 69 | 
            -
            真ん中を1にするのは簡単ですね。そのうえで左右反転すればよさそうです。
         | 
| 70 | 
            -
            当然ググります(日本語ではいまいちだったので英語)
         | 
| 71 | 
            -
            [java 2nd dimension array reverse - Google 検索](https://www.google.com/search?q=java+2nd+dimension+array+reverse)
         | 
| 72 | 
            -
            [java - Reverse the rows of a 2d array - Stack Overflow](https://stackoverflow.com/questions/21920939/reverse-the-rows-of-a-2d-array)
         | 
| 73 | 
            -
             | 
| 74 | 
            -
            ```Java
         | 
| 75 | 
            -
            public class Sample {
         | 
| 76 | 
            -
                public static void main(String[] args) {
         | 
| 77 | 
            -
                    int[][] array = getSpiralMatrix(5);
         | 
| 78 | 
            -
             | 
| 79 | 
            -
                    // 真ん中始まり
         | 
| 80 | 
            -
                    for (int y = 0; y < array.length; y++) {
         | 
| 81 | 
            -
                        for (int x = 0; x < array[y].length; x++) {
         | 
| 82 | 
            -
                            array[y][x] = (5 * 5 + 1) - array[y][x];
         | 
| 83 | 
            -
                        }
         | 
| 84 | 
            -
                    }
         | 
| 85 | 
            -
             | 
| 86 | 
            -
                    // 左右反転
         | 
| 87 | 
            -
                    // [java - Reverse the rows of a 2d array - Stack Overflow](https://stackoverflow.com/questions/21920939/reverse-the-rows-of-a-2d-array)
         | 
| 88 | 
            -
                    for (int j = 0; j < array.length; j++) {
         | 
| 89 | 
            -
                        for (int i = 0; i < array[j].length / 2; i++) {
         | 
| 90 | 
            -
                            int temp = array[j][i];
         | 
| 91 | 
            -
                            array[j][i] = array[j][array[j].length - i - 1];
         | 
| 92 | 
            -
                            array[j][array[j].length - i - 1] = temp;
         | 
| 93 | 
            -
                        }
         | 
| 94 | 
            -
                    }
         | 
| 95 | 
            -
             | 
| 96 | 
            -
                    for (int[] a : array) {
         | 
| 97 | 
            -
                        for (int i : a) {
         | 
| 98 | 
            -
                            System.out.printf("%3d", i);
         | 
| 99 | 
            -
                        }
         | 
| 100 | 
            -
                        System.out.println();
         | 
| 101 | 
            -
                    }
         | 
| 102 | 
            -
                }
         | 
| 103 | 
            -
             | 
| 104 | 
            -
                private static int[][] getSpiralMatrix(int n) { /* 同じなので省略 */ }
         | 
| 105 | 
            -
            }
         | 
| 106 | 
            -
            ```
         | 
| 107 | 
            -
             | 
| 108 | 
            -
            ```
         | 
| 109 | 
            -
             21 22 23 24 25
         | 
| 110 | 
            -
             20  7  8  9 10
         | 
| 111 | 
            -
             19  6  1  2 11
         | 
| 112 | 
            -
             18  5  4  3 12
         | 
| 113 | 
            -
             17 16 15 14 13
         | 
| 114 | 
            -
            ```
         | 
| 115 | 
            -
             | 
| 116 | 
            -
            ---
         | 
| 117 | 
            -
             | 
| 118 | 
            -
            上記を超ふまえたうえで、GUIにするとこうなりました^^;
         | 
| 119 | 
            -
            この2次元配列を種にして(xyや番号はわかっている)、マスのクラス(`Cell`)を生成します。
         | 
| 120 | 
            -
             | 
| 121 | 
            -
            あくまでアウトラインですが、部品は揃ってると思いますので参考にしていただけたらと思います^^
         | 
| 122 | 
            -
             | 
| 123 | 
            -
            ```Java
         | 
| 124 | 
            -
            import java.awt.BasicStroke;
         | 
| 125 | 
            -
            import java.awt.Color;
         | 
| 126 | 
            -
            import java.awt.Dimension;
         | 
| 127 | 
            -
            import java.awt.Graphics;
         | 
| 128 | 
            -
            import java.awt.Graphics2D;
         | 
| 129 | 
            -
            import java.awt.Rectangle;
         | 
| 130 | 
            -
            import java.awt.Stroke;
         | 
| 131 | 
            -
            import java.util.ArrayList;
         | 
| 132 | 
            -
            import javax.swing.JButton;
         | 
| 133 | 
            -
            import javax.swing.JFrame;
         | 
| 134 | 
            -
            import javax.swing.JPanel;
         | 
| 135 | 
            -
             | 
| 136 | 
            -
             | 
| 137 | 
            -
            public class GameWindow extends JFrame {
         | 
| 138 | 
            -
                public static void main(String[] args) {
         | 
| 139 | 
            -
                    new GameWindow().setVisible(true);
         | 
| 140 | 
            -
                }
         | 
| 141 | 
            -
             | 
| 142 | 
            -
                public GameWindow() {
         | 
| 143 | 
            -
                    DrawCanvas panel = new DrawCanvas();
         | 
| 144 | 
            -
            //        setSize(550, 600); // JFrame のサイズはタイトルバー・枠も含むので微妙にずれる
         | 
| 145 | 
            -
                    panel.setPreferredSize(new Dimension(550, 600)); // 中身の推奨サイズを設定
         | 
| 146 | 
            -
                    add(panel);
         | 
| 147 | 
            -
                    pack();
         | 
| 148 | 
            -
             | 
| 149 | 
            -
                    setResizable(false);
         | 
| 150 | 
            -
                    setLocationRelativeTo(null);
         | 
| 151 | 
            -
                    setDefaultCloseOperation(EXIT_ON_CLOSE);
         | 
| 152 | 
            -
                }
         | 
| 153 | 
            -
            }
         | 
| 154 | 
            -
             | 
| 155 | 
            -
            class Cell {
         | 
| 156 | 
            -
                int no;
         | 
| 157 | 
            -
                Rectangle rect;
         | 
| 158 | 
            -
                Color color;
         | 
| 159 | 
            -
             | 
| 160 | 
            -
                Cell(int no, Rectangle rect, Color color) {
         | 
| 161 | 
            -
                    this.no = no;
         | 
| 162 | 
            -
                    this.rect = rect;
         | 
| 163 | 
            -
                    this.color = color;
         | 
| 164 | 
            -
                }
         | 
| 165 | 
            -
             | 
| 166 | 
            -
                void draw(Graphics2D g2) {
         | 
| 167 | 
            -
                    g2.setColor(color);
         | 
| 168 | 
            -
                    g2.fill(rect);
         | 
| 169 | 
            -
                    g2.setColor(Color.BLACK);
         | 
| 170 | 
            -
                    g2.draw(rect);
         | 
| 171 | 
            -
                    g2.drawString(String.valueOf(no), rect.x + 5, rect.y + 15);
         | 
| 172 | 
            -
                }
         | 
| 173 | 
            -
            }
         | 
| 174 | 
            -
             | 
| 175 | 
            -
            class DrawCanvas extends JPanel {
         | 
| 176 | 
            -
                private final int[][] array = getSpiralMatrix(9);
         | 
| 177 | 
            -
                private final ArrayList<Cell> cells = new ArrayList<>();
         | 
| 178 | 
            -
             | 
| 179 | 
            -
                private int current = 1;
         | 
| 180 | 
            -
             | 
| 181 | 
            -
                DrawCanvas() {
         | 
| 182 | 
            -
                    initialize();
         | 
| 183 | 
            -
             | 
| 184 | 
            -
                    JButton go = new JButton("go");
         | 
| 185 | 
            -
                    go.addActionListener(e -> { current++; repaint(); });
         | 
| 186 | 
            -
                    add(go);
         | 
| 187 | 
            -
             | 
| 188 | 
            -
                    JButton back = new JButton("back");
         | 
| 189 | 
            -
                    back.addActionListener(e -> { current--; repaint(); });
         | 
| 190 | 
            -
                    add(back);
         | 
| 191 | 
            -
             | 
| 192 | 
            -
                    JButton rotation90 = new JButton("rotation90");
         | 
| 193 | 
            -
                    rotation90.addActionListener(e -> { rotation(); repaint(); });
         | 
| 194 | 
            -
                    add(rotation90);
         | 
| 195 | 
            -
             | 
| 196 | 
            -
                    JButton rotation180 = new JButton("rotation180");
         | 
| 197 | 
            -
                    rotation180.addActionListener(e -> { rotation(); rotation(); repaint(); });
         | 
| 198 | 
            -
                    add(rotation180);
         | 
| 199 | 
            -
                }
         | 
| 200 | 
            -
             | 
| 201 | 
            -
                private void initialize() {
         | 
| 202 | 
            -
                    for (int y = 0; y < array.length; y++) {
         | 
| 203 | 
            -
                        for (int x = 0; x < array[y].length; x++) {
         | 
| 204 | 
            -
                            array[y][x] = (array.length * array.length + 1) - array[y][x];
         | 
| 205 | 
            -
                        }
         | 
| 206 | 
            -
                    }
         | 
| 207 | 
            -
                    // [java - Reverse the rows of a 2d array - Stack Overflow](https://stackoverflow.com/questions/21920939/reverse-the-rows-of-a-2d-array)
         | 
| 208 | 
            -
                    for (int j = 0; j < array.length; j++) {
         | 
| 209 | 
            -
                        for (int i = 0; i < array[j].length / 2; i++) {
         | 
| 210 | 
            -
                            int temp = array[j][i];
         | 
| 211 | 
            -
                            array[j][i] = array[j][array[j].length - i - 1];
         | 
| 212 | 
            -
                            array[j][array[j].length - i - 1] = temp;
         | 
| 213 | 
            -
                        }
         | 
| 214 | 
            -
                    }
         | 
| 215 | 
            -
             | 
| 216 | 
            -
                    // array を種にして Cell のリストを作る
         | 
| 217 | 
            -
                    Color c = new Color(0, 170, 0);
         | 
| 218 | 
            -
                    for (int y = 0; y < array.length; y++) {
         | 
| 219 | 
            -
                        for (int x = 0; x < array[y].length; x++) {
         | 
| 220 | 
            -
                            Rectangle r = new Rectangle(x * 50 + 50, y * 50 + 100, 50, 50);
         | 
| 221 | 
            -
                            cells.add(new Cell(array[y][x], r, c));
         | 
| 222 | 
            -
                        }
         | 
| 223 | 
            -
                    }
         | 
| 224 | 
            -
                    // 番号順に並び変えれば点線を描きやすい
         | 
| 225 | 
            -
                    cells.sort((a, b) -> a.no - b.no);
         | 
| 226 | 
            -
                }
         | 
| 227 | 
            -
             | 
| 228 | 
            -
                private static final BasicStroke dashed = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1, new float[]{ 5 }, 0);
         | 
| 229 | 
            -
             | 
| 230 | 
            -
                public void paintComponent(Graphics g) {
         | 
| 231 | 
            -
                    super.paintComponent(g);
         | 
| 232 | 
            -
             | 
| 233 | 
            -
                    Graphics2D g2 = (Graphics2D) g;
         | 
| 234 | 
            -
                    Stroke oldStroke = g2.getStroke();
         | 
| 235 | 
            -
             | 
| 236 | 
            -
                    Cell before = null;
         | 
| 237 | 
            -
                    for (Cell cell : cells) {
         | 
| 238 | 
            -
                        cell.draw(g2);
         | 
| 239 | 
            -
             | 
| 240 | 
            -
                        // 前のセルとの間に点線を引く
         | 
| 241 | 
            -
                        if (before != null) {
         | 
| 242 | 
            -
                            g2.setStroke(dashed);
         | 
| 243 | 
            -
                            int x1 = before.rect.x + before.rect.width / 2;
         | 
| 244 | 
            -
                            int y1 = before.rect.y + before.rect.height / 2;
         | 
| 245 | 
            -
                            int x2 = cell.rect.x + cell.rect.width / 2;
         | 
| 246 | 
            -
                            int y2 = cell.rect.y + cell.rect.height / 2;
         | 
| 247 | 
            -
                            g2.drawLine(x1, y1, x2, y2);
         | 
| 248 | 
            -
                            g2.setStroke(oldStroke);
         | 
| 249 | 
            -
                        }
         | 
| 250 | 
            -
                        before = cell;
         | 
| 251 | 
            -
             | 
| 252 | 
            -
                        if (current == cell.no) {
         | 
| 253 | 
            -
                            g2.setColor(Color.RED);
         | 
| 254 | 
            -
                            Rectangle rect = cell.rect.getBounds();
         | 
| 255 | 
            -
                            rect.grow(-15, -15); // 一回り小さい四角に
         | 
| 256 | 
            -
                            g2.fill(rect);
         | 
| 257 | 
            -
                        }
         | 
| 258 | 
            -
                    }
         | 
| 259 | 
            -
                }
         | 
| 260 | 
            -
             | 
| 261 | 
            -
                // current を右90度回転 2次元配列の回転を応用(正方形のみ)
         | 
| 262 | 
            -
                private void rotation() {
         | 
| 263 | 
            -
                    for (int j = 0; j < array.length; j++) {
         | 
| 264 | 
            -
                        for (int i = 0; i < array[j].length; i++) {
         | 
| 265 | 
            -
                            if (current == array[j][i]) {
         | 
| 266 | 
            -
                                current = array[i][array.length - j - 1];
         | 
| 267 | 
            -
                                return;
         | 
| 268 | 
            -
                            }
         | 
| 269 | 
            -
                        }
         | 
| 270 | 
            -
                    }
         | 
| 271 | 
            -
                }
         | 
| 272 | 
            -
             | 
| 273 | 
            -
                // [How To Create Spiral Of Numbers (Spiral Matrix) In Java?](https://javaconceptoftheday.com/how-to-create-spiral-of-numbers-matrix-in-java/)
         | 
| 274 | 
            -
                private int[][] getSpiralMatrix(int n) {
         | 
| 275 | 
            -
                    int[][] spiral = new int[n][n];
         | 
| 276 | 
            -
                    int value = 1;
         | 
| 277 | 
            -
                    int minCol = 0;
         | 
| 278 | 
            -
                    int minRow = 0;
         | 
| 279 | 
            -
                    int maxCol = n - 1;
         | 
| 280 | 
            -
                    int maxRow = n - 1;
         | 
| 281 | 
            -
             | 
| 282 | 
            -
                    while (value <= n * n) {
         | 
| 283 | 
            -
                        for (int i = minCol; i <= maxCol; i++) {
         | 
| 284 | 
            -
                            spiral[minRow][i] = value++;
         | 
| 285 | 
            -
                        }
         | 
| 286 | 
            -
                        for (int i = minRow + 1; i <= maxRow; i++) {
         | 
| 287 | 
            -
                            spiral[i][maxCol] = value++;
         | 
| 288 | 
            -
                        }
         | 
| 289 | 
            -
                        for (int i = maxCol - 1; i >= minCol; i--) {
         | 
| 290 | 
            -
                            spiral[maxRow][i] = value++;
         | 
| 291 | 
            -
                        }
         | 
| 292 | 
            -
                        for (int i = maxRow - 1; i >= minRow + 1; i--) {
         | 
| 293 | 
            -
                            spiral[i][minCol] = value++;
         | 
| 294 | 
            -
                        }
         | 
| 295 | 
            -
                        minCol++; minRow++; maxCol--; maxRow--;
         | 
| 296 | 
            -
                    }
         | 
| 297 | 
            -
             | 
| 298 | 
            -
                    return spiral;
         | 
| 299 | 
            -
                }
         | 
| 300 | 
            -
            }
         | 
| 301 | 
            -
            ```
         | 
| 1 | 
            +
            一気に編集されすぎて意味が分からないうえ文法エラーになっているので、当初のコードだとして回答します(長すぎるのもあれですが、短すぎはもっとイヤですね)
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            yuki_javaさん自身も力技だと感じておられるようですが、どうせならスマートに解決したいですよね。
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            今回のポイントは明らかに渦巻ですよね?
         | 
| 6 | 
            +
            課題か何かで「渦巻の数字を出力せよ」みたいなものもありそうですし、ヒントがないか検索してみます。
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            「[java 数字 渦巻](https://www.google.co.jp/search?q=java+%E6%95%B0%E5%AD%97+%E6%B8%A6%E5%B7%BB)」で検索したらこちらが出ました。
         | 
| 9 | 
            +
            [2次元配列 に数値を並べる](http://miztools.so.land.to/java-aa/swing1_4/puzzle_array.html#rot_r)
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            英語も直訳で「[java number swirl](https://www.google.com/search?q=java+number+swirl)」としたらこちらが出ました(Spiralなんですね~^^;
         | 
| 12 | 
            +
            [How To Create Spiral Of Numbers (Spiral Matrix) In Java?](https://javaconceptoftheday.com/how-to-create-spiral-of-numbers-matrix-in-java/)
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            どちらも何をやっているかさっぱりわかりませんが、今回はxyの並びが好みな後者にしました^^;
         | 
| 15 | 
            +
            メソッドに切り出し、コンソール出力で動作確認をしてみましょう。
         | 
| 16 | 
            +
            ```Java
         | 
| 17 | 
            +
            public class Sample {
         | 
| 18 | 
            +
                public static void main(String[] args) {
         | 
| 19 | 
            +
                    int[][] array = getSpiralMatrix(5);
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                    for (int[] a : array) {
         | 
| 22 | 
            +
                        for (int i : a) {
         | 
| 23 | 
            +
                            System.out.printf("%3d", i);
         | 
| 24 | 
            +
                        }
         | 
| 25 | 
            +
                        System.out.println();
         | 
| 26 | 
            +
                    }
         | 
| 27 | 
            +
                }
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                // [How To Create Spiral Of Numbers (Spiral Matrix) In Java?](https://javaconceptoftheday.com/how-to-create-spiral-of-numbers-matrix-in-java/)
         | 
| 30 | 
            +
                private static int[][] getSpiralMatrix(int n) {
         | 
| 31 | 
            +
                    int[][] spiral = new int[n][n];
         | 
| 32 | 
            +
                    int value = 1;
         | 
| 33 | 
            +
                    int minCol = 0;
         | 
| 34 | 
            +
                    int minRow = 0;
         | 
| 35 | 
            +
                    int maxCol = n - 1;
         | 
| 36 | 
            +
                    int maxRow = n - 1;
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                    while (value <= n * n) {
         | 
| 39 | 
            +
                        for (int i = minCol; i <= maxCol; i++) {
         | 
| 40 | 
            +
                            spiral[minRow][i] = value++;
         | 
| 41 | 
            +
                        }
         | 
| 42 | 
            +
                        for (int i = minRow + 1; i <= maxRow; i++) {
         | 
| 43 | 
            +
                            spiral[i][maxCol] = value++;
         | 
| 44 | 
            +
                        }
         | 
| 45 | 
            +
                        for (int i = maxCol - 1; i >= minCol; i--) {
         | 
| 46 | 
            +
                            spiral[maxRow][i] = value++;
         | 
| 47 | 
            +
                        }
         | 
| 48 | 
            +
                        for (int i = maxRow - 1; i >= minRow + 1; i--) {
         | 
| 49 | 
            +
                            spiral[i][minCol] = value++;
         | 
| 50 | 
            +
                        }
         | 
| 51 | 
            +
                        minCol++; minRow++; maxCol--; maxRow--;
         | 
| 52 | 
            +
                    }
         | 
| 53 | 
            +
             | 
| 54 | 
            +
                    return spiral;
         | 
| 55 | 
            +
                }
         | 
| 56 | 
            +
            }
         | 
| 57 | 
            +
            ```
         | 
| 58 | 
            +
             | 
| 59 | 
            +
            ```
         | 
| 60 | 
            +
              1  2  3  4  5
         | 
| 61 | 
            +
             16 17 18 19  6
         | 
| 62 | 
            +
             15 24 25 20  7
         | 
| 63 | 
            +
             14 23 22 21  8
         | 
| 64 | 
            +
             13 12 11 10  9
         | 
| 65 | 
            +
            ```
         | 
| 66 | 
            +
             | 
| 67 | 
            +
            左上から時計回りに数字が増えていくようですね。
         | 
| 68 | 
            +
             | 
| 69 | 
            +
            真ん中を1にするのは簡単ですね。そのうえで左右反転すればよさそうです。
         | 
| 70 | 
            +
            当然ググります(日本語ではいまいちだったので英語)
         | 
| 71 | 
            +
            [java 2nd dimension array reverse - Google 検索](https://www.google.com/search?q=java+2nd+dimension+array+reverse)
         | 
| 72 | 
            +
            [java - Reverse the rows of a 2d array - Stack Overflow](https://stackoverflow.com/questions/21920939/reverse-the-rows-of-a-2d-array)
         | 
| 73 | 
            +
             | 
| 74 | 
            +
            ```Java
         | 
| 75 | 
            +
            public class Sample {
         | 
| 76 | 
            +
                public static void main(String[] args) {
         | 
| 77 | 
            +
                    int[][] array = getSpiralMatrix(5);
         | 
| 78 | 
            +
             | 
| 79 | 
            +
                    // 真ん中始まり
         | 
| 80 | 
            +
                    for (int y = 0; y < array.length; y++) {
         | 
| 81 | 
            +
                        for (int x = 0; x < array[y].length; x++) {
         | 
| 82 | 
            +
                            array[y][x] = (5 * 5 + 1) - array[y][x];
         | 
| 83 | 
            +
                        }
         | 
| 84 | 
            +
                    }
         | 
| 85 | 
            +
             | 
| 86 | 
            +
                    // 左右反転
         | 
| 87 | 
            +
                    // [java - Reverse the rows of a 2d array - Stack Overflow](https://stackoverflow.com/questions/21920939/reverse-the-rows-of-a-2d-array)
         | 
| 88 | 
            +
                    for (int j = 0; j < array.length; j++) {
         | 
| 89 | 
            +
                        for (int i = 0; i < array[j].length / 2; i++) {
         | 
| 90 | 
            +
                            int temp = array[j][i];
         | 
| 91 | 
            +
                            array[j][i] = array[j][array[j].length - i - 1];
         | 
| 92 | 
            +
                            array[j][array[j].length - i - 1] = temp;
         | 
| 93 | 
            +
                        }
         | 
| 94 | 
            +
                    }
         | 
| 95 | 
            +
             | 
| 96 | 
            +
                    for (int[] a : array) {
         | 
| 97 | 
            +
                        for (int i : a) {
         | 
| 98 | 
            +
                            System.out.printf("%3d", i);
         | 
| 99 | 
            +
                        }
         | 
| 100 | 
            +
                        System.out.println();
         | 
| 101 | 
            +
                    }
         | 
| 102 | 
            +
                }
         | 
| 103 | 
            +
             | 
| 104 | 
            +
                private static int[][] getSpiralMatrix(int n) { /* 同じなので省略 */ }
         | 
| 105 | 
            +
            }
         | 
| 106 | 
            +
            ```
         | 
| 107 | 
            +
             | 
| 108 | 
            +
            ```
         | 
| 109 | 
            +
             21 22 23 24 25
         | 
| 110 | 
            +
             20  7  8  9 10
         | 
| 111 | 
            +
             19  6  1  2 11
         | 
| 112 | 
            +
             18  5  4  3 12
         | 
| 113 | 
            +
             17 16 15 14 13
         | 
| 114 | 
            +
            ```
         | 
| 115 | 
            +
             | 
| 116 | 
            +
            ---
         | 
| 117 | 
            +
             | 
| 118 | 
            +
            上記を超ふまえたうえで、GUIにするとこうなりました^^;
         | 
| 119 | 
            +
            この2次元配列を種にして(xyや番号はわかっている)、マスのクラス(`Cell`)を生成します。
         | 
| 120 | 
            +
             | 
| 121 | 
            +
            あくまでアウトラインですが、部品は揃ってると思いますので参考にしていただけたらと思います^^
         | 
| 122 | 
            +
             | 
| 123 | 
            +
            ```Java
         | 
| 124 | 
            +
            import java.awt.BasicStroke;
         | 
| 125 | 
            +
            import java.awt.Color;
         | 
| 126 | 
            +
            import java.awt.Dimension;
         | 
| 127 | 
            +
            import java.awt.Graphics;
         | 
| 128 | 
            +
            import java.awt.Graphics2D;
         | 
| 129 | 
            +
            import java.awt.Rectangle;
         | 
| 130 | 
            +
            import java.awt.Stroke;
         | 
| 131 | 
            +
            import java.util.ArrayList;
         | 
| 132 | 
            +
            import javax.swing.JButton;
         | 
| 133 | 
            +
            import javax.swing.JFrame;
         | 
| 134 | 
            +
            import javax.swing.JPanel;
         | 
| 135 | 
            +
             | 
| 136 | 
            +
             | 
| 137 | 
            +
            public class GameWindow extends JFrame {
         | 
| 138 | 
            +
                public static void main(String[] args) {
         | 
| 139 | 
            +
                    new GameWindow().setVisible(true);
         | 
| 140 | 
            +
                }
         | 
| 141 | 
            +
             | 
| 142 | 
            +
                public GameWindow() {
         | 
| 143 | 
            +
                    DrawCanvas panel = new DrawCanvas();
         | 
| 144 | 
            +
            //        setSize(550, 600); // JFrame のサイズはタイトルバー・枠も含むので微妙にずれる
         | 
| 145 | 
            +
                    panel.setPreferredSize(new Dimension(550, 600)); // 中身の推奨サイズを設定
         | 
| 146 | 
            +
                    add(panel);
         | 
| 147 | 
            +
                    pack();
         | 
| 148 | 
            +
             | 
| 149 | 
            +
                    setResizable(false);
         | 
| 150 | 
            +
                    setLocationRelativeTo(null);
         | 
| 151 | 
            +
                    setDefaultCloseOperation(EXIT_ON_CLOSE);
         | 
| 152 | 
            +
                }
         | 
| 153 | 
            +
            }
         | 
| 154 | 
            +
             | 
| 155 | 
            +
            class Cell {
         | 
| 156 | 
            +
                int no;
         | 
| 157 | 
            +
                Rectangle rect;
         | 
| 158 | 
            +
                Color color;
         | 
| 159 | 
            +
             | 
| 160 | 
            +
                Cell(int no, Rectangle rect, Color color) {
         | 
| 161 | 
            +
                    this.no = no;
         | 
| 162 | 
            +
                    this.rect = rect;
         | 
| 163 | 
            +
                    this.color = color;
         | 
| 164 | 
            +
                }
         | 
| 165 | 
            +
             | 
| 166 | 
            +
                void draw(Graphics2D g2) {
         | 
| 167 | 
            +
                    g2.setColor(color);
         | 
| 168 | 
            +
                    g2.fill(rect);
         | 
| 169 | 
            +
                    g2.setColor(Color.BLACK);
         | 
| 170 | 
            +
                    g2.draw(rect);
         | 
| 171 | 
            +
                    g2.drawString(String.valueOf(no), rect.x + 5, rect.y + 15);
         | 
| 172 | 
            +
                }
         | 
| 173 | 
            +
            }
         | 
| 174 | 
            +
             | 
| 175 | 
            +
            class DrawCanvas extends JPanel {
         | 
| 176 | 
            +
                private final int[][] array = getSpiralMatrix(9);
         | 
| 177 | 
            +
                private final ArrayList<Cell> cells = new ArrayList<>();
         | 
| 178 | 
            +
             | 
| 179 | 
            +
                private int current = 1;
         | 
| 180 | 
            +
             | 
| 181 | 
            +
                DrawCanvas() {
         | 
| 182 | 
            +
                    initialize();
         | 
| 183 | 
            +
             | 
| 184 | 
            +
                    JButton go = new JButton("go");
         | 
| 185 | 
            +
                    go.addActionListener(e -> { current++; repaint(); });
         | 
| 186 | 
            +
                    add(go);
         | 
| 187 | 
            +
             | 
| 188 | 
            +
                    JButton back = new JButton("back");
         | 
| 189 | 
            +
                    back.addActionListener(e -> { current--; repaint(); });
         | 
| 190 | 
            +
                    add(back);
         | 
| 191 | 
            +
             | 
| 192 | 
            +
                    JButton rotation90 = new JButton("rotation90");
         | 
| 193 | 
            +
                    rotation90.addActionListener(e -> { rotation(); repaint(); });
         | 
| 194 | 
            +
                    add(rotation90);
         | 
| 195 | 
            +
             | 
| 196 | 
            +
                    JButton rotation180 = new JButton("rotation180");
         | 
| 197 | 
            +
                    rotation180.addActionListener(e -> { rotation(); rotation(); repaint(); });
         | 
| 198 | 
            +
                    add(rotation180);
         | 
| 199 | 
            +
                }
         | 
| 200 | 
            +
             | 
| 201 | 
            +
                private void initialize() {
         | 
| 202 | 
            +
                    for (int y = 0; y < array.length; y++) {
         | 
| 203 | 
            +
                        for (int x = 0; x < array[y].length; x++) {
         | 
| 204 | 
            +
                            array[y][x] = (array.length * array.length + 1) - array[y][x];
         | 
| 205 | 
            +
                        }
         | 
| 206 | 
            +
                    }
         | 
| 207 | 
            +
                    // [java - Reverse the rows of a 2d array - Stack Overflow](https://stackoverflow.com/questions/21920939/reverse-the-rows-of-a-2d-array)
         | 
| 208 | 
            +
                    for (int j = 0; j < array.length; j++) {
         | 
| 209 | 
            +
                        for (int i = 0; i < array[j].length / 2; i++) {
         | 
| 210 | 
            +
                            int temp = array[j][i];
         | 
| 211 | 
            +
                            array[j][i] = array[j][array[j].length - i - 1];
         | 
| 212 | 
            +
                            array[j][array[j].length - i - 1] = temp;
         | 
| 213 | 
            +
                        }
         | 
| 214 | 
            +
                    }
         | 
| 215 | 
            +
             | 
| 216 | 
            +
                    // array を種にして Cell のリストを作る
         | 
| 217 | 
            +
                    Color c = new Color(0, 170, 0);
         | 
| 218 | 
            +
                    for (int y = 0; y < array.length; y++) {
         | 
| 219 | 
            +
                        for (int x = 0; x < array[y].length; x++) {
         | 
| 220 | 
            +
                            Rectangle r = new Rectangle(x * 50 + 50, y * 50 + 100, 50, 50);
         | 
| 221 | 
            +
                            cells.add(new Cell(array[y][x], r, c));
         | 
| 222 | 
            +
                        }
         | 
| 223 | 
            +
                    }
         | 
| 224 | 
            +
                    // 番号順に並び変えれば点線を描きやすい
         | 
| 225 | 
            +
                    cells.sort((a, b) -> a.no - b.no);
         | 
| 226 | 
            +
                }
         | 
| 227 | 
            +
             | 
| 228 | 
            +
                private static final BasicStroke dashed = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1, new float[]{ 5 }, 0);
         | 
| 229 | 
            +
             | 
| 230 | 
            +
                public void paintComponent(Graphics g) {
         | 
| 231 | 
            +
                    super.paintComponent(g);
         | 
| 232 | 
            +
             | 
| 233 | 
            +
                    Graphics2D g2 = (Graphics2D) g;
         | 
| 234 | 
            +
                    Stroke oldStroke = g2.getStroke();
         | 
| 235 | 
            +
             | 
| 236 | 
            +
                    Cell before = null;
         | 
| 237 | 
            +
                    for (Cell cell : cells) {
         | 
| 238 | 
            +
                        cell.draw(g2);
         | 
| 239 | 
            +
             | 
| 240 | 
            +
                        // 前のセルとの間に点線を引く
         | 
| 241 | 
            +
                        if (before != null) {
         | 
| 242 | 
            +
                            g2.setStroke(dashed);
         | 
| 243 | 
            +
                            int x1 = before.rect.x + before.rect.width / 2;
         | 
| 244 | 
            +
                            int y1 = before.rect.y + before.rect.height / 2;
         | 
| 245 | 
            +
                            int x2 = cell.rect.x + cell.rect.width / 2;
         | 
| 246 | 
            +
                            int y2 = cell.rect.y + cell.rect.height / 2;
         | 
| 247 | 
            +
                            g2.drawLine(x1, y1, x2, y2);
         | 
| 248 | 
            +
                            g2.setStroke(oldStroke);
         | 
| 249 | 
            +
                        }
         | 
| 250 | 
            +
                        before = cell;
         | 
| 251 | 
            +
             | 
| 252 | 
            +
                        if (current == cell.no) {
         | 
| 253 | 
            +
                            g2.setColor(Color.RED);
         | 
| 254 | 
            +
                            Rectangle rect = cell.rect.getBounds();
         | 
| 255 | 
            +
                            rect.grow(-15, -15); // 一回り小さい四角に
         | 
| 256 | 
            +
                            g2.fill(rect);
         | 
| 257 | 
            +
                        }
         | 
| 258 | 
            +
                    }
         | 
| 259 | 
            +
                }
         | 
| 260 | 
            +
             | 
| 261 | 
            +
                // current を右90度回転 2次元配列の回転を応用(正方形のみ)
         | 
| 262 | 
            +
                private void rotation() {
         | 
| 263 | 
            +
                    for (int j = 0; j < array.length; j++) {
         | 
| 264 | 
            +
                        for (int i = 0; i < array[j].length; i++) {
         | 
| 265 | 
            +
                            if (current == array[j][i]) {
         | 
| 266 | 
            +
                                current = array[i][array.length - j - 1];
         | 
| 267 | 
            +
                                return;
         | 
| 268 | 
            +
                            }
         | 
| 269 | 
            +
                        }
         | 
| 270 | 
            +
                    }
         | 
| 271 | 
            +
                }
         | 
| 272 | 
            +
             | 
| 273 | 
            +
                // [How To Create Spiral Of Numbers (Spiral Matrix) In Java?](https://javaconceptoftheday.com/how-to-create-spiral-of-numbers-matrix-in-java/)
         | 
| 274 | 
            +
                private int[][] getSpiralMatrix(int n) {
         | 
| 275 | 
            +
                    int[][] spiral = new int[n][n];
         | 
| 276 | 
            +
                    int value = 1;
         | 
| 277 | 
            +
                    int minCol = 0;
         | 
| 278 | 
            +
                    int minRow = 0;
         | 
| 279 | 
            +
                    int maxCol = n - 1;
         | 
| 280 | 
            +
                    int maxRow = n - 1;
         | 
| 281 | 
            +
             | 
| 282 | 
            +
                    while (value <= n * n) {
         | 
| 283 | 
            +
                        for (int i = minCol; i <= maxCol; i++) {
         | 
| 284 | 
            +
                            spiral[minRow][i] = value++;
         | 
| 285 | 
            +
                        }
         | 
| 286 | 
            +
                        for (int i = minRow + 1; i <= maxRow; i++) {
         | 
| 287 | 
            +
                            spiral[i][maxCol] = value++;
         | 
| 288 | 
            +
                        }
         | 
| 289 | 
            +
                        for (int i = maxCol - 1; i >= minCol; i--) {
         | 
| 290 | 
            +
                            spiral[maxRow][i] = value++;
         | 
| 291 | 
            +
                        }
         | 
| 292 | 
            +
                        for (int i = maxRow - 1; i >= minRow + 1; i--) {
         | 
| 293 | 
            +
                            spiral[i][minCol] = value++;
         | 
| 294 | 
            +
                        }
         | 
| 295 | 
            +
                        minCol++; minRow++; maxCol--; maxRow--;
         | 
| 296 | 
            +
                    }
         | 
| 297 | 
            +
             | 
| 298 | 
            +
                    return spiral;
         | 
| 299 | 
            +
                }
         | 
| 300 | 
            +
            }
         | 
| 301 | 
            +
            ```
         | 
| 302 | 
            +
            
         | 
1
(自分の意図と)逆だった^^;
    
        answer	
    CHANGED
    
    | @@ -222,7 +222,7 @@ | |
| 222 222 | 
             
                        }
         | 
| 223 223 | 
             
                    }
         | 
| 224 224 | 
             
                    // 番号順に並び変えれば点線を描きやすい
         | 
| 225 | 
            -
                    cells.sort((a, b) ->  | 
| 225 | 
            +
                    cells.sort((a, b) -> a.no - b.no);
         | 
| 226 226 | 
             
                }
         | 
| 227 227 |  | 
| 228 228 | 
             
                private static final BasicStroke dashed = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1, new float[]{ 5 }, 0);
         | 
