Пример #1
0
 public void paint(Graphics canvas) {
   this.setBackground(Color.WHITE);
   canvas.setColor(Color.GREEN);
   canvas.fillPolygon(xBranches, yBranches, xBranches.length);
   canvas.setColor(Color.GRAY);
   canvas.fillPolygon(xTrunk, yTrunk, xTrunk.length);
 }
Пример #2
0
  public void paint(Graphics g) {
    super.paint(g);

    Dimension size = getSize();
    double w = size.getWidth() - 50;
    double h = size.getHeight() + 20;

    try {
      readFile();
    } catch (IOException e) {
      System.err.println("IO issue");
    }

    barWidth = ((int) (w / numBars)) - 20;
    pos = 5;

    int xPos[] = {pos, pos, pos, pos};
    int yPos[] = {pos, pos, pos, pos};

    maxVal = maxValue(values);
    double barH, ratio;

    for (int i = 0; i < numBars - 1; i++) {
      Color col[] = new Color[numBars];
      for (int j = 0; j < numBars - 1; j++) {
        col[j] = new Color((int) (Math.random() * 0x1000000));
      }

      ratio = (double) values[i] / (double) maxVal;
      barH = ((h) * ratio) - 10;

      xPos[0] = pos;
      xPos[2] = pos + barWidth;
      xPos[1] = xPos[0];
      xPos[3] = xPos[2];
      yPos[0] = (int) h;
      yPos[1] = (int) barH;
      yPos[2] = yPos[1];
      yPos[3] = yPos[0];

      System.out.println(
          "xPos:" + xPos[1] + " yPos:" + yPos[0] + " h:" + h + " barH:" + barH + " ratio:" + ratio
              + " pos:" + pos);

      int stringPtsY[] = {
        ((i + 1) * 20) + 180, ((i + 1) * 20) + 200, ((i + 1) * 20) + 200, ((i + 1) * 20) + 180
      };
      int stringPtsX[] = {600, 600, 580, 580};

      g.setColor(col[i]);
      g.fillPolygon(xPos, yPos, xPos.length);

      g.fillPolygon(stringPtsX, stringPtsY, 4);
      g.setColor(Color.black);
      g.drawString(labels[i], 610, ((i + 1) * 20) + 195);
      pos = pos + barWidth + 10;
    }
  }
Пример #3
0
 public static void fillPolygon(
     Graphics g,
     int x1,
     int y1,
     int x2,
     int y2,
     int x3,
     int y3,
     int x4,
     int y4,
     int x5,
     int y5,
     int x6,
     int y6,
     int x7,
     int y7,
     int x8,
     int y8,
     int x9,
     int y9,
     int x10,
     int y10) {
   Polygon myPoly = new Polygon();
   myPoly.addPoint(x1, y1);
   myPoly.addPoint(x2, y2);
   myPoly.addPoint(x3, y3);
   myPoly.addPoint(x4, y4);
   myPoly.addPoint(x5, y5);
   myPoly.addPoint(x6, y6);
   myPoly.addPoint(x7, y7);
   myPoly.addPoint(x8, y8);
   myPoly.addPoint(x9, y9);
   myPoly.addPoint(x10, y10);
   g.fillPolygon(myPoly);
 }
Пример #4
0
 /**
  * Draws a solid "filled-in" irregular polygon using between 3 and 12 sets of provided
  * coordinates. <br>
  * Examples: <br>
  * Expo.fillPolygon(g,100,300,200,100,300,300); // for a triangle
  * Expo.fillPolygon(g,525,300,600,250,650,250,725,300,725,350,650,400); // for a hexagon
  */
 public static void fillPolygon(Graphics g, int x1, int y1, int x2, int y2, int x3, int y3) {
   Polygon myPoly = new Polygon();
   myPoly.addPoint(x1, y1);
   myPoly.addPoint(x2, y2);
   myPoly.addPoint(x3, y3);
   g.fillPolygon(myPoly);
 }
Пример #5
0
  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    if (drawOverlay) {
      g = g.create();
      AntialiasingManager.activateAntialiasing(g);

      try {
        // Paint a roll over fade out.
        FadeTracker fadeTracker = FadeTracker.getInstance();

        float visibility = 0.0f;
        if (fadeTracker.isTracked(this, FadeKind.ROLLOVER)) {
          visibility = fadeTracker.getFade(this, FadeKind.ROLLOVER);
          visibility /= 4;
        } else visibility = 0.5f;

        // Draw black overlay
        g.setColor(new Color(0.0f, 0.0f, 0.0f, visibility));
        g.fillRoundRect(1, 1, width - 2, height - 2, 10, 10);

        // Draw arrow
        g.setColor(Color.WHITE);

        int[] arrowX = new int[] {width - 17, width - 7, width - 12};
        int[] arrowY = new int[] {height - 12, height - 12, height - 7};
        g.fillPolygon(arrowX, arrowY, arrowX.length);
      } finally {
        g.dispose();
      }
    }
  }
  private void paintObstacle(Graphics g, Obstacle obstacle) {
    Polygon polygon = obstacle.getPolygon();
    boolean opaque = obstacle.getOpaque();

    if (opaque) {
      g.setColor(obstacle.getOpaqueBackgroundColor());
      g.fillPolygon(polygon);
      g.setColor(obstacle.getOpaqueForegroundColor());
      g.drawPolygon(polygon);
    } else {
      g.setColor(obstacle.getBackgroundColor());
      g.fillPolygon(polygon);
      g.setColor(obstacle.getForegroundColor());
      g.drawPolygon(polygon);
    }
  }
