コード例 #1
0
  /**
   * All purpose paint method that should do the right thing for all linear bouncing-box progress
   * bars. Override this if you are making another kind of progress bar.
   *
   * @see #paintDeterminate
   * @since 1.4
   */
  protected void paintIndeterminate(Graphics g, JComponent c) {
    if (!(g instanceof Graphics2D)) {
      return;
    }

    Insets b = progressBar.getInsets(); // area for border
    int barRectWidth = progressBar.getWidth() - (b.right + b.left);
    int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);

    if (barRectWidth <= 0 || barRectHeight <= 0) {
      return;
    }

    Graphics2D g2 = (Graphics2D) g;

    // Paint the bouncing box.
    boxRect = getBox(boxRect);
    if (boxRect != null) {
      g2.setColor(progressBar.getForeground());
      g2.fillRect(boxRect.x, boxRect.y, boxRect.width, boxRect.height);
    }

    // Deal with possible text painting
    if (progressBar.isStringPainted()) {
      if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
        paintString(g2, b.left, b.top, barRectWidth, barRectHeight, boxRect.x, boxRect.width, b);
      } else {
        paintString(g2, b.left, b.top, barRectWidth, barRectHeight, boxRect.y, boxRect.height, b);
      }
    }
  }
コード例 #2
0
  /**
   * All purpose paint method that should do the right thing for almost all linear, determinate
   * progress bars. By setting a few values in the defaults table, things should work just fine to
   * paint your progress bar. Naturally, override this if you are making a circular or semi-circular
   * progress bar.
   *
   * @see #paintIndeterminate
   * @since 1.4
   */
  protected void paintDeterminate(Graphics g, JComponent c) {
    if (!(g instanceof Graphics2D)) {
      return;
    }

    Insets b = progressBar.getInsets(); // area for border
    int barRectWidth = progressBar.getWidth() - (b.right + b.left);
    int barRectHeight = progressBar.getHeight() - (b.top + b.bottom);

    if (barRectWidth <= 0 || barRectHeight <= 0) {
      return;
    }

    int cellLength = getCellLength();
    int cellSpacing = getCellSpacing();
    // amount of progress to draw
    int amountFull = getAmountFull(b, barRectWidth, barRectHeight);

    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(progressBar.getForeground());

    if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
      // draw the cells
      if (cellSpacing == 0 && amountFull > 0) {
        // draw one big Rect because there is no space between cells
        g2.setStroke(
            new BasicStroke((float) barRectHeight, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
      } else {
        // draw each individual cell
        g2.setStroke(
            new BasicStroke(
                (float) barRectHeight,
                BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_BEVEL,
                0.f,
                new float[] {cellLength, cellSpacing},
                0.f));
      }

      if (BasicGraphicsUtils.isLeftToRight(c)) {
        g2.drawLine(
            b.left, (barRectHeight / 2) + b.top, amountFull + b.left, (barRectHeight / 2) + b.top);
      } else {
        g2.drawLine(
            (barRectWidth + b.left),
            (barRectHeight / 2) + b.top,
            barRectWidth + b.left - amountFull,
            (barRectHeight / 2) + b.top);
      }

    } else { // VERTICAL
      // draw the cells
      if (cellSpacing == 0 && amountFull > 0) {
        // draw one big Rect because there is no space between cells
        g2.setStroke(
            new BasicStroke((float) barRectWidth, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
      } else {
        // draw each individual cell
        g2.setStroke(
            new BasicStroke(
                (float) barRectWidth,
                BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_BEVEL,
                0f,
                new float[] {cellLength, cellSpacing},
                0f));
      }

      g2.drawLine(
          barRectWidth / 2 + b.left,
          b.top + barRectHeight,
          barRectWidth / 2 + b.left,
          b.top + barRectHeight - amountFull);
    }

    // Deal with possible text painting
    if (progressBar.isStringPainted()) {
      paintString(g, b.left, b.top, barRectWidth, barRectHeight, amountFull, b);
    }
  }