Sat, 26 Jan 2013 18:43:49 +0100
added NEW function
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 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;
38 /**
39 *
40 * @author mike
41 */
42 public final class Field extends JPanel {
43 private SudokuTextField[][] cells;
45 public Field() {
46 setBackground(Color.WHITE);
48 setLayout(new GridBagLayout());
49 GridBagConstraints c = new GridBagConstraints();
50 c.insets = new Insets(5, 5, 5, 5);
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 }
63 @Override
64 public void paint(Graphics graphics) {
65 super.paint(graphics);
66 final int w = getWidth();
67 final int h = getHeight();
68 final int cw = w / 9;
69 final int ch = h / 9;
71 BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
72 Graphics2D g = img.createGraphics();
73 g.setBackground(Color.WHITE);
74 g.clearRect(0, 0, w, h);
76 g.setColor(Color.BLACK);
77 g.drawRect(1, 1, w-2, h-2);
78 g.drawRect(2, 2, w-4, h-4);
79 for (int x = cw ; x < w ; x += cw) {
80 for (int y = ch ; y < h ; y += ch) {
81 g.drawLine(x, 2, x, h-2);
82 g.drawLine(2, y, w-2, y);
83 if ((x / cw) % 3 == 0) {
84 g.drawLine(x+1, 2, x+1, h-2);
85 }
86 if ((y / ch) % 3 == 0) {
87 g.drawLine(2, y+1, w-2, y+1);
88 }
89 }
90 }
92 graphics.drawImage(img, 0, 0, this);
93 }
95 public int getCellValue(int x, int y) {
96 return cells[x][y].getValue();
97 }
99 public void setCellValue(int x, int y, int v) {
100 cells[x][y].setValue(v);
101 }
103 public void setAllCellsModified(boolean modified) {
104 for (int x = 0 ; x < 9 ; x++) {
105 for (int y = 0 ; y < 9 ; y++) {
106 cells[x][y].setModified(modified);
107 }
108 }
109 }
111 public boolean isAnyCellModified() {
112 for (int x = 0 ; x < 9 ; x++) {
113 for (int y = 0 ; y < 9 ; y++) {
114 if (cells[x][y].isModified()) {
115 return true;
116 }
117 }
118 }
119 return false;
120 }
122 public void clear() {
123 for (int x = 0 ; x < 9 ; x++) {
124 for (int y = 0 ; y < 9 ; y++) {
125 cells[x][y].setValue(0);
126 }
127 }
128 }
129 }