FileDocCategorySizeDatePackage
AnimationComponent.javaAPI DocExample2845Mon Apr 05 12:08:22 BST 1999None

AnimationComponent

public abstract class AnimationComponent extends Container implements Runnable

Fields Summary
private boolean
mTrucking
private long[]
mPreviousTimes
private int
mPreviousIndex
private boolean
mPreviousFilled
private double
mFrameRate
private Image
mImage
private transient AnimationFrame
mRateListener
Constructors Summary
public AnimationComponent()

  
    
    mPreviousTimes = new long[128];
    mPreviousTimes[0] = System.currentTimeMillis();
    mPreviousIndex = 1;
    mPreviousFilled = false;
  
Methods Summary
protected voidcalculateFrameRate()

    // Measure the frame rate
    long now = System.currentTimeMillis();
    int numberOfFrames = mPreviousTimes.length;
    double newRate;
    // Use the more stable method if a history is available.
    if (mPreviousFilled)
      newRate = (double)numberOfFrames /
          (double)(now - mPreviousTimes[mPreviousIndex]) *
          1000.0;
    else
      newRate = 1000.0 /
          (double)(now - mPreviousTimes[numberOfFrames - 1]);
    firePropertyChange("frameRate", mFrameRate, newRate);
    mFrameRate = newRate;
    // Update the history.
    mPreviousTimes[mPreviousIndex] = now;
    mPreviousIndex++;
    if (mPreviousIndex >= numberOfFrames) {
      mPreviousIndex = 0;
      mPreviousFilled = true;
    }
  
protected booleancheckImage(java.awt.Dimension d)

    if (d.width == 0 || d.height == 0) return false;
    if (mImage == null || mImage.getWidth(null) != d.width
        || mImage.getHeight(null) != d.height) {
      mImage = createImage(d.width, d.height);
    }
    return true;
  
protected voidfirePropertyChange(java.lang.String name, double oldValue, double newValue)

    mRateListener.rateChanged(newValue);
  
public doublegetFrameRate()

 return mFrameRate; 
protected voidrender()

    Graphics g = getGraphics();
    if (g != null) {
      Dimension d = getSize();
      if (checkImage(d)) {
        Graphics imageGraphics = mImage.getGraphics();
        // Clear the image background.
        imageGraphics.setColor(getBackground());
        imageGraphics.fillRect(0, 0, d.width, d.height);
        imageGraphics.setColor(getForeground());
        // Draw this component offscreen.
        paint(imageGraphics);
        // Now put the offscreen image on the screen.
        g.drawImage(mImage, 0, 0, null);
        // Clean up.
        imageGraphics.dispose();
      }
      g.dispose();
    }
  
public voidrun()

    while (mTrucking) {
      render();
      timeStep();
      calculateFrameRate();
    }
  
public voidsetRateListener(AnimationFrame af)

    mRateListener = af;
  
public abstract voidtimeStep()