/** * Main Sudoku Paint. Paints everything for the sudoku board. * * @param m * @param g * @param w * @param oh */ @Override public void paint(SudokuModel m, Graphics g, int w, int oh) { this.oh = oh; this.w = w; int h = oh - 100; this.h = h; g.setFont(new Font(SANS_SERIF, PLAIN, 50)); m.checkConflict(); for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) paintSmallGrid(g, i * w / 3, j * h / 3, w / 3, h / 3); m.updateSelection(); for (int i = 0; i < 9; ++i) for (int j = 0; j < 9; ++j) paintBox(g, i * w / 9, j * h / 9, m.get(j, i), w / 9, h / 9); paintGrid(g, w, h); if (m.gameOver()) { g.setColor(Color.BLACK); g.fillRect(w / 2 - 200, h / 2 - 100, 400, 200); g.setColor(Color.RED); drawStringBox(g, w / 2 - 200, h / 2 - 200, "GAME OVER", 400, 400); } }
/** * Paints a box. * * @param g usable graphics object * @param tlx x-coordinate of the top left corner of the box * @param tly y-coordinate of the top left corner of the box * @param b the Box object * @param w * @param h */ public void paintBox(Graphics g, int tlx, int tly, Box b, int w, int h) { if (b.clue) g.setColor(Color.BLACK); else if (b.conflict) g.setColor(Color.RED); else g.setColor(Color.BLUE); drawStringBox(g, tlx, tly, (b.number == 0) ? "" : (b.number + ""), w, h); Graphics2D g2 = (Graphics2D) g; g2.setStroke(new BasicStroke(5)); g2.setColor(Color.GRAY); if (b.selected) g2.drawRect(tlx + 5, tly + 5, w - 10, h - 10); g2.setStroke(new BasicStroke(1)); }
/** * Paints the 3x3 outer grid. * * @param g * @param w * @param h */ public void paintGrid(Graphics g, int w, int h) { g.setColor(Color.BLACK); Graphics2D g2 = (Graphics2D) g; g2.setStroke(new BasicStroke(3)); g2.drawRect(0, 0, w, h); g2.drawRect(0, h / 3, w, h / 3); g2.drawRect(w / 3, 0, w / 3, h); g2.setStroke(new BasicStroke(1)); }
public void paintComponent(Graphics screen) { if (inactive == null) { inactive = new Image[BUTTON_COUNT]; rollover = new Image[BUTTON_COUNT]; active = new Image[BUTTON_COUNT]; int IMAGE_SIZE = 33; for (int i = 0; i < BUTTON_COUNT; i++) { inactive[i] = createImage(BUTTON_WIDTH, BUTTON_HEIGHT); Graphics g = inactive[i].getGraphics(); g.drawImage(buttons, -(i * IMAGE_SIZE) - 3, -2 * IMAGE_SIZE, null); rollover[i] = createImage(BUTTON_WIDTH, BUTTON_HEIGHT); g = rollover[i].getGraphics(); g.drawImage(buttons, -(i * IMAGE_SIZE) - 3, -1 * IMAGE_SIZE, null); active[i] = createImage(BUTTON_WIDTH, BUTTON_HEIGHT); g = active[i].getGraphics(); g.drawImage(buttons, -(i * IMAGE_SIZE) - 3, -0 * IMAGE_SIZE, null); } state = new int[buttonCount]; stateImage = new Image[buttonCount]; for (int i = 0; i < buttonCount; i++) { setState(i, INACTIVE, false); } } Dimension size = getSize(); if ((offscreen == null) || (size.width != width) || (size.height != height)) { offscreen = createImage(size.width, size.height); width = size.width; height = size.height; y1 = 0; y2 = BUTTON_HEIGHT; x1 = new int[buttonCount]; x2 = new int[buttonCount]; int offsetX = 3; for (int i = 0; i < buttonCount; i++) { x1[i] = offsetX; if (i == 2) x1[i] += BUTTON_GAP; x2[i] = x1[i] + BUTTON_WIDTH; offsetX = x2[i]; } } Graphics g = offscreen.getGraphics(); g.setColor(bgcolor); // getBackground()); g.fillRect(0, 0, width, height); for (int i = 0; i < buttonCount; i++) { g.drawImage(stateImage[i], x1[i], y1, null); } g.setColor(statusColor); g.setFont(statusFont); /* // if i ever find the guy who wrote the java2d api, i will hurt him. Graphics2D g2 = (Graphics2D) g; FontRenderContext frc = g2.getFontRenderContext(); float statusW = (float) statusFont.getStringBounds(status, frc).getWidth(); float statusX = (getSize().width - statusW) / 2; g2.drawString(status, statusX, statusY); */ // int statusY = (BUTTON_HEIGHT + statusFont.getAscent()) / 2; int statusY = (BUTTON_HEIGHT + g.getFontMetrics().getAscent()) / 2; g.drawString(status, buttonCount * BUTTON_WIDTH + 2 * BUTTON_GAP, statusY); screen.drawImage(offscreen, 0, 0, null); }
/** * Paints the inner, smaller grids. * * @param g * @param tlx * @param tly * @param w * @param h */ public void paintSmallGrid(Graphics g, int tlx, int tly, int w, int h) { g.setColor(Color.BLACK); g.drawRect(tlx + w / 3, tly, w / 3, h); g.drawRect(tlx, tly + h / 3, w, h / 3); }
public void paintIcon(Component c, Graphics g, int x, int y) { Color oldColor = g.getColor(); g.setColor(color); g.fill3DRect(x, y, getIconWidth(), getIconHeight(), true); g.setColor(oldColor); }