src/de/uapcore/sudoku/Field.java

Sat, 26 Jan 2013 15:48:59 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 26 Jan 2013 15:48:59 +0100
changeset 1
f1d7de36b01e
child 2
5179eff8a9b6
permissions
-rw-r--r--

init project + editable sudoku field

package de.uapcore.sudoku;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

/**
 *
 * @author mike
 */
public 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) {
        super.paint(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);
    }
    
    
}

mercurial