Пример #7
0
  /**
   * Draws a solid "filled in" star with a specified number of points.<br>
   * The center of this star is specified by centerX,centerY and its size is specified by radius
   * <br>
   * (As in the radius of the circle the star would fit inside). <br>
   * Precondition: points >= 2 <br>
   * Example: <br>
   * Expo.fillStar(g,300,200,100,8); <br>
   * Draws a solid star with 8 points and a radius of 100 pixels whose center is located at the
   * coordinate (300,200).
   */
  public static void fillStar(Graphics g, int centerX, int centerY, int radius, int points) {
    int halfRadius = getHalfRadius(radius, points);
    int p = points;
    points *= 2;

    int xCoord[] = new int[points];
    int yCoord[] = new int[points];

    int currentRadius;

    for (int k = 0; k < points; k++) {
      if (k % 2 == 0) currentRadius = radius;
      else currentRadius = halfRadius;

      xCoord[k] = (int) Math.round(Math.cos(twoPI * k / points - halfPI) * currentRadius) + centerX;
      yCoord[k] = (int) Math.round(Math.sin(twoPI * k / points - halfPI) * currentRadius) + centerY;
    }

    int x = (p - 5) / 2 + 1;
    if (p >= 5 && p <= 51)
      switch (p % 4) {
        case 1:
          yCoord[x] = yCoord[x + 1] = yCoord[points - x - 1] = yCoord[points - x];
          break;
        case 2:
          yCoord[x] = yCoord[x + 1] = yCoord[points - x - 1] = yCoord[points - x];
          yCoord[x + 3] = yCoord[x + 4] = yCoord[points - x - 4] = yCoord[points - x - 3];
          break;
        case 3:
          yCoord[x + 2] = yCoord[x + 3] = yCoord[points - x - 3] = yCoord[points - x - 2];
      }
    g.fillPolygon(xCoord, yCoord, points);
  }
Пример #8
0
  @Override
  public void repaint(Graphics g) {
    if (tiles.size() >= 2) {
      g.setColor(new Color(255, 255, 255, 255));
      Tile[] t = new Tile[tiles.size()];
      tiles.toArray(t);
      Area a = new Area(t);
      for (int i = 0; i < a.getPolygon().npoints; i++) {
        Point p =
            new Point(
                tiles.get(i).matrix(ctx).bounds().xpoints[0],
                tiles.get(i).matrix(ctx).bounds().ypoints[0]);
        Point pp;
        if (i == a.getPolygon().npoints - 1)
          pp =
              new Point(
                  tiles.get(0).matrix(ctx).bounds().xpoints[0],
                  tiles.get(0).matrix(ctx).bounds().ypoints[0]);
        else
          pp =
              new Point(
                  tiles.get(i + 1).matrix(ctx).bounds().xpoints[0],
                  tiles.get(i + 1).matrix(ctx).bounds().ypoints[0]);
        g.drawLine(p.x, p.y, pp.x, pp.y);
      }
      g.drawPolygon(a.getPolygon());

      if (a.contains(ctx.players.local())) {
        g.setColor(new Color(0, 255, 0, 100));
      } else {
        g.setColor(new Color(255, 0, 0, 100));
      }
      g.fillPolygon(ctx.players.local().tile().matrix(ctx).bounds());
    } else {
      g.setColor(new Color(255, 0, 0, 100));
      g.fillPolygon(ctx.players.local().tile().matrix(ctx).bounds());
    }
    for (int i = 0; i < tiles.size(); i++) {
      if (i == gui.tileList.getSelectedIndex()) g.setColor(Color.yellow);
      else g.setColor(new Color(0, 0, 255, 150));
      g.fillPolygon(tiles.get(i).matrix(ctx).bounds());
    }
  }
Пример #9
0
    /**
     * Paints the icon.
     *
     * @param g context.
     */
    public void paint(Graphics g) {
      Graphics2D g2 = (Graphics2D) g;

      g.setColor(Color.WHITE);

      Object aaval = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      if (collapsed) {
        int arrowX = 1;
        int arrowY = 0;
        g.fillPolygon(
            new int[] {arrowX, arrowX + 7, arrowX}, new int[] {arrowY, arrowY + 4, arrowY + 9}, 3);
      } else {
        int arrowX = 0;
        int arrowY = 1;
        g.fillPolygon(
            new int[] {arrowX, arrowX + 4, arrowX + 9}, new int[] {arrowY, arrowY + 7, arrowY}, 3);
      }
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaval);
    }
