/**
   * Sets the index of the current animation frame to the specified value and requests that the
   * progress bar be repainted. Subclasses that don't use the default painting code might need to
   * override this method to change the way that the <code>repaint</code> method is invoked.
   *
   * @param newValue the new animation index; no checking is performed on its value
   * @see #incrementAnimationIndex
   * @since 1.4
   */
  protected void setAnimationIndex(int newValue) {
    if (animationIndex != newValue) {
      if (sizeChanged()) {
        animationIndex = newValue;
        maxPosition = 0; // needs to be recalculated
        delta = 0.0; // needs to be recalculated
        progressBar.repaint();
        return;
      }

      // Get the previous box drawn.
      nextPaintRect = getBox(nextPaintRect);

      // Update the frame number.
      animationIndex = newValue;

      // Get the next box to draw.
      if (nextPaintRect != null) {
        boxRect = getBox(boxRect);
        if (boxRect != null) {
          nextPaintRect.add(boxRect);
        }
      }
    } else { // animationIndex == newValue
      return;
    }

    if (nextPaintRect != null) {
      progressBar.repaint(nextPaintRect);
    } else {
      progressBar.repaint();
    }
  }
 public void drawingInvalidated(DrawingChangeEvent e) {
   Rectangle r = e.getInvalidatedRectangle();
   if (getDamage() == null) {
     setDamage(r);
   } else {
     // don't manipulate rectangle returned by getDamage() directly
     // because it could be a cloned rectangle.
     Rectangle damagedR = getDamage();
     damagedR.add(r);
     setDamage(damagedR);
   }
 }
  /** This method is taken from inside the source of JLabel (in the inner AccessibleJLabel class) */
  private Rectangle getTextRectangle() {
    final String text = getText();
    final Icon icon = (isEnabled()) ? getIcon() : getDisabledIcon();

    if ((icon == null) && (text == null)) {
      return null;
    }

    final Rectangle paintIconR = new Rectangle();
    final Rectangle paintTextR = new Rectangle();
    final Rectangle paintViewR = new Rectangle();

    Insets paintViewInsets = new Insets(0, 0, 0, 0);
    paintViewInsets = getInsets(paintViewInsets);

    paintViewR.x = paintViewInsets.left;
    paintViewR.y = paintViewInsets.top;
    paintViewR.width = getWidth() - (paintViewInsets.left + paintViewInsets.right);
    paintViewR.height = getHeight() - (paintViewInsets.top + paintViewInsets.bottom);

    final Graphics g = getGraphics();
    if (g == null) {
      return null;
    }

    SwingUtilities.layoutCompoundLabel(
        this,
        g.getFontMetrics(),
        text,
        icon,
        getVerticalAlignment(),
        getHorizontalAlignment(),
        getVerticalTextPosition(),
        getHorizontalTextPosition(),
        paintViewR,
        paintIconR,
        paintTextR,
        getIconTextGap());

    final Rectangle returnValue = new Rectangle(paintTextR);
    returnValue.add(paintIconR);

    return returnValue;
  }