src/main/java/de/uapcore/sudoku/Field.java

Sat, 25 Jul 2020 14:01:28 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 25 Jul 2020 14:01:28 +0200
changeset 9
576e7a2861ae
parent 7
src/de/uapcore/sudoku/Field.java@2c0a2766461c
child 10
369903afbb29
permissions
-rw-r--r--

converts to maven project

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

mercurial