src/de/uapcore/sudoku/Field.java

Sat, 26 Jan 2013 19:34:31 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 26 Jan 2013 19:34:31 +0100
changeset 5
8ddf4af937d7
parent 4
b8588e318001
child 7
2c0a2766461c
permissions
-rw-r--r--

moved field methods to field class + added (parts of the) document handler

     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         final int w = getWidth();
    66         final int h = getHeight();
    67         final int cw = w / 9;
    68         final int ch = h / 9;
    70         BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    71         Graphics2D g = img.createGraphics();
    72         g.setBackground(Color.WHITE);
    73         g.clearRect(0, 0, w, h);
    75         g.setColor(Color.BLACK);
    76         g.drawRect(1, 1, w-2, h-2);
    77         g.drawRect(2, 2, w-4, h-4);
    78         for (int x = cw ; x < w ; x += cw) {
    79             for (int y = ch ; y < h ; y += ch) {
    80                 g.drawLine(x, 2, x, h-2);
    81                 g.drawLine(2, y, w-2, y);
    82                 if ((x / cw) % 3 == 0) {
    83                     g.drawLine(x+1, 2, x+1, h-2);
    84                 }
    85                 if ((y / ch) % 3 == 0) {
    86                     g.drawLine(2, y+1, w-2, y+1);
    87                 }
    88             }
    89         }
    91         graphics.drawImage(img, 0, 0, this);
    92         super.paintChildren(graphics);
    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     }
   130     public int[][] getSquare(int x, int y) {
   131         if (x < 0 || x > 2 || y < 0 || y > 2) {
   132             throw new IllegalArgumentException("Invalid square coordinates");
   133         }
   134         int[][] square = new int[3][3];
   136         for (int u = 0 ; u < 3 ; u++) {
   137             for (int v = 0 ; v < 3 ; v++) {
   138                 square[u][v] = getCellValue(3*x+u, 3*y+v);
   139             }
   140         }
   142         return square;
   143     }
   145     public int[] getRow(int y) {
   146         if (y < 0 || y > 8) {
   147             throw new IllegalArgumentException("Invalid row number");
   148         }
   149         int row[] = new int[9];
   151         for (int x = 0 ; x < 9 ; x++) {
   152             row[x] = getCellValue(x, y);
   153         }
   155         return row;
   156     }
   158     public int[] getColumn(int x) {
   159         if (x < 0 || x > 8) {
   160             throw new IllegalArgumentException("Invalid column number");
   161         }
   162         int column[] = new int[9];
   164         for (int y = 0 ; y < 9 ; y++) {
   165             column[y] = getCellValue(x, y);
   166         }
   168         return column;
   169     }
   170 }

mercurial