Пример #10
0
  /**
   * Draws a solid "filled in" regular polygon with a specified number of sides.<br>
   * The center of this polygon is specified by centerX,centerY and its size is specified by radius
   * <br>
   * (As in the radius of the circle the regular polygon would fit inside). <br>
   * Precondition: sides >= 3 <br>
   * Example: Expo.fillRegularPolygon(g,300,200,100,8); Draws a solid "filled-in" regular octagon
   * with a radius of 100 pixels whose center is located at the coordinate (300,200).
   */
  public static void fillRegularPolygon(
      Graphics g, int centerX, int centerY, int radius, int sides) {
    int xCoord[] = new int[sides];
    int yCoord[] = new int[sides];

    double rotate;
    if (sides % 2 == 1) rotate = halfPI;
    else rotate = halfPI + Math.PI / sides;

    for (int k = 0; k < sides; k++) {
      xCoord[k] = (int) Math.round(Math.cos(twoPI * k / sides - rotate) * radius) + centerX;
      yCoord[k] = (int) Math.round(Math.sin(twoPI * k / sides - rotate) * radius) + centerY;
    }
    if (sides == 3) yCoord[1] = yCoord[2];
    g.fillPolygon(xCoord, yCoord, sides);
  }
Пример #11
0
    public void paintIcon(Component c, Graphics g, int x, int y) {
      int w = getIconWidth();
      int h = w;
      Polygon p = new Polygon();
      switch (direction) {
        case EAST:
          p.addPoint(x + 2, y);
          p.addPoint(x + w - 2, y + h / 2);
          p.addPoint(x + 2, y + h - 1);
          break;

        case SOUTH:
          p.addPoint(x, y + 2);
          p.addPoint(x + w / 2, y + h - 2);
          p.addPoint(x + w - 1, y + 2);
          break;
      }
      g.fillPolygon(p);
    }
  private void paintDevice(Graphics g, Device d) {
    // reads the robot's current position

    model.getRobot().readPosition(d.getRobotPosition());
    Polygon currentShape = d.getShape();
    // draws the shape
    Polygon globalShape = new Polygon();
    Point2D point = new Point2D.Double();
    for (int i = 0; i < currentShape.npoints; i++) {
      point.setLocation(currentShape.xpoints[i], currentShape.ypoints[i]);
      // calculates the coordinates of the point according to the local position
      d.getLocalPosition().rotateAroundAxis(point);
      // calculates the coordinates of the point according to the robot position
      d.getRobotPosition().rotateAroundAxis(point);
      // adds the point to the global shape
      globalShape.addPoint((int) Math.round(point.getX()), (int) Math.round(point.getY()));
    }
    g.setColor(d.getBackgroundColor());
    g.fillPolygon(globalShape);
    g.setColor(d.getForegroundColor());
    g.drawPolygon(globalShape);
  }
Пример #13
0
 public static void fillPolygon(
     Graphics g,
     int x1,
     int y1,
     int x2,
     int y2,
     int x3,
     int y3,
     int x4,
     int y4,
     int x5,
     int y5,
     int x6,
     int y6) {
   Polygon myPoly = new Polygon();
   myPoly.addPoint(x1, y1);
   myPoly.addPoint(x2, y2);
   myPoly.addPoint(x3, y3);
   myPoly.addPoint(x4, y4);
   myPoly.addPoint(x5, y5);
   myPoly.addPoint(x6, y6);
   g.fillPolygon(myPoly);
 }
  public void flecha(Graphics papel, int x1, int y1, int x2, int y2) {
    double ang = 0.0, angSep = 0.0;
    double tx = 0, ty = 0;
    int dist = 0;
    Point punto1 = null, punto2 = null;

    punto2 = new Point(x1, y1);
    punto1 = new Point(x2, y2);

    dist = 15;

    ty = -(punto1.y - punto2.y) * 1.0;
    tx = (punto1.x - punto2.x) * 1.0;
    ang = Math.atan(ty / tx);

    if (tx < 0) ang += Math.PI;
    Point p1 = new Point(), p2 = new Point(), punto = punto2;
    angSep = 25.0;

    p1.x = (int) (punto.x + dist * Math.cos(ang - Math.toRadians(angSep)));
    p1.y = (int) (punto.y - dist * Math.sin(ang - Math.toRadians(angSep)));
    p2.x = (int) (punto.x + dist * Math.cos(ang + Math.toRadians(angSep)));
    p2.y = (int) (punto.y - dist * Math.sin(ang + Math.toRadians(angSep)));

    Graphics2D g2D = (Graphics2D) papel;
    papel.setColor(Color.black);
    g2D.setStroke(new BasicStroke(1.2f));
    papel.drawLine(punto1.x, punto1.y, punto.x, punto.y);

    int x[] = {p1.x, punto.x, p2.x};
    int y[] = {p1.y, punto.y, p2.y};
    Polygon myTri = new Polygon(x, y, 3);
    papel.setColor(Color.BLACK);
    papel.drawPolygon(myTri);
    papel.fillPolygon(myTri);
  }
