/** Paint it. */ public void paint(Graphics g) { Dimension d = getSize(); if (!inited || buff == null || d.width != buffSize.width || d.height != buffSize.height) { buffSize.width = d.width; buffSize.height = d.height; buff = createImage(d.width + 4, d.height + 4); if (buff == null) return; buffG = buff.getGraphics(); inited = true; } buffG.setColor(Color.lightGray); buffG.fillRect(0, 0, prefSize.width, prefSize.height); PressedRect(buffG, 0, 0, prefSize.width - 2, prefSize.height - 2); buffG.setColor(Color.lightGray); buffG.fillRect(0, 0, prefSize.width + 2, prefSize.height + 2); if (!gameover) if ((gameover = is_game_over())) end_game(); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { drawBlock(buffG, i, j, status(i, j)); } } // PressedRect(buffG,0,0,prefSize.width + 2,prefSize.height + 2); if (buff != null) g.drawImage(buff, 0, 0, null); }
void PressedRect(Graphics g, int x, int y, int w, int h) { Color dg = new Color(128, 128, 128); g.setColor(Color.lightGray); g.drawLine(x + 1, y + h - 1, x + 1, y + h - 1); g.setColor(dg); g.drawLine(x, y, x + w - 1, y); g.drawLine(x, y, x, y + h - 1); g.setColor(Color.black); g.drawLine(x + 1, y + 1, x + w - 2, y + 1); g.drawLine(x + 1, y + 1, x + 1, y + h - 2); g.setColor(Color.white); g.drawLine(x + w, y, x + w, y + h); g.drawLine(x + w, y + h, x, y + h); }
public void drawBomb(Graphics g, int x, int y) { Color c = g.getColor(); g.setColor(new Color(128, 128, 128)); g.drawLine(x + 2, y + 7, x + 12, y + 7); g.drawLine(x + 7, y + 2, x + 7, y + 12); g.fillRect(x + 6, y + 4, 3, 7); g.fillRect(x + 4, y + 6, 7, 3); g.setColor(Color.black); g.fillRect(x + 5, y + 5, 5, 5); g.drawLine(x + 3, y + 7, x + 11, y + 7); g.drawLine(x + 7, y + 3, x + 7, y + 11); g.drawLine(x + 4, y + 4, x + 4, y + 4); g.drawLine(x + 4, y + 10, x + 4, y + 10); g.drawLine(x + 10, y + 4, x + 10, y + 4); g.drawLine(x + 10, y + 10, x + 10, y + 10); g.setColor(Color.white); g.drawLine(x + 6, y + 6, x + 6, y + 6); g.setColor(c); }
public void drawBlock(Graphics g, int r, int c, int mode) { Color curr = g.getColor(); g.setColor(Color.black); int inc = 2; int xpos = inc + (c * wt), ypos = inc + (r * ht); switch (mode) { case 1: { // drawn on a square with an unclicked // bomb drawBomb(g, xpos, ypos); break; } case 2: { // draws a cell with a number on it g.setColor(Color.lightGray); g.fillRect(xpos, ypos, wt, ht); if (field[r][c] > 0) { g.setFont(new Font("Serif", Font.BOLD, 12)); g.setColor(list[field[r][c] - 1]); g.drawString(field[r][c] + "", xpos + 5, ypos + 12); } } break; case 3: { // drawn on the square where the bomb was clicked g.setColor(Color.red); g.fillRect(xpos, ypos, wt, ht); g.setColor(Color.black); drawBomb(g, xpos, ypos); } break; case 4: { // draws a plain unrevealed cell g.setColor(Color.lightGray); g.fill3DRect(xpos, ypos, wt, ht, true); } break; case 5: { // a Flag. g.setColor(Color.lightGray); g.fill3DRect(xpos, ypos, wt, ht, true); g.setColor(Color.red); g.fillRect(xpos + 4, ypos + 3, 4, 4); g.setColor(Color.red.darker()); g.drawLine(xpos + 8, ypos + 3, xpos + 8, ypos + 6); g.drawLine(xpos + 8, ypos + 6, xpos + 5, ypos + 6); g.drawLine(xpos + 6, ypos + 5, xpos + 7, ypos + 5); g.drawLine(xpos + 7, ypos + 5, xpos + 7, ypos + 4); g.setColor(new Color(128, 128, 128)); g.drawLine(xpos + 5, ypos + 12, xpos + 11, ypos + 12); g.drawLine(xpos + 7, ypos + 11, xpos + 9, ypos + 11); g.setColor(Color.black); g.drawLine(xpos + 8, ypos + 7, xpos + 8, ypos + 11); g.drawLine(xpos + 6, ypos + 12, xpos + 10, ypos + 12); } break; case 6: { // A red cross - flag over non mine block g.setColor(Color.red); g.drawLine(xpos + 3, ypos + 3, xpos + 11, ypos + 11); g.drawLine(xpos + 4, ypos + 3, xpos + 12, ypos + 11); g.drawLine(xpos + 12, ypos + 3, xpos + 4, ypos + 11); g.drawLine(xpos + 3, ypos + 11, xpos + 11, ypos + 3); g.setColor(Color.red.darker()); g.drawLine(xpos + 3, ypos + 12, xpos + 4, ypos + 12); g.drawLine(xpos + 4, ypos + 12, xpos + 7, ypos + 9); g.drawLine(xpos + 8, ypos + 9, xpos + 11, ypos + 12); g.drawLine(xpos + 11, ypos + 12, xpos + 12, ypos + 12); g.drawLine(xpos + 3, ypos + 4, xpos + 6, ypos + 7); g.drawLine(xpos + 12, ypos + 4, xpos + 9, ypos + 7); } break; case 7: { g.setColor(Color.lightGray); g.fillRect(xpos, ypos, wt, ht); } break; } g.setColor(curr); }