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

changeset 9
576e7a2861ae
parent 7
2c0a2766461c
child 10
369903afbb29
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main/java/de/uapcore/sudoku/Field.java	Sat Jul 25 14:01:28 2020 +0200
     1.3 @@ -0,0 +1,177 @@
     1.4 +/*
     1.5 + * Copyright 2013 Mike Becker. All rights reserved.
     1.6 + * 
     1.7 + * Redistribution and use in source and binary forms, with or without
     1.8 + * modification, are permitted provided that the following conditions are met:
     1.9 + * 
    1.10 + * 1. Redistributions of source code must retain the above copyright
    1.11 + *    notice, this list of conditions and the following disclaimer.
    1.12 + * 
    1.13 + * 2. Redistributions in binary form must reproduce the above copyright
    1.14 + *    notice, this list of conditions and the following disclaimer in the
    1.15 + *    documentation and/or other materials provided with the distribution.
    1.16 + * 
    1.17 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.18 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.19 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.20 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.21 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.22 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.23 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.24 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.25 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.26 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.27 + * POSSIBILITY OF SUCH DAMAGE.
    1.28 + */
    1.29 +
    1.30 +package de.uapcore.sudoku;
    1.31 +
    1.32 +import javax.swing.*;
    1.33 +import java.awt.*;
    1.34 +import java.awt.image.BufferedImage;
    1.35 +
    1.36 +/**
    1.37 + *
    1.38 + * @author mike
    1.39 + */
    1.40 +public final class Field extends JPanel {
    1.41 +    private SudokuTextField[][] cells;
    1.42 +    
    1.43 +    public Field() {
    1.44 +        setBackground(Color.WHITE);
    1.45 +        
    1.46 +        setLayout(new GridBagLayout());
    1.47 +        GridBagConstraints c = new GridBagConstraints();
    1.48 +        c.insets = new Insets(5, 5, 5, 5);
    1.49 +        
    1.50 +        cells = new SudokuTextField[9][9];
    1.51 +        for (int x = 0 ; x < 9 ; x++) {
    1.52 +            for (int y = 0 ; y < 9 ; y++) {
    1.53 +                cells[x][y] = new SudokuTextField();
    1.54 +                c.gridx = x;
    1.55 +                c.gridy = y;
    1.56 +                add(cells[x][y], c);
    1.57 +            }
    1.58 +        }
    1.59 +    }
    1.60 +
    1.61 +    @Override
    1.62 +    public void paint(Graphics graphics) {
    1.63 +        final int w = getWidth();
    1.64 +        final int h = getHeight();
    1.65 +        final int cw = w / 9;
    1.66 +        final int ch = h / 9;
    1.67 +        
    1.68 +        BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    1.69 +        Graphics2D g = img.createGraphics();
    1.70 +        g.setBackground(Color.WHITE);
    1.71 +        g.clearRect(0, 0, w, h);
    1.72 +        
    1.73 +        g.setColor(Color.BLACK);
    1.74 +        g.drawRect(1, 1, w-2, h-2);
    1.75 +        g.drawRect(2, 2, w-4, h-4);
    1.76 +        for (int x = cw ; x < w ; x += cw) {
    1.77 +            for (int y = ch ; y < h ; y += ch) {
    1.78 +                g.drawLine(x, 2, x, h-2);
    1.79 +                g.drawLine(2, y, w-2, y);
    1.80 +                if ((x / cw) % 3 == 0) {
    1.81 +                    g.drawLine(x+1, 2, x+1, h-2);
    1.82 +                }
    1.83 +                if ((y / ch) % 3 == 0) {
    1.84 +                    g.drawLine(2, y+1, w-2, y+1);
    1.85 +                }
    1.86 +            }
    1.87 +        }
    1.88 +        
    1.89 +        graphics.drawImage(img, 0, 0, this);
    1.90 +        super.paintChildren(graphics);
    1.91 +    }
    1.92 +    
    1.93 +    public boolean isCellEmpty(int x, int y) {
    1.94 +        return getCellValue(x, y) == 0;
    1.95 +    }
    1.96 +    
    1.97 +    public int getCellValue(int x, int y) {
    1.98 +        return cells[x][y].getValue();
    1.99 +    }
   1.100 +    
   1.101 +    public void setCellValue(int x, int y, int v) {
   1.102 +        cells[x][y].setValue(v);
   1.103 +    }
   1.104 +    
   1.105 +    public void clearCellValue(int x, int y) {
   1.106 +        setCellValue(x, y, 0);
   1.107 +    }
   1.108 +    
   1.109 +    public void setCellModified(int x, int y, boolean modified) {
   1.110 +        cells[x][y].setModified(modified);
   1.111 +    }
   1.112 +    
   1.113 +    public void setAllCellsModified(boolean modified) {
   1.114 +        for (int x = 0 ; x < 9 ; x++) {
   1.115 +            for (int y = 0 ; y < 9 ; y++) {
   1.116 +                cells[x][y].setModified(modified);
   1.117 +            }
   1.118 +        }
   1.119 +    }
   1.120 +    
   1.121 +    public boolean isAnyCellModified() {
   1.122 +        for (int x = 0 ; x < 9 ; x++) {
   1.123 +            for (int y = 0 ; y < 9 ; y++) {
   1.124 +                if (cells[x][y].isModified()) {
   1.125 +                    return true;
   1.126 +                }
   1.127 +            }
   1.128 +        }
   1.129 +        return false;
   1.130 +    }
   1.131 +    
   1.132 +    public void clear() {
   1.133 +        for (int x = 0 ; x < 9 ; x++) {
   1.134 +            for (int y = 0 ; y < 9 ; y++) {
   1.135 +                cells[x][y].setValue(0);
   1.136 +            }
   1.137 +        }
   1.138 +    }
   1.139 +    
   1.140 +    public int[][] getSquare(int x, int y) {
   1.141 +        if (x < 0 || x > 2 || y < 0 || y > 2) {
   1.142 +            throw new IllegalArgumentException("Invalid square coordinates");
   1.143 +        }
   1.144 +        int[][] square = new int[3][3];
   1.145 +        
   1.146 +        for (int u = 0 ; u < 3 ; u++) {
   1.147 +            for (int v = 0 ; v < 3 ; v++) {
   1.148 +                square[u][v] = getCellValue(3*x+u, 3*y+v);
   1.149 +            }
   1.150 +        }
   1.151 +        
   1.152 +        return square;
   1.153 +    }
   1.154 +    
   1.155 +    public int[] getRow(int y) {
   1.156 +        if (y < 0 || y > 8) {
   1.157 +            throw new IllegalArgumentException("Invalid row number");
   1.158 +        }
   1.159 +        int row[] = new int[9];
   1.160 +        
   1.161 +        for (int x = 0 ; x < 9 ; x++) {
   1.162 +            row[x] = getCellValue(x, y);
   1.163 +        }
   1.164 +        
   1.165 +        return row;
   1.166 +    }
   1.167 +    
   1.168 +    public int[] getColumn(int x) {
   1.169 +        if (x < 0 || x > 8) {
   1.170 +            throw new IllegalArgumentException("Invalid column number");
   1.171 +        }
   1.172 +        int column[] = new int[9];
   1.173 +        
   1.174 +        for (int y = 0 ; y < 9 ; y++) {
   1.175 +            column[y] = getCellValue(x, y);
   1.176 +        }
   1.177 +        
   1.178 +        return column;
   1.179 +    }
   1.180 +}

mercurial