Пример #15
0
  // -----------------------------------------------------------------
  //  Draws a snowman.
  // -----------------------------------------------------------------
  public void paintComponent(Graphics page) {
    super.paintComponent(page);
    final int MID = 170;
    final int TOP = 50;

    // constants for snow body
    final int head = 40;
    final int UP_T[] = {70, 50};
    final int LOW_T[] = {100, 60};

    // coordinates for shadow hat polygon
    final int hatX[] = {MID - 70, MID - 50, MID - 65, MID - 85};
    final int hatY[] = {TOP + 168, TOP + 173, TOP + 193, TOP + 188};

    // coordinates for sun rays
    final int ray1X[] = {250, 255, -100, -110};
    final int ray1Y[] = {5, 15, 95, 45};

    final int ray2X[] = {260, 280, 170, -130};
    final int ray2Y[] = {30, 45, 360, 220};

    final char name[] = {'E', '.', 'Z', '.'};

    setBackground(Color.blue);

    page.setColor(Color.yellow);
    page.fillOval(260, -40, 80, 80); // sun

    page.fillPolygon(ray1X, ray1Y, 4); // upper sun ray
    page.fillPolygon(ray2X, ray2Y, 4); // lower sun ray

    // ground changed to white
    page.setColor(Color.white);
    page.fillRect(0, 175, 300, 50); // ground

    // shadow body added
    page.setColor(Color.lightGray);
    page.fillOval(MID - 60, TOP + 120, LOW_T[0], LOW_T[1] / 2); // lower torso shadow
    page.fillOval(MID - 65, TOP + 140, UP_T[0], UP_T[1] / 2); // upper torso shadow
    page.fillOval(MID - 70, TOP + 155, head, head / 2); // head shadow

    page.drawLine(MID - 75, TOP + 165, MID - 45, TOP + 180); // hat brim shadow
    page.drawLine(MID - 75, TOP + 166, MID - 45, TOP + 181); // thicker brim shadow
    page.fillPolygon(hatX, hatY, 4); // had shadow

    page.setColor(Color.white);
    page.fillOval(MID - 20, TOP, 40, 40); // head
    page.fillOval(MID - 35, TOP + 35, UP_T[0], UP_T[1]); // upper torso
    page.fillOval(MID - 50, TOP + 80, LOW_T[0], LOW_T[1]); // lower torso

    page.setColor(Color.red);
    page.fillOval(MID - 5, TOP + 45, 10, 10); // 1st button
    page.fillOval(MID - 5, TOP + 65, 10, 10); // 2nd button

    page.setColor(Color.black);
    page.fillOval(MID - 10, TOP + 10, 5, 5); // left eye
    page.fillOval(MID + 5, TOP + 10, 5, 5); // right eye

    page.drawArc(MID - 10, TOP + 25, 20, 10, 10, 160); // frown, was smile

    page.drawLine(MID - 25, TOP + 60, MID - 50, TOP + 40); // left arm
    page.drawLine(MID + 25, TOP + 60, MID + 55, TOP + 60); // right arm

    page.drawLine(MID - 20, TOP + 5, MID + 20, TOP + 5); // brim of hat
    page.drawLine(MID - 20, TOP + 4, MID + 20, TOP + 4); // thicker brim
    page.fillRect(MID - 15, TOP - 20, 30, 25); // top of hat

    page.setColor(Color.white);
    page.drawChars(name, 0, 4, 10, 20); // display name
  }
Пример #16
0
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    int width = this.getWidth();
    int height = this.getHeight();
    if (!isDisabled) {
      if (this.state == FLAG) {
        g.fillRect(width / 3, height / 5, 2, 3 * height / 5);
        g.setColor(Color.RED);
        g.fillPolygon(
            new int[] {width / 3 + 2, 2 * width / 3, width / 3 + 2},
            new int[] {height / 5, 3 * height / 5, 3 * height / 5},
            3);
      }
    } else {
      switch (state) {
        case MINE: // draw mine in the button
          this.disableButton();
          if (thisMineClicked) g.setColor(Color.RED);
          else g.setColor(Color.BLACK);
          g.fillOval(width / 2 - 5, height / 2 - 5, 10, 10);
          g.fillRect(width / 2 - 1, height / 5, 3, 3 * height / 5);
          g.fillRect(width / 5, height / 2 - 1, 3 * width / 5, 3);
          break;
        case NUMBER:
          switch (num) {
            case 1:
              g.setColor(Color.GREEN);
              g.drawString("1", width / 3, 2 * height / 3);
              break;
            case 2:
              g.setColor(Color.BLUE);
              g.drawString("2", width / 3, 2 * height / 3);
              break;
            case 3:
              g.setColor(Color.RED);
              g.drawString("3", width / 3, 2 * height / 3);
              break;
            case 4:
              g.setColor(Color.DARK_GRAY);
              g.drawString("4", width / 3, 2 * height / 3);
              break;
            case 5:
              g.setColor(Color.MAGENTA);
              g.drawString("5", width / 3, 2 * height / 3);
              break;
            case 6:
              g.setColor(Color.ORANGE);
              g.drawString("6", width / 3, 2 * height / 3);
              break;
            case 7:
              g.setColor(Color.PINK);
              g.drawString("7", width / 3, 2 * height / 3);
              break;
            case 8:
              g.setColor(Color.YELLOW);
              g.drawString("8", width / 3, 2 * height / 3);
              break;
            default:
              break;
          }
        default:
          break;
      }
    }
  }
