/* * @(#)PerformanceMonitor.java 1.18 98/06/29 * * Copyright 1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ import java.awt.*; import com.sun.java.swing.JPanel; import com.sun.java.swing.border.EtchedBorder; import com.sun.java.swing.border.TitledBorder; import java.awt.font.TextLayout; import java.awt.font.FontRenderContext; import java.awt.image.BufferedImage; import java.awt.geom.Rectangle2D; /** * Displays the time for a J2DCanvas to paint. Displays the number * of frames per second on animated demos. Up to for J2DCanvases * are displayed. */ public class PerformanceMonitor extends JPanel { PerformanceMonitorCanvas pmc; public PerformanceMonitor() { setLayout(new BorderLayout()); setBorder(new TitledBorder(new EtchedBorder(), "Performance")); pmc = new PerformanceMonitorCanvas(); add(pmc); } public class PerformanceMonitorCanvas extends Canvas implements Runnable { public Thread thread; private BufferedImage offImg; private Font font = new Font("Times New Roman", Font.PLAIN, 12); private JPanel panel; public PerformanceMonitorCanvas() { setBackground(Color.black); start(); } public Dimension getMinimumSize() { return getPreferredSize(); } public Dimension getMaximumSize() { return getPreferredSize(); } public Dimension getPreferredSize() { TextLayout tl = new TextLayout("Nothing",font, new FontRenderContext(null, false, false)); int h = (int)(tl.getAscent()+tl.getDescent()); return new Dimension(140,4+h*4); } public void paint(Graphics g) { if (offImg != null) g.drawImage(offImg, 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; notify(); } public void setup(JPanel panel) { this.panel = panel; } public void run() { Thread me = Thread.currentThread(); while (thread == me && !isShowing() || getSize().width == 0) { try { thread.sleep(999); } catch (InterruptedException e) { thread = null; return; } } offImg = (BufferedImage) createImage(getSize().width, getSize().height); Graphics2D offG = offImg.createGraphics(); offG.setFont(font); FontMetrics fm = offG.getFontMetrics(); int ascent = fm.getAscent(); int descent = fm.getDescent(); Dimension d = getSize(); while (thread == me && isShowing()) { try { thread.sleep(999); } catch (InterruptedException e) { thread = null; return; } offG.setBackground(getBackground()); offG.clearRect(0, 0, d.width, d.height); offG.setColor(Color.green); if (panel == null) continue; Component cmps[] = panel.getComponents(); int ssH = 1; for (int i = 0; i < cmps.length; i++) { if (cmps[i] instanceof CanvasPanel) { CanvasPanel cp = (CanvasPanel) cmps[i]; if (cp.canvas != null && cp.canvas.perfStr != null) { ssH += ascent; offG.drawString(cp.canvas.perfStr, 4, ssH+1); ssH += descent; } } } if (isShowing()) { Graphics g = getGraphics(); if (g == null) continue; paint(g); g.dispose(); } } thread = null; } } // End PerformanceMonitorCanvas } // End PeformanceMonitor