diff -r e70a0e3555fb -r 576e7a2861ae src/main/java/de/uapcore/sudoku/Field.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/uapcore/sudoku/Field.java Sat Jul 25 14:01:28 2020 +0200 @@ -0,0 +1,177 @@ +/* + * Copyright 2013 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +package de.uapcore.sudoku; + +import javax.swing.*; +import java.awt.*; +import java.awt.image.BufferedImage; + +/** + * + * @author mike + */ +public final class Field extends JPanel { + private SudokuTextField[][] cells; + + public Field() { + setBackground(Color.WHITE); + + setLayout(new GridBagLayout()); + GridBagConstraints c = new GridBagConstraints(); + c.insets = new Insets(5, 5, 5, 5); + + cells = new SudokuTextField[9][9]; + for (int x = 0 ; x < 9 ; x++) { + for (int y = 0 ; y < 9 ; y++) { + cells[x][y] = new SudokuTextField(); + c.gridx = x; + c.gridy = y; + add(cells[x][y], c); + } + } + } + + @Override + public void paint(Graphics graphics) { + final int w = getWidth(); + final int h = getHeight(); + final int cw = w / 9; + final int ch = h / 9; + + BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); + Graphics2D g = img.createGraphics(); + g.setBackground(Color.WHITE); + g.clearRect(0, 0, w, h); + + g.setColor(Color.BLACK); + g.drawRect(1, 1, w-2, h-2); + g.drawRect(2, 2, w-4, h-4); + for (int x = cw ; x < w ; x += cw) { + for (int y = ch ; y < h ; y += ch) { + g.drawLine(x, 2, x, h-2); + g.drawLine(2, y, w-2, y); + if ((x / cw) % 3 == 0) { + g.drawLine(x+1, 2, x+1, h-2); + } + if ((y / ch) % 3 == 0) { + g.drawLine(2, y+1, w-2, y+1); + } + } + } + + graphics.drawImage(img, 0, 0, this); + super.paintChildren(graphics); + } + + public boolean isCellEmpty(int x, int y) { + return getCellValue(x, y) == 0; + } + + public int getCellValue(int x, int y) { + return cells[x][y].getValue(); + } + + public void setCellValue(int x, int y, int v) { + cells[x][y].setValue(v); + } + + public void clearCellValue(int x, int y) { + setCellValue(x, y, 0); + } + + public void setCellModified(int x, int y, boolean modified) { + cells[x][y].setModified(modified); + } + + public void setAllCellsModified(boolean modified) { + for (int x = 0 ; x < 9 ; x++) { + for (int y = 0 ; y < 9 ; y++) { + cells[x][y].setModified(modified); + } + } + } + + public boolean isAnyCellModified() { + for (int x = 0 ; x < 9 ; x++) { + for (int y = 0 ; y < 9 ; y++) { + if (cells[x][y].isModified()) { + return true; + } + } + } + return false; + } + + public void clear() { + for (int x = 0 ; x < 9 ; x++) { + for (int y = 0 ; y < 9 ; y++) { + cells[x][y].setValue(0); + } + } + } + + public int[][] getSquare(int x, int y) { + if (x < 0 || x > 2 || y < 0 || y > 2) { + throw new IllegalArgumentException("Invalid square coordinates"); + } + int[][] square = new int[3][3]; + + for (int u = 0 ; u < 3 ; u++) { + for (int v = 0 ; v < 3 ; v++) { + square[u][v] = getCellValue(3*x+u, 3*y+v); + } + } + + return square; + } + + public int[] getRow(int y) { + if (y < 0 || y > 8) { + throw new IllegalArgumentException("Invalid row number"); + } + int row[] = new int[9]; + + for (int x = 0 ; x < 9 ; x++) { + row[x] = getCellValue(x, y); + } + + return row; + } + + public int[] getColumn(int x) { + if (x < 0 || x > 8) { + throw new IllegalArgumentException("Invalid column number"); + } + int column[] = new int[9]; + + for (int y = 0 ; y < 9 ; y++) { + column[y] = getCellValue(x, y); + } + + return column; + } +}