Пример #17
0
  /**
   * Draws the stars on the flag. Two integer arrays are used to store the x-coordinates and
   * y-coordinates of a star (a total of 10 points for each of the 10 vertices). The outer radius is
   * then calculated by multiplying the actual height by the star diameter ratio then dividing by 2.
   * The inner radius is then calculated using trigonometric relationships between the first point
   * (outer right most point) and the point directly to the left of it (the inner right most point),
   * based on the assumption that the y-coordinates of these points are the same. The method then
   * uses a series of for loops to draw the stars, drawing them row by row and 1 by 1 across each
   * row. The outer most for loop increments each row while also determining the number of stars in
   * each row and the initial x offset for the first star, both of which vary depending on the row
   * number. The intermediate for loop increments each column while also determining the X and Y
   * coordinate centers for each star. The inner most for loop generates the x and y coordinates for
   * each of the 10 vertices for each star and stores them in their respective arrays to be used by
   * the fillPolygon method to draw each star.
   *
   * @param g The graphics component on which to draw the stars
   * @param initialWidthOffset The initial width offset towards the right from the left side of the
   *     frame
   * @param initialHeightOffset The initial height offset towards the bottom from the top of the
   *     frame
   * @param actualWidth The actual width of the window, not including the frame border
   * @param actualHeight The actual height of the window, not including the frame border
   */
  public void drawStars(
      Graphics g,
      int initialWidthOffset,
      int initialHeightOffset,
      int actualWidth,
      int actualHeight) {

    g.setColor(Color.WHITE);

    int[] xPoints = new int[10];
    int[] yPoints = new int[10];

    // outer radius
    double r1 = actualHeight * (STAR_DIAMETER / 2);

    // inner radius
    double r2 = r1 * Math.sin(Math.toRadians(18)) / Math.sin(Math.toRadians(54));

    for (int row = 0; row < 9; row++) {

      /* The number of stars per row initially begin at 6 for the first row and alternate between
       * 5 and 6 for each successive row.
       */
      int starsPerRow = (row % 2 == 0) ? 6 : 5;

      /* The initial star x offset is initially 1 for the first row, but alternates between 2
       * and 1 for each successive row.
       */
      int initialStarXOffset = (row % 2 == 0) ? 1 : 2;

      for (int col = 0; col < starsPerRow; col++) {

        /* The x-coordinate center is calculated based on the initial width offset and the star x
         * offset proportional to the actual height, in addition to the initial star x offset and the
         * column number. The x-coordinate center is incremented by the 2 times the star x offset in
         * proportion to the actual height for each iteration of the intermediate for loop. The
         * y-coordinate center is calculated based on the initial height offset and the star y offset
         * proportional to the actual height, in addition to the row number. The y-coordinate center is
         * incremented by the star y offset in proportion to the actual height for each iteration of the
         * outer most for loop.
         */
        int starCenterX =
            (initialWidthOffset)
                + (int) ((actualHeight * STAR_X_OFFSET) * (initialStarXOffset + 2 * col));
        int starCenterY =
            (initialHeightOffset) + (int) ((actualHeight * STAR_Y_OFFSET) * (1 + row));

        for (int i = 0; i < 10; i++) {

          /* Assigns different x and y coordinates for each of the 10 points on a star depending on
           * whether the point is an even/outer point (i % 2) == 0, or an odd/inner point. Points are
           * calculated based on the center x and y coordinates and with the trigonometric sin and cos
           * functions. The angle measure is incremented by 36 degrees for each iteration of the for loop, for
           * use in the cos and sin functions.
           */
          xPoints[i] =
              (i % 2) == 0
                  ? starCenterX + (int) (r1 * Math.cos(Math.toRadians(18 + 36 * i)))
                  : starCenterX + (int) (r2 * Math.cos(Math.toRadians(54 + 36 * (i - 1))));
          yPoints[i] =
              (i % 2) == 0
                  ? starCenterY - (int) (r1 * Math.sin(Math.toRadians(18 + 36 * i)))
                  : starCenterY - (int) (r2 * Math.sin(Math.toRadians(54 + 36 * (i - 1))));
        }

        g.fillPolygon(xPoints, yPoints, 10);
      }
    }
  }
