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 */ |
|
26 |
|
27 package de.uapcore.sudoku; |
|
28 |
|
29 import java.awt.Color; |
|
30 import java.awt.Graphics; |
|
31 import java.awt.Graphics2D; |
|
32 import java.awt.GridBagConstraints; |
|
33 import java.awt.GridBagLayout; |
|
34 import java.awt.Insets; |
|
35 import java.awt.image.BufferedImage; |
|
36 import javax.swing.JPanel; |
|
37 |
|
38 /** |
|
39 * |
|
40 * @author mike |
|
41 */ |
|
42 public final class Field extends JPanel { |
|
43 private SudokuTextField[][] cells; |
|
44 |
|
45 public Field() { |
|
46 setBackground(Color.WHITE); |
|
47 |
|
48 setLayout(new GridBagLayout()); |
|
49 GridBagConstraints c = new GridBagConstraints(); |
|
50 c.insets = new Insets(5, 5, 5, 5); |
|
51 |
|
52 cells = new SudokuTextField[9][9]; |
|
53 for (int x = 0 ; x < 9 ; x++) { |
|
54 for (int y = 0 ; y < 9 ; y++) { |
|
55 cells[x][y] = new SudokuTextField(); |
|
56 c.gridx = x; |
|
57 c.gridy = y; |
|
58 add(cells[x][y], c); |
|
59 } |
|
60 } |
|
61 } |
|
62 |
|
63 @Override |
|
64 public void paint(Graphics graphics) { |
|
65 final int w = getWidth(); |
|
66 final int h = getHeight(); |
|
67 final int cw = w / 9; |
|
68 final int ch = h / 9; |
|
69 |
|
70 BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); |
|
71 Graphics2D g = img.createGraphics(); |
|
72 g.setBackground(Color.WHITE); |
|
73 g.clearRect(0, 0, w, h); |
|
74 |
|
75 g.setColor(Color.BLACK); |
|
76 g.drawRect(1, 1, w-2, h-2); |
|
77 g.drawRect(2, 2, w-4, h-4); |
|
78 for (int x = cw ; x < w ; x += cw) { |
|
79 for (int y = ch ; y < h ; y += ch) { |
|
80 g.drawLine(x, 2, x, h-2); |
|
81 g.drawLine(2, y, w-2, y); |
|
82 if ((x / cw) % 3 == 0) { |
|
83 g.drawLine(x+1, 2, x+1, h-2); |
|
84 } |
|
85 if ((y / ch) % 3 == 0) { |
|
86 g.drawLine(2, y+1, w-2, y+1); |
|
87 } |
|
88 } |
|
89 } |
|
90 |
|
91 graphics.drawImage(img, 0, 0, this); |
|
92 super.paintChildren(graphics); |
|
93 } |
|
94 |
|
95 public boolean isCellEmpty(int x, int y) { |
|
96 return getCellValue(x, y) == 0; |
|
97 } |
|
98 |
|
99 public int getCellValue(int x, int y) { |
|
100 return cells[x][y].getValue(); |
|
101 } |
|
102 |
|
103 public void setCellValue(int x, int y, int v) { |
|
104 cells[x][y].setValue(v); |
|
105 } |
|
106 |
|
107 public void clearCellValue(int x, int y) { |
|
108 setCellValue(x, y, 0); |
|
109 } |
|
110 |
|
111 public void setCellModified(int x, int y, boolean modified) { |
|
112 cells[x][y].setModified(modified); |
|
113 } |
|
114 |
|
115 public void setAllCellsModified(boolean modified) { |
|
116 for (int x = 0 ; x < 9 ; x++) { |
|
117 for (int y = 0 ; y < 9 ; y++) { |
|
118 cells[x][y].setModified(modified); |
|
119 } |
|
120 } |
|
121 } |
|
122 |
|
123 public boolean isAnyCellModified() { |
|
124 for (int x = 0 ; x < 9 ; x++) { |
|
125 for (int y = 0 ; y < 9 ; y++) { |
|
126 if (cells[x][y].isModified()) { |
|
127 return true; |
|
128 } |
|
129 } |
|
130 } |
|
131 return false; |
|
132 } |
|
133 |
|
134 public void clear() { |
|
135 for (int x = 0 ; x < 9 ; x++) { |
|
136 for (int y = 0 ; y < 9 ; y++) { |
|
137 cells[x][y].setValue(0); |
|
138 } |
|
139 } |
|
140 } |
|
141 |
|
142 public int[][] getSquare(int x, int y) { |
|
143 if (x < 0 || x > 2 || y < 0 || y > 2) { |
|
144 throw new IllegalArgumentException("Invalid square coordinates"); |
|
145 } |
|
146 int[][] square = new int[3][3]; |
|
147 |
|
148 for (int u = 0 ; u < 3 ; u++) { |
|
149 for (int v = 0 ; v < 3 ; v++) { |
|
150 square[u][v] = getCellValue(3*x+u, 3*y+v); |
|
151 } |
|
152 } |
|
153 |
|
154 return square; |
|
155 } |
|
156 |
|
157 public int[] getRow(int y) { |
|
158 if (y < 0 || y > 8) { |
|
159 throw new IllegalArgumentException("Invalid row number"); |
|
160 } |
|
161 int row[] = new int[9]; |
|
162 |
|
163 for (int x = 0 ; x < 9 ; x++) { |
|
164 row[x] = getCellValue(x, y); |
|
165 } |
|
166 |
|
167 return row; |
|
168 } |
|
169 |
|
170 public int[] getColumn(int x) { |
|
171 if (x < 0 || x > 8) { |
|
172 throw new IllegalArgumentException("Invalid column number"); |
|
173 } |
|
174 int column[] = new int[9]; |
|
175 |
|
176 for (int y = 0 ; y < 9 ; y++) { |
|
177 column[y] = getCellValue(x, y); |
|
178 } |
|
179 |
|
180 return column; |
|
181 } |
|
182 } |
|