Java 2D Graphics // Measure the frame rate
Java 2D Graphics 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(); } } // Offscreen image. protected boolean checkImage(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 void calculateFrameRate() { // Measure the frame ratelong 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; } } public double getFrameRate() { return mFrameRate; } // Property change support. private transient AnimationFrame mRateListener; public void setRateListener(AnimationFrame af) { mRateListener = af; } protected void firePropertyChange(String name, double oldValue, double newValue) { mRateListener.rateChanged(newValue); page 257
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services