Пример #18
0
  public void drawHexBoard(Graphics g) {
    g.setColor(Color.BLACK);
    double mx1, mx2, mx3, mx4, my1, my2, my3, my4; // Margin attributes

    mx1 = data.a[0][0].x - bst.scale * (r3 / 2 + 0.5);
    mx2 = data.a[0][bst.getOrder() - 1].x + bst.scale * (r3 + 1);
    mx3 = data.a[bst.getOrder() - 1][0].x - bst.scale * (r3 + 1);
    mx4 = data.a[bst.getOrder() - 1][bst.getOrder() - 1].x + bst.scale * (r3 / 2 + 0.5);
    my1 = data.a[0][0].y - bst.scale * 1.5;
    my2 = data.a[0][bst.getOrder() - 1].y - bst.scale * 1.5;
    my3 = data.a[bst.getOrder() - 1][0].y + bst.scale * 1.5;
    my4 = data.a[bst.getOrder() - 1][bst.getOrder() - 1].y + bst.scale * 1.5;

    g.fillPolygon(
        getIntArray(new double[] {mx1, mx2, mx4, mx3}),
        getIntArray(new double[] {my1, my2, my4, my3}),
        4);
    switch (gst.theme) {
      case 0:
        g.setColor(Color.RED);
        break;
      case 1:
        g.setColor(Color.BLACK);
        break;
      case 2:
        g.setColor(Color.YELLOW);
        break;
    }
    g.fillPolygon(
        getIntArray(new double[] {mx1 + 1, mx3 + 1, (mx2 + mx3) / 2}),
        getIntArray(new double[] {my1 + 1, my3 - 1, (my2 + my3) / 2}),
        3);
    g.fillPolygon(
        getIntArray(new double[] {mx2 - 1, mx4 - 1, (mx2 + mx3) / 2}),
        getIntArray(new double[] {my2 + 1, my4 - 1, (my2 + my3) / 2}),
        3);
    switch (gst.theme) {
      case 0:
        g.setColor(Color.BLUE);
        break;
      case 1:
        g.setColor(new Color(1, 175, 1));
        break;
      case 2:
        g.setColor(Color.lightGray);
    }
    g.fillPolygon(
        getIntArray(new double[] {mx1 + 1, mx2 - 1, (mx2 + mx3) / 2}),
        getIntArray(new double[] {my1 + 1, my2 + 1, (my2 + my3) / 2}),
        3);
    g.fillPolygon(
        getIntArray(new double[] {mx3 + 1, mx4 - 1, (mx2 + mx3) / 2}),
        getIntArray(new double[] {my3 - 1, my4 - 1, (my2 + my3) / 2}),
        3);

    for (int i = 0; i < bst.getOrder(); i++)
      for (int j = 0; j < bst.getOrder(); j++) {
        g.setColor(Color.BLACK);
        g.fillPolygon(getIntArray(data.a[i][j].polyX), getIntArray(data.a[i][j].polyY), 6);
        g.setColor(Color.WHITE);
        g.fillPolygon(getIntArray(data.a[i][j].polyXIn()), getIntArray(data.a[i][j].polyYIn()), 6);
        g.setColor(Color.BLACK);
        double x = data.a[i][j].x, y = data.a[i][j].y;
        g.drawLine((int) x, (int) y, (int) x + 1, (int) y + 1);
        circle(g, x, y, data.a[i][j].getState());
      }
    if (ptm == null) return;
    if (ptm.length == 0) return;
    for (int i = 0; i < ptm.length; i++) {
      g.setColor(Color.YELLOW);
      g.fillPolygon(
          getIntArray(data.a[ptm[i][0]][ptm[i][1]].polyXIn()),
          getIntArray(data.a[ptm[i][0]][ptm[i][1]].polyYIn()),
          6);
      g.setColor(Color.BLACK);
      double x = data.a[ptm[i][0]][ptm[i][1]].x, y = data.a[ptm[i][0]][ptm[i][1]].y;
      circle(g, x, y, data.a[ptm[i][0]][ptm[i][1]].getState());
    }
  }
