Sat, 25 Jul 2020 14:01:28 +0200
converts to maven project
universe@3 | 1 | /* |
universe@3 | 2 | * Copyright 2013 Mike Becker. All rights reserved. |
universe@3 | 3 | * |
universe@3 | 4 | * Redistribution and use in source and binary forms, with or without |
universe@3 | 5 | * modification, are permitted provided that the following conditions are met: |
universe@3 | 6 | * |
universe@3 | 7 | * 1. Redistributions of source code must retain the above copyright |
universe@3 | 8 | * notice, this list of conditions and the following disclaimer. |
universe@3 | 9 | * |
universe@3 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@3 | 11 | * notice, this list of conditions and the following disclaimer in the |
universe@3 | 12 | * documentation and/or other materials provided with the distribution. |
universe@3 | 13 | * |
universe@3 | 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@3 | 15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@3 | 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@3 | 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@3 | 18 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@3 | 19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@3 | 20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@3 | 21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@3 | 22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@3 | 23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@3 | 24 | * POSSIBILITY OF SUCH DAMAGE. |
universe@3 | 25 | */ |
universe@3 | 26 | |
universe@1 | 27 | package de.uapcore.sudoku; |
universe@1 | 28 | |
universe@9 | 29 | import javax.swing.*; |
universe@9 | 30 | import java.awt.*; |
universe@1 | 31 | import java.awt.image.BufferedImage; |
universe@1 | 32 | |
universe@1 | 33 | /** |
universe@1 | 34 | * |
universe@1 | 35 | * @author mike |
universe@1 | 36 | */ |
universe@2 | 37 | public final class Field extends JPanel { |
universe@1 | 38 | private SudokuTextField[][] cells; |
universe@1 | 39 | |
universe@1 | 40 | public Field() { |
universe@1 | 41 | setBackground(Color.WHITE); |
universe@1 | 42 | |
universe@1 | 43 | setLayout(new GridBagLayout()); |
universe@1 | 44 | GridBagConstraints c = new GridBagConstraints(); |
universe@1 | 45 | c.insets = new Insets(5, 5, 5, 5); |
universe@1 | 46 | |
universe@1 | 47 | cells = new SudokuTextField[9][9]; |
universe@1 | 48 | for (int x = 0 ; x < 9 ; x++) { |
universe@1 | 49 | for (int y = 0 ; y < 9 ; y++) { |
universe@1 | 50 | cells[x][y] = new SudokuTextField(); |
universe@1 | 51 | c.gridx = x; |
universe@1 | 52 | c.gridy = y; |
universe@1 | 53 | add(cells[x][y], c); |
universe@1 | 54 | } |
universe@1 | 55 | } |
universe@1 | 56 | } |
universe@1 | 57 | |
universe@1 | 58 | @Override |
universe@1 | 59 | public void paint(Graphics graphics) { |
universe@1 | 60 | final int w = getWidth(); |
universe@1 | 61 | final int h = getHeight(); |
universe@1 | 62 | final int cw = w / 9; |
universe@1 | 63 | final int ch = h / 9; |
universe@1 | 64 | |
universe@1 | 65 | BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); |
universe@1 | 66 | Graphics2D g = img.createGraphics(); |
universe@1 | 67 | g.setBackground(Color.WHITE); |
universe@1 | 68 | g.clearRect(0, 0, w, h); |
universe@1 | 69 | |
universe@1 | 70 | g.setColor(Color.BLACK); |
universe@1 | 71 | g.drawRect(1, 1, w-2, h-2); |
universe@1 | 72 | g.drawRect(2, 2, w-4, h-4); |
universe@1 | 73 | for (int x = cw ; x < w ; x += cw) { |
universe@1 | 74 | for (int y = ch ; y < h ; y += ch) { |
universe@1 | 75 | g.drawLine(x, 2, x, h-2); |
universe@1 | 76 | g.drawLine(2, y, w-2, y); |
universe@1 | 77 | if ((x / cw) % 3 == 0) { |
universe@1 | 78 | g.drawLine(x+1, 2, x+1, h-2); |
universe@1 | 79 | } |
universe@1 | 80 | if ((y / ch) % 3 == 0) { |
universe@1 | 81 | g.drawLine(2, y+1, w-2, y+1); |
universe@1 | 82 | } |
universe@1 | 83 | } |
universe@1 | 84 | } |
universe@1 | 85 | |
universe@1 | 86 | graphics.drawImage(img, 0, 0, this); |
universe@5 | 87 | super.paintChildren(graphics); |
universe@1 | 88 | } |
universe@1 | 89 | |
universe@7 | 90 | public boolean isCellEmpty(int x, int y) { |
universe@7 | 91 | return getCellValue(x, y) == 0; |
universe@7 | 92 | } |
universe@7 | 93 | |
universe@2 | 94 | public int getCellValue(int x, int y) { |
universe@2 | 95 | return cells[x][y].getValue(); |
universe@2 | 96 | } |
universe@1 | 97 | |
universe@2 | 98 | public void setCellValue(int x, int y, int v) { |
universe@2 | 99 | cells[x][y].setValue(v); |
universe@2 | 100 | } |
universe@2 | 101 | |
universe@7 | 102 | public void clearCellValue(int x, int y) { |
universe@7 | 103 | setCellValue(x, y, 0); |
universe@7 | 104 | } |
universe@7 | 105 | |
universe@7 | 106 | public void setCellModified(int x, int y, boolean modified) { |
universe@7 | 107 | cells[x][y].setModified(modified); |
universe@7 | 108 | } |
universe@7 | 109 | |
universe@2 | 110 | public void setAllCellsModified(boolean modified) { |
universe@2 | 111 | for (int x = 0 ; x < 9 ; x++) { |
universe@2 | 112 | for (int y = 0 ; y < 9 ; y++) { |
universe@2 | 113 | cells[x][y].setModified(modified); |
universe@2 | 114 | } |
universe@2 | 115 | } |
universe@2 | 116 | } |
universe@2 | 117 | |
universe@2 | 118 | public boolean isAnyCellModified() { |
universe@2 | 119 | for (int x = 0 ; x < 9 ; x++) { |
universe@2 | 120 | for (int y = 0 ; y < 9 ; y++) { |
universe@2 | 121 | if (cells[x][y].isModified()) { |
universe@2 | 122 | return true; |
universe@2 | 123 | } |
universe@2 | 124 | } |
universe@2 | 125 | } |
universe@2 | 126 | return false; |
universe@2 | 127 | } |
universe@4 | 128 | |
universe@4 | 129 | public void clear() { |
universe@4 | 130 | for (int x = 0 ; x < 9 ; x++) { |
universe@4 | 131 | for (int y = 0 ; y < 9 ; y++) { |
universe@4 | 132 | cells[x][y].setValue(0); |
universe@4 | 133 | } |
universe@4 | 134 | } |
universe@4 | 135 | } |
universe@5 | 136 | |
universe@5 | 137 | public int[][] getSquare(int x, int y) { |
universe@5 | 138 | if (x < 0 || x > 2 || y < 0 || y > 2) { |
universe@5 | 139 | throw new IllegalArgumentException("Invalid square coordinates"); |
universe@5 | 140 | } |
universe@5 | 141 | int[][] square = new int[3][3]; |
universe@5 | 142 | |
universe@5 | 143 | for (int u = 0 ; u < 3 ; u++) { |
universe@5 | 144 | for (int v = 0 ; v < 3 ; v++) { |
universe@5 | 145 | square[u][v] = getCellValue(3*x+u, 3*y+v); |
universe@5 | 146 | } |
universe@5 | 147 | } |
universe@5 | 148 | |
universe@5 | 149 | return square; |
universe@5 | 150 | } |
universe@5 | 151 | |
universe@5 | 152 | public int[] getRow(int y) { |
universe@5 | 153 | if (y < 0 || y > 8) { |
universe@5 | 154 | throw new IllegalArgumentException("Invalid row number"); |
universe@5 | 155 | } |
universe@5 | 156 | int row[] = new int[9]; |
universe@5 | 157 | |
universe@5 | 158 | for (int x = 0 ; x < 9 ; x++) { |
universe@5 | 159 | row[x] = getCellValue(x, y); |
universe@5 | 160 | } |
universe@5 | 161 | |
universe@5 | 162 | return row; |
universe@5 | 163 | } |
universe@5 | 164 | |
universe@5 | 165 | public int[] getColumn(int x) { |
universe@5 | 166 | if (x < 0 || x > 8) { |
universe@5 | 167 | throw new IllegalArgumentException("Invalid column number"); |
universe@5 | 168 | } |
universe@5 | 169 | int column[] = new int[9]; |
universe@5 | 170 | |
universe@5 | 171 | for (int y = 0 ; y < 9 ; y++) { |
universe@5 | 172 | column[y] = getCellValue(x, y); |
universe@5 | 173 | } |
universe@5 | 174 | |
universe@5 | 175 | return column; |
universe@5 | 176 | } |
universe@1 | 177 | } |