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