// paintWinningMessageAndLine - paints the winning message (X wins!, O wins! Tie!) and paints
  // a green line indicating the winning row/col/diagonal (if one player won)
  private void paintWinningMessageAndLine(TicTacToeBoard tttb, Graphics g, int w, int h) {
    String line = tttb.getWinningLine();
    if (line.startsWith("row")) {
      int rn = Integer.parseInt(line.substring(4));
      g.setColor(Color.GREEN);
      g.fillRect(
          (int) (0.05 * w / tttb.getSize()),
          (int)
              (h / (2.0 * tttb.getSize()) - 0.05 * h / tttb.getSize() + rn * h / (tttb.getSize())),
          (int) ((1 - .1 / tttb.getSize()) * w),
          (int) (0.1 * h / tttb.getSize()));
      g.setColor(Color.BLACK);
      g.drawRect(
          (int) (0.05 * w / tttb.getSize()),
          (int)
              (h / (2.0 * tttb.getSize()) - 0.05 * h / tttb.getSize() + rn * h / (tttb.getSize())),
          (int) ((1 - .1 / tttb.getSize()) * w),
          (int) (0.1 * h / tttb.getSize()));
    } else if (line.startsWith("col")) {
      int cn = Integer.parseInt(line.substring(4));
      g.setColor(Color.GREEN);
      g.fillRect(
          (int)
              (w / (2.0 * tttb.getSize()) - 0.05 * w / tttb.getSize() + cn * w / (tttb.getSize())),
          (int) (0.05 * h / tttb.getSize()),
          (int) (0.1 * w / tttb.getSize()),
          (int) ((1 - .1 / tttb.getSize()) * h));
      g.setColor(Color.BLACK);
      g.drawRect(
          (int)
              (w / (2.0 * tttb.getSize()) - 0.05 * w / tttb.getSize() + cn * w / (tttb.getSize())),
          (int) (0.05 * h / tttb.getSize()),
          (int) (0.1 * w / tttb.getSize()),
          (int) ((1 - .1 / tttb.getSize()) * h));
    } else if (line.startsWith("main")) {
      double po = 0.05 * Math.sqrt(2);
      int[] xs = new int[6];
      int[] ys = new int[6];
      xs[0] = (int) (po * w / tttb.getSize());
      ys[0] = (int) (po * h / tttb.getSize());
      xs[1] = xs[0];
      ys[1] = (int) (2 * po * h / tttb.getSize());
      xs[2] = (int) ((1 - 2 * po / tttb.getSize()) * w);
      ys[2] = (int) ((1 - po / tttb.getSize()) * h);
      xs[3] = (int) ((1 - po / tttb.getSize()) * w);
      ys[3] = ys[2];
      xs[4] = xs[3];
      ys[4] = (int) ((1 - 2 * po / tttb.getSize()) * h);
      xs[5] = (int) (2 * po * w / tttb.getSize());
      ys[5] = ys[0];
      g.setColor(Color.GREEN);
      g.fillPolygon(xs, ys, 6);
      g.setColor(Color.BLACK);
      g.drawPolygon(xs, ys, 6);
    } else if (line.startsWith("off")) {
      double po = 0.05 * Math.sqrt(2);
      int[] xs = new int[6];
      int[] ys = new int[6];
      xs[0] = (int) (po * w / tttb.getSize());
      ys[0] = (int) ((1 - 2 * po / tttb.getSize()) * h);
      xs[1] = xs[0];
      ys[1] = (int) ((1 - po / tttb.getSize()) * h);
      xs[2] = (int) (2 * po * w / tttb.getSize());
      ys[2] = ys[1];
      xs[3] = (int) ((1 - po / tttb.getSize()) * w);
      ys[3] = (int) (2 * po * h / tttb.getSize());
      xs[4] = xs[3];
      ys[4] = (int) (po * h / tttb.getSize());
      xs[5] = (int) ((1 - 2 * po / tttb.getSize()) * w);
      ys[5] = ys[4];
      g.setColor(Color.GREEN);
      g.fillPolygon(xs, ys, 6);
      g.setColor(Color.BLACK);
      g.drawPolygon(xs, ys, 6);
    }

    if (tttb.getWinner() != ' ') {
      g.setColor(Color.WHITE);
      g.fillRect((int) (0.1 * w), (int) (0.4 * h), (int) (0.8 * w), (int) (0.2 * h));
      g.setColor(Color.BLACK);
      g.drawRect((int) (0.1 * w), (int) (0.4 * h), (int) (0.8 * w), (int) (0.2 * h));
      GraphicsUtilityFunctions.drawStringWithFontInRectangle(
          g,
          tttb.getWinner() + " WINS!",
          GraphicsUtilityFunctions.getFont((int) (0.2 * h)),
          (int) (0.3 * w),
          (int) (0.4 * h),
          (int) (0.4 * w),
          (int) (0.2 * h));
    } else {
      g.setColor(Color.WHITE);
      g.fillRect((int) (0.1 * w), (int) (0.4 * h), (int) (0.8 * w), (int) (0.2 * h));
      g.setColor(Color.BLACK);
      g.drawRect((int) (0.1 * w), (int) (0.4 * h), (int) (0.8 * w), (int) (0.2 * h));
      GraphicsUtilityFunctions.drawStringWithFontInRectangle(
          g,
          "TIE!",
          GraphicsUtilityFunctions.getFont((int) (0.2 * h)),
          (int) (0.3 * w),
          (int) (0.4 * h),
          (int) (0.4 * w),
          (int) (0.2 * h));
    }
  }
  @Override
  public void paint(IABGModel m, Graphics g, int w, int h) {
    int r = m.getNumRows();
    int c = m.getNumColumns();

    double x =
        Math.min(w / ((r + c) * Math.cos(Math.PI / 6)), h / ((r + c) * Math.sin(Math.PI / 6)));

    double topW = x / Math.sqrt(2);
    double topH = x / Math.sqrt(2);

    BufferedImage top = new BufferedImage((int) topW, (int) topH, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D gr = top.createGraphics();
    gr.setBackground(new Color(255, 255, 255, 0));
    gr.setColor(Color.BLACK);

    Font f = GraphicsUtilityFunctions.getFont((int) (3 * topH / 5));

    AffineTransform s = AffineTransform.getScaleInstance(x * Math.sqrt(6) / (2 * topH), 1);
    AffineTransform t = AffineTransform.getRotateInstance(-Math.PI / 4);
    s.concatenate(t);

    Graphics2D g2D = (Graphics2D) g;

    double xf = c * x * Math.cos(Math.PI / 6);

    double minZUnit = Double.MAX_VALUE;

    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        double zUnit =
            m.getItem(i, j) == 0
                ? (h - x * Math.sin(Math.PI / 6) * (2 + i + j) < 0.0001 ? 0 : Double.MAX_VALUE)
                : (h - x * Math.sin(Math.PI / 6) * (2 + i + j)) / m.getItem(i, j);
        minZUnit = Math.min(minZUnit, zUnit);
      }
    }

    // minZUnit = 0;
    g.setColor(Color.BLACK);
    for (int i = r - 1; i >= 0; i--) {
      for (int j = c - 1; j >= 0; j--) {
        Color barColor = getColor(m.getItem(i, j));
        double Fx = (xf + x * Math.cos(Math.PI / 6) * (i - j));
        double Rx = Fx + x * Math.cos(Math.PI / 6);
        double Lx = Fx - x * Math.cos(Math.PI / 6);

        double BFy = (h - x * Math.sin(Math.PI / 6) * (i + j));
        double BMy = BFy - x * Math.sin(Math.PI / 6);

        double TFy = BFy - m.getItem(i, j) * minZUnit;
        double TMy = TFy - x * Math.sin(Math.PI / 6);
        double TBy = TMy - x * Math.sin(Math.PI / 6);

        int[] xs = new int[4];
        int[] ys = new int[4];

        xs[0] = (int) Fx;
        ys[0] = (int) BFy;
        xs[1] = (int) Rx;
        ys[1] = (int) BMy;
        xs[2] = (int) Rx;
        ys[2] = (int) TMy;
        xs[3] = (int) Fx;
        ys[3] = (int) TFy;

        g.setColor(colors[m.getItem(i, j) % colors.length]);
        g.setColor(barColor);
        g.fillPolygon(xs, ys, 4);
        g.setColor(Color.BLACK);
        g.drawPolygon(xs, ys, 4);

        xs[0] = (int) Fx;
        ys[0] = (int) BFy;
        xs[1] = (int) Fx;
        ys[1] = (int) TFy;
        xs[2] = (int) Lx;
        ys[2] = (int) TMy;
        xs[3] = (int) Lx;
        ys[3] = (int) BMy;

        g.setColor(barColor);
        g.fillPolygon(xs, ys, 4);
        g.setColor(Color.BLACK);
        g.drawPolygon(xs, ys, 4);

        xs[0] = (int) Fx;
        ys[0] = (int) TFy;
        xs[1] = (int) Rx;
        ys[1] = (int) TMy;
        xs[2] = (int) Fx;
        ys[2] = (int) TBy;
        xs[3] = (int) Lx;
        ys[3] = (int) TMy;

        g.setColor(barColor);
        g.fillPolygon(xs, ys, 4);
        g.setColor(Color.BLACK);
        g.drawPolygon(xs, ys, 4);

        gr.clearRect(0, 0, (int) topW, (int) topH);
        GraphicsUtilityFunctions.drawStringWithFontInRectangle(
            gr, "" + m.getItem(i, j), f, 0, 0, (int) topW, (int) topH);

        g2D.drawImage(
            top,
            new AffineTransformOp(s, AffineTransformOp.TYPE_NEAREST_NEIGHBOR),
            (int) Lx,
            (int) TMy);
      }
    }
  }
Beispiel #3
0
 /**
  * @param g graphics object
  * @param tlx top left x coordinate
  * @param tly top left y coordinate
  * @param s painted string
  * @param w width of outlying box
  * @param h height of outlying box
  */
 public void drawStringBox(Graphics g, int tlx, int tly, String s, int w, int h) {
   GraphicsUtilityFunctions.drawStringWithFontInRectangle(g, s, g.getFont(), tlx, tly, w, h);
 }