001 package com.croftsoft.apps.sprite; 002 003 import java.awt.Graphics2D; 004 import javax.swing.JComponent; 005 006 import com.croftsoft.core.lang.NullArgumentException; 007 import com.croftsoft.core.animation.ComponentPainter; 008 import com.croftsoft.core.animation.ComponentUpdater; 009 import com.croftsoft.core.animation.sprite.TextSprite; 010 011 /********************************************************************* 012 * Monitors and updates the frame rate scrolling text. 013 * 014 * @version 015 * 2002-03-23 016 * @since 017 * 2002-02-21 018 * @author 019 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 020 *********************************************************************/ 021 022 public final class FrameRateSampler 023 implements ComponentPainter, ComponentUpdater 024 ////////////////////////////////////////////////////////////////////// 025 ////////////////////////////////////////////////////////////////////// 026 { 027 028 private final TextSprite textSprite; 029 030 private final long samplePeriod; 031 032 private final String prefix; 033 034 // 035 036 private long frameCount; 037 038 private long lastUpdateTime; 039 040 private long lastFrameRate; 041 042 ////////////////////////////////////////////////////////////////////// 043 ////////////////////////////////////////////////////////////////////// 044 045 public FrameRateSampler ( 046 TextSprite textSprite, 047 long samplePeriod, 048 String prefix ) 049 ////////////////////////////////////////////////////////////////////// 050 { 051 NullArgumentException.check ( this.textSprite = textSprite ); 052 053 NullArgumentException.check ( this.prefix = prefix ); 054 055 this.samplePeriod = samplePeriod; 056 057 lastFrameRate = -1L; 058 } 059 060 ////////////////////////////////////////////////////////////////////// 061 ////////////////////////////////////////////////////////////////////// 062 063 public void update ( JComponent component ) 064 ////////////////////////////////////////////////////////////////////// 065 { 066 increaseFrameCount ( ); 067 } 068 069 public void paint ( 070 JComponent component, 071 Graphics2D graphics ) 072 ////////////////////////////////////////////////////////////////////// 073 { 074 increaseFrameCount ( ); 075 } 076 077 ////////////////////////////////////////////////////////////////////// 078 ////////////////////////////////////////////////////////////////////// 079 080 private void increaseFrameCount ( ) 081 ////////////////////////////////////////////////////////////////////// 082 { 083 long updateTime = System.currentTimeMillis ( ); 084 085 frameCount++; 086 087 long timeDelta = updateTime - lastUpdateTime; 088 089 if ( timeDelta < samplePeriod ) 090 { 091 return; 092 } 093 094 long frameRate = Math.round ( frameCount * 1000.0 / timeDelta ); 095 096 frameCount = 0; 097 098 lastUpdateTime = updateTime; 099 100 if ( frameRate != lastFrameRate ) 101 { 102 textSprite.setText ( prefix + frameRate ); 103 104 lastFrameRate = frameRate; 105 } 106 } 107 108 ////////////////////////////////////////////////////////////////////// 109 ////////////////////////////////////////////////////////////////////// 110 }