Java 2D Graphics // Measure the frame rate
Java 2D Graphics // 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; } } Sharp-eyed readers will have noticed that calculateFrameRate() fires off a property event when the rate changes. What’s that all about? Animation-Component allows other objects that are interested in the frame rate to receive notification when it changes. This is done through the use of the java.beans.PropertyChangeSupport class. Without further ado, here is the entire AnimationComponent class: import java.awt.*; public abstract class AnimationComponentextends Container implements Runnable { private boolean mTrucking = true; private long[] mPreviousTimes; // milliseconds private int mPreviousIndex; private boolean mPreviousFilled; private double mFrameRate; // frames per second private Image mImage; public AnimationComponent() { mPreviousTimes = new long[128]; mPreviousTimes[0] = System.currentTimeMillis(); mPreviousIndex = 1; mPreviousFilled = false; } public abstract void timeStep(); public void run() { while (mTrucking) { render(); timeStep(); calculateFrameRate(); } } protected void render() { Graphics g = getGraphics(); if (g != null) { page 256
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services