Пример #19
0
  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    int width = getWidth() - rightMargin - leftMargin - 10;
    int height = getHeight() - topMargin - bottomMargin;
    if (width <= 0 || height <= 0) {
      // not enough room to paint anything
      return;
    }

    Color oldColor = g.getColor();
    Font oldFont = g.getFont();
    Color fg = getForeground();
    Color bg = getBackground();
    boolean bgIsLight = (bg.getRed() > 200 && bg.getGreen() > 200 && bg.getBlue() > 200);

    ((Graphics2D) g)
        .setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    if (smallFont == null) {
      smallFont = oldFont.deriveFont(9.0F);
    }

    r.x = leftMargin - 5;
    r.y = topMargin - 8;
    r.width = getWidth() - leftMargin - rightMargin;
    r.height = getHeight() - topMargin - bottomMargin + 16;

    if (border == null) {
      // By setting colors here, we avoid recalculating them
      // over and over.
      border =
          new BevelBorder(
              BevelBorder.LOWERED,
              getBackground().brighter().brighter(),
              getBackground().brighter(),
              getBackground().darker().darker(),
              getBackground().darker());
    }

    border.paintBorder(this, g, r.x, r.y, r.width, r.height);

    // Fill background color
    g.setColor(bgColor);
    g.fillRect(r.x + 2, r.y + 2, r.width - 4, r.height - 4);
    g.setColor(oldColor);

    long tMin = Long.MAX_VALUE;
    long tMax = Long.MIN_VALUE;
    long vMin = Long.MAX_VALUE;
    long vMax = 1;

    int w = getWidth() - rightMargin - leftMargin - 10;
    int h = getHeight() - topMargin - bottomMargin;

    if (times.size > 1) {
      tMin = Math.min(tMin, times.time(0));
      tMax = Math.max(tMax, times.time(times.size - 1));
    }
    long viewRangeMS;
    if (viewRange > 0) {
      viewRangeMS = viewRange * MINUTE;
    } else {
      // Display full time range, but no less than a minute
      viewRangeMS = Math.max(tMax - tMin, 1 * MINUTE);
    }

    // Calculate min/max values
    for (Sequence seq : seqs) {
      if (seq.size > 0) {
        for (int i = 0; i < seq.size; i++) {
          if (seq.size == 1 || times.time(i) >= tMax - viewRangeMS) {
            long val = seq.value(i);
            if (val > Long.MIN_VALUE) {
              vMax = Math.max(vMax, val);
              vMin = Math.min(vMin, val);
            }
          }
        }
      } else {
        vMin = 0L;
      }
      if (unit == Unit.BYTES || !seq.isPlotted) {
        // We'll scale only to the first (main) value set.
        // TODO: Use a separate property for this.
        break;
      }
    }

    // Normalize scale
    vMax = normalizeMax(vMax);
    if (vMin > 0) {
      if (vMax / vMin > 4) {
        vMin = 0;
      } else {
        vMin = normalizeMin(vMin);
      }
    }

    g.setColor(fg);

    // Axes
    // Draw vertical axis
    int x = leftMargin - 18;
    int y = topMargin;
    FontMetrics fm = g.getFontMetrics();

    g.drawLine(x, y, x, y + h);

    int n = 5;
    if (("" + vMax).startsWith("2")) {
      n = 4;
    } else if (("" + vMax).startsWith("3")) {
      n = 6;
    } else if (("" + vMax).startsWith("4")) {
      n = 4;
    } else if (("" + vMax).startsWith("6")) {
      n = 6;
    } else if (("" + vMax).startsWith("7")) {
      n = 7;
    } else if (("" + vMax).startsWith("8")) {
      n = 8;
    } else if (("" + vMax).startsWith("9")) {
      n = 3;
    }

    // Ticks
    ArrayList<Long> tickValues = new ArrayList<Long>();
    tickValues.add(vMin);
    for (int i = 0; i < n; i++) {
      long v = i * vMax / n;
      if (v > vMin) {
        tickValues.add(v);
      }
    }
    tickValues.add(vMax);
    n = tickValues.size();

    String[] tickStrings = new String[n];
    for (int i = 0; i < n; i++) {
      long v = tickValues.get(i);
      tickStrings[i] = getSizeString(v, vMax);
    }

    // Trim trailing decimal zeroes.
    if (decimals > 0) {
      boolean trimLast = true;
      boolean removedDecimalPoint = false;
      do {
        for (String str : tickStrings) {
          if (!(str.endsWith("0") || str.endsWith("."))) {
            trimLast = false;
            break;
          }
        }
        if (trimLast) {
          if (tickStrings[0].endsWith(".")) {
            removedDecimalPoint = true;
          }
          for (int i = 0; i < n; i++) {
            String str = tickStrings[i];
            tickStrings[i] = str.substring(0, str.length() - 1);
          }
        }
      } while (trimLast && !removedDecimalPoint);
    }

    // Draw ticks
    int lastY = Integer.MAX_VALUE;
    for (int i = 0; i < n; i++) {
      long v = tickValues.get(i);
      y = topMargin + h - (int) (h * (v - vMin) / (vMax - vMin));
      g.drawLine(x - 2, y, x + 2, y);
      String s = tickStrings[i];
      if (unit == Unit.PERCENT) {
        s += "%";
      }
      int sx = x - 6 - fm.stringWidth(s);
      if (y < lastY - 13) {
        if (checkLeftMargin(sx)) {
          // Wait for next repaint
          return;
        }
        g.drawString(s, sx, y + 4);
      }
      // Draw horizontal grid line
      g.setColor(Color.lightGray);
      g.drawLine(r.x + 4, y, r.x + r.width - 4, y);
      g.setColor(fg);
      lastY = y;
    }

    // Draw horizontal axis
    x = leftMargin;
    y = topMargin + h + 15;
    g.drawLine(x, y, x + w, y);

    long t1 = tMax;
    if (t1 <= 0L) {
      // No data yet, so draw current time
      t1 = System.currentTimeMillis();
    }
    long tz = timeDF.getTimeZone().getOffset(t1);
    long tickInterval = calculateTickInterval(w, 40, viewRangeMS);
    if (tickInterval > 3 * HOUR) {
      tickInterval = calculateTickInterval(w, 80, viewRangeMS);
    }
    long t0 = tickInterval - (t1 - viewRangeMS + tz) % tickInterval;
    while (t0 < viewRangeMS) {
      x = leftMargin + (int) (w * t0 / viewRangeMS);
      g.drawLine(x, y - 2, x, y + 2);

      long t = t1 - viewRangeMS + t0;
      String str = formatClockTime(t);
      g.drawString(str, x, y + 16);
      // if (tickInterval > (1 * HOUR) && t % (1 * DAY) == 0) {
      if ((t + tz) % (1 * DAY) == 0) {
        str = formatDate(t);
        g.drawString(str, x, y + 27);
      }
      // Draw vertical grid line
      g.setColor(Color.lightGray);
      g.drawLine(x, topMargin, x, topMargin + h);
      g.setColor(fg);
      t0 += tickInterval;
    }

    // Plot values
    int start = 0;
    int nValues = 0;
    int nLists = seqs.size();
    if (nLists > 0) {
      nValues = seqs.get(0).size;
    }
    if (nValues == 0) {
      g.setColor(oldColor);
      return;
    } else {
      Sequence seq = seqs.get(0);
      // Find starting point
      for (int p = 0; p < seq.size; p++) {
        if (times.time(p) >= tMax - viewRangeMS) {
          start = p;
          break;
        }
      }
    }

    // Optimization: collapse plot of more than four values per pixel
    int pointsPerPixel = (nValues - start) / w;
    if (pointsPerPixel < 4) {
      pointsPerPixel = 1;
    }

    // Draw graphs
    // Loop backwards over sequences because the first needs to be painted on top
    for (int i = nLists - 1; i >= 0; i--) {
      int x0 = leftMargin;
      int y0 = topMargin + h + 1;

      Sequence seq = seqs.get(i);
      if (seq.isPlotted && seq.size > 0) {
        // Paint twice, with white and with color
        for (int pass = 0; pass < 2; pass++) {
          g.setColor((pass == 0) ? Color.white : seq.color);
          int x1 = -1;
          long v1 = -1;
          for (int p = start; p < nValues; p += pointsPerPixel) {
            // Make sure we get the last value
            if (pointsPerPixel > 1 && p >= nValues - pointsPerPixel) {
              p = nValues - 1;
            }
            int x2 = (int) (w * (times.time(p) - (t1 - viewRangeMS)) / viewRangeMS);
            long v2 = seq.value(p);
            if (v2 >= vMin && v2 <= vMax) {
              int y2 = (int) (h * (v2 - vMin) / (vMax - vMin));
              if (x1 >= 0 && v1 >= vMin && v1 <= vMax) {
                int y1 = (int) (h * (v1 - vMin) / (vMax - vMin));

                if (y1 == y2) {
                  // fillrect is much faster
                  g.fillRect(x0 + x1, y0 - y1 - pass, x2 - x1, 1);
                } else {
                  Graphics2D g2d = (Graphics2D) g;
                  Stroke oldStroke = null;
                  if (seq.transitionStroke != null) {
                    oldStroke = g2d.getStroke();
                    g2d.setStroke(seq.transitionStroke);
                  }
                  g.drawLine(x0 + x1, y0 - y1 - pass, x0 + x2, y0 - y2 - pass);
                  if (oldStroke != null) {
                    g2d.setStroke(oldStroke);
                  }
                }
              }
            }
            x1 = x2;
            v1 = v2;
          }
        }

        // Current value
        long v = seq.value(seq.size - 1);
        if (v >= vMin && v <= vMax) {
          if (bgIsLight) {
            g.setColor(seq.color);
          } else {
            g.setColor(fg);
          }
          x = r.x + r.width + 2;
          y = topMargin + h - (int) (h * (v - vMin) / (vMax - vMin));
          // a small triangle/arrow
          g.fillPolygon(new int[] {x + 2, x + 6, x + 6}, new int[] {y, y + 3, y - 3}, 3);
        }
        g.setColor(fg);
      }
    }

    int[] valueStringSlots = new int[nLists];
    for (int i = 0; i < nLists; i++) valueStringSlots[i] = -1;
    for (int i = 0; i < nLists; i++) {
      Sequence seq = seqs.get(i);
      if (seq.isPlotted && seq.size > 0) {
        // Draw current value

        // TODO: collapse values if pointsPerPixel >= 4

        long v = seq.value(seq.size - 1);
        if (v >= vMin && v <= vMax) {
          x = r.x + r.width + 2;
          y = topMargin + h - (int) (h * (v - vMin) / (vMax - vMin));
          int y2 = getValueStringSlot(valueStringSlots, y, 2 * 10, i);
          g.setFont(smallFont);
          if (bgIsLight) {
            g.setColor(seq.color);
          } else {
            g.setColor(fg);
          }
          String curValue = getFormattedValue(v, true);
          if (unit == Unit.PERCENT) {
            curValue += "%";
          }
          int valWidth = fm.stringWidth(curValue);
          String legend = (displayLegend ? seq.name : "");
          int legendWidth = fm.stringWidth(legend);
          if (checkRightMargin(valWidth) || checkRightMargin(legendWidth)) {
            // Wait for next repaint
            return;
          }
          g.drawString(legend, x + 17, Math.min(topMargin + h, y2 + 3 - 10));
          g.drawString(curValue, x + 17, Math.min(topMargin + h + 10, y2 + 3));

          // Maybe draw a short line to value
          if (y2 > y + 3) {
            g.drawLine(x + 9, y + 2, x + 14, y2);
          } else if (y2 < y - 3) {
            g.drawLine(x + 9, y - 2, x + 14, y2);
          }
        }
        g.setFont(oldFont);
        g.setColor(fg);
      }
    }
    g.setColor(oldColor);
  }