Sat, 25 Jul 2020 15:56:48 +0200
missing class documentation
1 /*
2 * Copyright 2013 Mike Becker. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
27 package de.uapcore.sudoku;
29 import javax.swing.*;
30 import java.awt.*;
31 import java.awt.image.BufferedImage;
33 /**
34 * A panel rendering the Sudoku field.
35 * <p>
36 * Cells are identified by zero-based indices from top-left to bottom-right.
37 */
38 public final class Field extends JPanel {
39 private SudokuTextField[][] cells;
41 /**
42 * Constructs a new 9x9 Sudoku grid.
43 */
44 public Field() {
45 setBackground(Color.WHITE);
47 setLayout(new GridBagLayout());
48 GridBagConstraints c = new GridBagConstraints();
49 c.insets = new Insets(5, 5, 5, 5);
51 cells = new SudokuTextField[9][9];
52 for (int x = 0; x < 9; x++) {
53 for (int y = 0; y < 9; y++) {
54 cells[x][y] = new SudokuTextField();
55 c.gridx = x;
56 c.gridy = y;
57 add(cells[x][y], c);
58 }
59 }
60 }
62 /**
63 * Paints the grid and all contained cells.
64 *
65 * @param graphics the graphics context
66 */
67 @Override
68 public void paint(Graphics graphics) {
69 final int w = getWidth();
70 final int h = getHeight();
71 final int cw = w / 9;
72 final int ch = h / 9;
74 BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
75 Graphics2D g = img.createGraphics();
76 g.setBackground(Color.WHITE);
77 g.clearRect(0, 0, w, h);
79 g.setColor(Color.BLACK);
80 g.drawRect(1, 1, w - 2, h - 2);
81 g.drawRect(2, 2, w - 4, h - 4);
82 for (int x = cw; x < w; x += cw) {
83 for (int y = ch; y < h; y += ch) {
84 g.drawLine(x, 2, x, h - 2);
85 g.drawLine(2, y, w - 2, y);
86 if ((x / cw) % 3 == 0) {
87 g.drawLine(x + 1, 2, x + 1, h - 2);
88 }
89 if ((y / ch) % 3 == 0) {
90 g.drawLine(2, y + 1, w - 2, y + 1);
91 }
92 }
93 }
95 graphics.drawImage(img, 0, 0, this);
96 super.paintChildren(graphics);
97 }
99 /**
100 * Checks whether a cell is empty
101 *
102 * @param x horizontal position
103 * @param y vertical position
104 * @return true if the cell is empty, false otherwise
105 */
106 public boolean isCellEmpty(int x, int y) {
107 return getCellValue(x, y) == 0;
108 }
110 /**
111 * Returns value of a specific cell.
112 *
113 * @param x horizontal position
114 * @param y vertical position
115 * @return the cell's value
116 */
117 public int getCellValue(int x, int y) {
118 return cells[x][y].getValue();
119 }
121 /**
122 * Sets the value of a specific cell.
123 *
124 * @param x horizontal position
125 * @param y vertical position
126 * @param v the cells value
127 */
128 public void setCellValue(int x, int y, int v) {
129 cells[x][y].setValue(v);
130 }
132 /**
133 * Clears the value of a specific cell.
134 *
135 * @param x horizontal position
136 * @param y vertical position
137 */
138 public void clearCellValue(int x, int y) {
139 setCellValue(x, y, 0);
140 }
142 /**
143 * Sets the modified state of a specific cell.
144 *
145 * @param x horizontal position
146 * @param y vertical position
147 * @param modified the modified state
148 */
149 public void setCellModified(int x, int y, boolean modified) {
150 cells[x][y].setModified(modified);
151 }
153 /**
154 * Sets the modified state of all cells.
155 *
156 * @param modified the modified state
157 */
158 public void setAllCellsModified(boolean modified) {
159 for (int x = 0; x < 9; x++) {
160 for (int y = 0; y < 9; y++) {
161 cells[x][y].setModified(modified);
162 }
163 }
164 }
166 /**
167 * Checks whether any cell is modified.
168 *
169 * @return true if any cell is modified, false otherwise
170 */
171 public boolean isAnyCellModified() {
172 for (int x = 0; x < 9; x++) {
173 for (int y = 0; y < 9; y++) {
174 if (cells[x][y].isModified()) {
175 return true;
176 }
177 }
178 }
179 return false;
180 }
182 /**
183 * Clears all cells.
184 */
185 public void clear() {
186 for (int x = 0; x < 9; x++) {
187 for (int y = 0; y < 9; y++) {
188 cells[x][y].setValue(0);
189 }
190 }
191 }
193 /**
194 * Returns a square identified by square coordinates.
195 * <p>
196 * Cells within the square are identified by the same coordinate system.
197 *
198 * @param x horizontal position from 0 to 2
199 * @param y vertical position from 0 to 2
200 * @return a two-dimensional array containing the square cell values
201 */
202 public int[][] getSquare(int x, int y) {
203 if (x < 0 || x > 2 || y < 0 || y > 2) {
204 throw new IllegalArgumentException("Invalid square coordinates");
205 }
206 int[][] square = new int[3][3];
208 for (int u = 0; u < 3; u++) {
209 for (int v = 0; v < 3; v++) {
210 square[u][v] = getCellValue(3 * x + u, 3 * y + v);
211 }
212 }
214 return square;
215 }
217 /**
218 * Returns an entire row.
219 *
220 * @param y the row position
221 * @return an array containing the row values
222 */
223 public int[] getRow(int y) {
224 if (y < 0 || y > 8) {
225 throw new IllegalArgumentException("Invalid row number");
226 }
227 int row[] = new int[9];
229 for (int x = 0; x < 9; x++) {
230 row[x] = getCellValue(x, y);
231 }
233 return row;
234 }
236 /**
237 * Returns an entire column
238 *
239 * @param x the column position
240 * @return an array containing the column values
241 */
242 public int[] getColumn(int x) {
243 if (x < 0 || x > 8) {
244 throw new IllegalArgumentException("Invalid column number");
245 }
246 int column[] = new int[9];
248 for (int y = 0; y < 9; y++) {
249 column[y] = getCellValue(x, y);
250 }
252 return column;
253 }
254 }