View Javadoc

1   /* OpenLogViewer
2    *
3    * Copyright 2011
4    *
5    * This file is part of the OpenLogViewer project.
6    *
7    * OpenLogViewer software is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * OpenLogViewer software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with any OpenLogViewer software.  If not, see http://www.gnu.org/licenses/
19   *
20   * I ask that if you make any changes to this file you fork the code on github.com!
21   *
22   */
23  package org.diyefi.openlogviewer;
24  
25  import java.awt.Dimension;
26  import java.awt.FlowLayout;
27  import java.awt.event.ActionEvent;
28  import java.awt.event.ActionListener;
29  import java.text.DecimalFormat;
30  import java.text.DecimalFormatSymbols;
31  import java.text.NumberFormat;
32  import java.util.ResourceBundle;
33  
34  import javax.swing.JLabel;
35  import javax.swing.JPanel;
36  import javax.swing.Timer;
37  
38  public class FramesPerSecondPanel extends JPanel implements ActionListener {
39  	private static final long serialVersionUID = 1L;
40  	private static final int PREFERRED_WIDTH = 80;
41  	private static final int PPREFERRED_HEIGHT = 16;
42  	private static final int TIMER_RATE = 250; // milliseconds - changes display/update speed
43  	private static final double MILLISECONDS_PER_SECOND = 1000d;
44  
45  	private static final char ZERO = '0';
46  	private static final char DS = DecimalFormatSymbols.getInstance().getDecimalSeparator();
47  	private static final char TS = DecimalFormatSymbols.getInstance().getGroupingSeparator();
48  	private static final String FORMAT = "#" + TS + "##0" + DS + ZERO;
49  
50  	private static final DecimalFormat DF = (DecimalFormat) NumberFormat.getNumberInstance();
51  	static {
52  		DF.applyLocalizedPattern(FORMAT);
53  	}
54  
55  	private static long frameCount;
56  	private static long thePast;
57  	private static long currentTime;
58  	private static long previousCount;
59  
60  	private final String framesPerSecond;
61  
62  	private final JLabel output;
63  	private final Timer sampleTimer;
64  
65  	private Double fps;
66  
67  	public FramesPerSecondPanel(final ResourceBundle labels) {
68  		setName("fpsPanel");
69  		setPreferredSize(new Dimension(PREFERRED_WIDTH, PPREFERRED_HEIGHT));
70  		setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
71  
72  		framesPerSecond = labels.getString(Text.FRAMES_PER_SECOND);
73  		final String defaultFpsLabel = framesPerSecond + ZERO + DS + ZERO;
74  		output = new JLabel(defaultFpsLabel);
75  		output.setVerticalTextPosition(JLabel.CENTER);
76  		output.setHorizontalTextPosition(JLabel.CENTER);
77  		this.add(output);
78  
79  		// Start timer after objects that it operates on exist...
80  		sampleTimer = new Timer(TIMER_RATE, this);
81  		sampleTimer.setInitialDelay(0);
82  		sampleTimer.start();
83  		currentTime = System.currentTimeMillis();
84  		thePast = currentTime;
85  	}
86  
87  	public static void increaseFrameCount() {
88  		currentTime = System.currentTimeMillis();
89  		frameCount++;
90  	}
91  
92  	@Override
93  	public final void actionPerformed(final ActionEvent e) {
94  		if (e.getSource().equals(sampleTimer)) {
95  			final long sampleWindow = currentTime - thePast;
96  			final long sampleCount = frameCount - previousCount;
97  			if (sampleWindow == 0L) { // Avoid division by zero
98  				fps = 0d;
99  			} else {
100 				fps = ((double) sampleCount / (double) sampleWindow) * MILLISECONDS_PER_SECOND;
101 			}
102 			output.setText(framesPerSecond + DF.format(fps));
103 			previousCount = frameCount;
104 			thePast = currentTime;
105 		}
106 	}
107 
108 }