, . 2011 , OP, , . , , , ( 2 ).
, Java GUI:
public class FrameRateCounter {
private Map<Long, Long> measurementTimes;
public FrameRateCounter() {
measurementTimes = new HashMap<Long, Long>();
}
public synchronized void submitReading() {
long measurementTime = System.nanoTime();
long invalidationTime = System.nanoTime() + 1000000000;
measurementTimes.put(measurementTime, invalidationTime);
}
public int getFrameRate() {
performInvalidations();
return measurementTimes.size();
}
private synchronized void performInvalidations() {
long currentTime = System.nanoTime();
Iterator<Long> i = measurementTimes.keySet().iterator();
while(i.hasNext()) {
long measurementTime = i.next();
long invalidationTime = measurementTimes.get(measurementTime);
if(invalidationTime < currentTime) {
i.remove();
}
}
}
}
( ):
- - , FPS, " " . , /.
- GUI - , . - ,
Graphics JLabel , . - , - - - ( O (1)). , .
javax.Swing.Timer - submitReading() /, getFrameRate() /, (, JLabel, ..).
(PS .: Note that with Swing, all drawing operations must be populated in the Event Dispatcher (EDT) stream. Therefore, no drawing work should be delegated to other (unprocessed) streams without using special commands such as SwingUtilities.invokeLater(..)).
Hope this helps!
source
share