Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/PerformanceMonitor.java
41154 views
1
/*
2
*
3
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
*
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
*
12
* - Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in the
14
* documentation and/or other materials provided with the distribution.
15
*
16
* - Neither the name of Oracle nor the names of its
17
* contributors may be used to endorse or promote products derived
18
* from this software without specific prior written permission.
19
*
20
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
*/
32
package java2d;
33
34
35
import java.awt.BorderLayout;
36
import java.awt.Color;
37
import java.awt.Component;
38
import java.awt.Dimension;
39
import java.awt.Font;
40
import java.awt.FontMetrics;
41
import java.awt.Graphics;
42
import java.awt.Graphics2D;
43
import java.awt.event.MouseAdapter;
44
import java.awt.event.MouseEvent;
45
import java.awt.image.BufferedImage;
46
import javax.swing.JPanel;
47
import javax.swing.border.EtchedBorder;
48
import javax.swing.border.TitledBorder;
49
50
51
/**
52
* Displays the time for a Surface to paint. Displays the number
53
* of frames per second on animated demos. Up to four surfaces fit
54
* in the display area.
55
*/
56
@SuppressWarnings("serial")
57
public class PerformanceMonitor extends JPanel {
58
59
Surface surf;
60
61
public PerformanceMonitor() {
62
setLayout(new BorderLayout());
63
setBorder(new TitledBorder(new EtchedBorder(), "Performance"));
64
add(surf = new Surface());
65
}
66
67
68
public class Surface extends JPanel implements Runnable {
69
70
public Thread thread;
71
private BufferedImage bimg;
72
private Font font = new Font(Font.SERIF, Font.PLAIN, 12);
73
private JPanel panel;
74
75
public Surface() {
76
setBackground(Color.black);
77
addMouseListener(new MouseAdapter() {
78
79
@Override
80
public void mouseClicked(MouseEvent e) {
81
if (thread == null) {
82
start();
83
} else {
84
stop();
85
}
86
}
87
});
88
}
89
90
@Override
91
public Dimension getMinimumSize() {
92
return getPreferredSize();
93
}
94
95
@Override
96
public Dimension getMaximumSize() {
97
return getPreferredSize();
98
}
99
100
@Override
101
public Dimension getPreferredSize() {
102
int textH = getFontMetrics(font).getHeight();
103
return new Dimension(135, 2 + textH * 4);
104
}
105
106
@Override
107
public void paint(Graphics g) {
108
if (bimg != null) {
109
g.drawImage(bimg, 0, 0, this);
110
}
111
}
112
113
public void start() {
114
thread = new Thread(this);
115
thread.setPriority(Thread.MIN_PRIORITY);
116
thread.setName("PerformanceMonitor");
117
thread.start();
118
}
119
120
public synchronized void stop() {
121
thread = null;
122
setSurfaceState();
123
notify();
124
}
125
126
public void setSurfaceState() {
127
if (panel != null) {
128
for (Component comp : panel.getComponents()) {
129
if (((DemoPanel) comp).surface != null) {
130
((DemoPanel) comp).surface.setMonitor(thread != null);
131
}
132
}
133
}
134
}
135
136
public void setPanel(JPanel panel) {
137
this.panel = panel;
138
}
139
140
@Override
141
@SuppressWarnings("SleepWhileHoldingLock")
142
public void run() {
143
144
Thread me = Thread.currentThread();
145
146
while (thread == me && !isShowing() || getSize().width == 0) {
147
try {
148
Thread.sleep(500);
149
} catch (InterruptedException e) {
150
return;
151
}
152
}
153
154
Dimension d = new Dimension(0, 0);
155
Graphics2D big = null;
156
FontMetrics fm = null;
157
int ascent = 0;
158
int descent = 0;
159
160
while (thread == me && isShowing()) {
161
162
if (getWidth() != d.width || getHeight() != d.height) {
163
d = getSize();
164
bimg = (BufferedImage) createImage(d.width, d.height);
165
big = bimg.createGraphics();
166
big.setFont(font);
167
fm = big.getFontMetrics();
168
ascent = fm.getAscent();
169
descent = fm.getDescent();
170
setSurfaceState();
171
}
172
173
big.setBackground(getBackground());
174
big.clearRect(0, 0, d.width, d.height);
175
if (panel == null) {
176
continue;
177
}
178
big.setColor(Color.green);
179
int ssH = 1;
180
for (Component comp : panel.getComponents()) {
181
if (((DemoPanel) comp).surface != null) {
182
String pStr = ((DemoPanel) comp).surface.perfStr;
183
if (pStr != null) {
184
ssH += ascent;
185
big.drawString(pStr, 4, ssH + 1);
186
ssH += descent;
187
}
188
}
189
}
190
repaint();
191
192
try {
193
Thread.sleep(999);
194
} catch (InterruptedException e) {
195
break;
196
}
197
}
198
thread = null;
199
}
200
} // End Surface
201
} // End PeformanceMonitor
202
203
204