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