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

1
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 package de.uapcore.sudoku;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 import java.awt.Color;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 import java.awt.Graphics;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 import java.awt.Graphics2D;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 import java.awt.GridBagConstraints;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 import java.awt.GridBagLayout;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 import java.awt.Insets;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 import java.awt.image.BufferedImage;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 import javax.swing.JPanel;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 /**
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 *
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * @author mike
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 */
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 public class Field extends JPanel {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 private SudokuTextField[][] cells;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 public Field() {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 setBackground(Color.WHITE);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 setLayout(new GridBagLayout());
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 GridBagConstraints c = new GridBagConstraints();
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 c.insets = new Insets(5, 5, 5, 5);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 cells = new SudokuTextField[9][9];
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 for (int x = 0 ; x < 9 ; x++) {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 for (int y = 0 ; y < 9 ; y++) {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 cells[x][y] = new SudokuTextField();
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 c.gridx = x;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 c.gridy = y;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 add(cells[x][y], c);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 @Override
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 public void paint(Graphics graphics) {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 super.paint(graphics);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 final int w = getWidth();
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 final int h = getHeight();
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 final int cw = w / 9;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 final int ch = h / 9;
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 Graphics2D g = img.createGraphics();
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 g.setBackground(Color.WHITE);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 g.clearRect(0, 0, w, h);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 g.setColor(Color.BLACK);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 g.drawRect(1, 1, w-2, h-2);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 g.drawRect(2, 2, w-4, h-4);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 for (int x = cw ; x < w ; x += cw) {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 for (int y = ch ; y < h ; y += ch) {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 g.drawLine(x, 2, x, h-2);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 g.drawLine(2, y, w-2, y);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 if ((x / cw) % 3 == 0) {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 g.drawLine(x+1, 2, x+1, h-2);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 if ((y / ch) % 3 == 0) {
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 g.drawLine(2, y+1, w-2, y+1);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 graphics.drawImage(img, 0, 0, this);
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 }
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69
f1d7de36b01e init project + editable sudoku field
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 }

mercurial