📄 performancemonitor.java
字号:
package java2d;import java.awt.*;import java.awt.image.BufferedImage;import javax.swing.JPanel;import javax.swing.border.EtchedBorder;import javax.swing.border.TitledBorder;/** * Displays the time for a Surface to paint. Displays the number * of frames per second on animated demos. Up to four surfaces fit * in the display area. */public class PerformanceMonitor extends JPanel { Surface surf; public PerformanceMonitor() { setLayout(new BorderLayout()); setBorder(new TitledBorder(new EtchedBorder(), "实时数据")); add(surf = new Surface()); } public class Surface extends JPanel implements Runnable { public Thread thread; private BufferedImage bimg; //private Font font = new Font("Times New Roman", Font.PLAIN, 12); private Font font = new Font("serif", Font.PLAIN, 12); private JPanel panel; public Surface() { setBackground(Color.black); /* addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (thread == null) start(); else stop(); } }); */ } public Dimension getMinimumSize() { return getPreferredSize(); } public Dimension getMaximumSize() { return getPreferredSize(); } public Dimension getPreferredSize() { int textH = getFontMetrics(font).getHeight(); return new Dimension(135, 2 + textH * 8); } public void paint(Graphics g) { if (bimg != null) { g.drawImage(bimg, 0, 0, this); } } public void start() { thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.setName("PerformanceMonitor"); thread.start(); } public synchronized void stop() { thread = null; setSurfaceState(); notify(); } public void setSurfaceState() { if (panel == null) { return; } Component cmps[] = panel.getComponents(); for (int i = 0; i < cmps.length; i++) { if (((DemoPanel) cmps[i]).surface != null) { ((DemoPanel) cmps[i]).surface.setMonitor(thread != null); } } } public void setPanel(JPanel panel) { this.panel = panel; } public void run() { Thread me = Thread.currentThread(); while (thread == me && !isShowing() || getSize().width == 0) { try { thread.sleep(500); } catch (InterruptedException e) { return; } } Dimension d = getSize(); bimg = (BufferedImage) createImage(d.width, d.height); Graphics2D big = bimg.createGraphics(); big.setFont(font); FontMetrics fm = big.getFontMetrics(); int ascent = fm.getAscent(); int descent = fm.getDescent(); setSurfaceState(); while (thread == me && isShowing()) { big.setBackground(getBackground()); big.clearRect(0, 0, d.width, d.height); if (panel == null) { continue; } Component cmps[] = panel.getComponents(); big.setColor(Color.green); int ssH = 1; for (int i = 0; i < cmps.length; i++) { if (((DemoPanel) cmps[i]).surface != null) { String pStr = ((DemoPanel) cmps[i]).surface.perfStr; String pStrMa = ((DemoPanel) cmps[i]).surface.perfStrMa; if (pStr != null) { ssH += ascent; big.drawString(pStr, 4, ssH + 1); ssH += descent; } if (pStrMa != null) { ssH += ascent; big.drawString(pStrMa, 4, ssH + 1); ssH += descent; } } } repaint(); try { thread.sleep(500); } catch (InterruptedException e) { break; } } thread = null; } } // End Surface} // End PeformanceMonitor
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -