home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / Java2D / PerformanceMonitor.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  4.5 KB  |  166 lines

  1. /*
  2.  * @(#)PerformanceMonitor.java    1.24 98/09/13
  3.  *
  4.  * Copyright 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15.  
  16. import java.awt.*;
  17. import java.awt.event.*;
  18. import javax.swing.JPanel;
  19. import javax.swing.border.EtchedBorder;
  20. import javax.swing.border.TitledBorder;
  21. import java.awt.font.TextLayout;
  22. import java.awt.font.FontRenderContext;
  23. import java.awt.image.BufferedImage;
  24. import java.awt.geom.Rectangle2D;
  25.  
  26.  
  27. /**
  28.  * Displays the time for a DemoSurface to paint. Displays the number
  29.  * of frames per second on animated demos.  Up to four surfaces fit
  30.  * in the display area.
  31.  */
  32. public class PerformanceMonitor extends JPanel {
  33.  
  34.     MonitorComponent mc;
  35.  
  36.     public PerformanceMonitor() {
  37.         setLayout(new BorderLayout());
  38.         setBorder(new TitledBorder(new EtchedBorder(), "Performance"));
  39.         mc = new MonitorComponent();
  40.         add(mc);
  41.     }
  42.  
  43. public class MonitorComponent extends JPanel implements Runnable {
  44.  
  45.     public Thread thread;
  46.     private BufferedImage bimg;
  47.     private Font font = new Font("Times New Roman", Font.PLAIN, 12);
  48.     private JPanel panel;
  49.  
  50.  
  51.     public MonitorComponent() {
  52.         setBackground(Color.black);
  53.         addMouseListener(new MouseAdapter() {
  54.             public void mouseClicked(MouseEvent e) {
  55.                 if (thread == null) start(); else stop();
  56.             }
  57.         });
  58.         start();
  59.     }
  60.  
  61.     public Dimension getMinimumSize() {
  62.         return getPreferredSize();
  63.     }
  64.  
  65.     public Dimension getMaximumSize() {
  66.         return getPreferredSize();
  67.     }
  68.  
  69.  
  70.     public Dimension getPreferredSize() {
  71.         TextLayout tl = new TextLayout("Nothing",font,
  72.             new FontRenderContext(null, false, false));
  73.         int h = (int)(tl.getAscent()+tl.getDescent());
  74.         return new Dimension(140,4+h*4);
  75.     }
  76.  
  77.  
  78.     public void paint(Graphics g) {
  79.         if (bimg != null) {
  80.             g.drawImage(bimg, 0, 0, this);
  81.         }
  82.     }
  83.  
  84.  
  85.     public void start() {
  86.         thread = new Thread(this);
  87.         thread.setPriority(Thread.MIN_PRIORITY);
  88.         thread.setName("PerformanceMonitor");
  89.         thread.start();
  90.     }
  91.  
  92.  
  93.     public synchronized void stop() {
  94.         thread = null;
  95.         setSurfaceState();
  96.         notify();
  97.     }
  98.  
  99.  
  100.     public void setSurfaceState() {
  101.         if (panel == null) {
  102.             return;
  103.         }
  104.         Component cmps[] = panel.getComponents();
  105.         for (int i = 0; i < cmps.length; i++) {
  106.             if (((DemoPanel) cmps[i]).surface != null) {
  107.                 ((DemoPanel) cmps[i]).surface.setMonitor(thread != null);
  108.             }
  109.         }
  110.     }
  111.  
  112.  
  113.     public void setPanel(JPanel panel) {
  114.         this.panel = panel;
  115.     }
  116.  
  117.  
  118.     public void run() {
  119.  
  120.         Thread me = Thread.currentThread();
  121.  
  122.         while (thread == me && !isShowing() || getSize().width == 0) {
  123.             try {
  124.                 thread.sleep(500);
  125.             } catch (InterruptedException e) { thread = null; return; }
  126.         }
  127.  
  128.  
  129.         bimg = (BufferedImage) createImage(getSize().width, getSize().height);
  130.         Graphics2D big = bimg.createGraphics();
  131.         big.setFont(font);
  132.         FontMetrics fm = big.getFontMetrics();
  133.         int ascent = fm.getAscent();
  134.         int descent = fm.getDescent();
  135.         Dimension d = getSize();
  136.         setSurfaceState();
  137.  
  138.         while (thread == me && isShowing()) {
  139.  
  140.             try {
  141.                 thread.sleep(999);
  142.             } catch (InterruptedException e) { thread = null; return; }
  143.  
  144.             big.setBackground(getBackground());
  145.             big.clearRect(0, 0, d.width, d.height);
  146.             big.setColor(Color.green);
  147.             if (panel == null) {
  148.                 continue;
  149.             }
  150.             Component cmps[] = panel.getComponents();
  151.             int ssH = 1;
  152.             for (int i = 0; i < cmps.length; i++) {
  153.                 DemoSurface surface = ((DemoPanel) cmps[i]).surface;
  154.                 if (surface != null && surface.perfStr != null) {
  155.                     ssH += ascent;
  156.                     big.drawString(surface.perfStr, 4, ssH+1);
  157.                     ssH += descent;
  158.                 }
  159.             }
  160.             repaint();
  161.         }
  162.         thread = null;
  163.     }
  164. } // End MonitorComponent
  165. } // End PeformanceMonitor
  166.