Exemple #1
0
  /**
   * 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);
    }
  }
  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);
  }