home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / PerformanceMonitor.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  6.1 KB  |  189 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)PerformanceMonitor.java    1.32 02/06/13
  38.  */
  39.  
  40.  
  41. package java2d;
  42.  
  43. import java.awt.*;
  44. import java.awt.event.*;
  45. import javax.swing.JPanel;
  46. import javax.swing.border.EtchedBorder;
  47. import javax.swing.border.TitledBorder;
  48. import java.awt.font.TextLayout;
  49. import java.awt.font.FontRenderContext;
  50. import java.awt.image.BufferedImage;
  51. import java.awt.geom.Rectangle2D;
  52.  
  53.  
  54. /**
  55.  * Displays the time for a Surface to paint. Displays the number
  56.  * of frames per second on animated demos.  Up to four surfaces fit
  57.  * in the display area.
  58.  */
  59. public class PerformanceMonitor extends JPanel {
  60.  
  61.     Surface surf;
  62.  
  63.     public PerformanceMonitor() {
  64.         setLayout(new BorderLayout());
  65.         setBorder(new TitledBorder(new EtchedBorder(), "Performance"));
  66.         add(surf = new Surface());
  67.     }
  68.  
  69.  
  70.     public class Surface extends JPanel implements Runnable {
  71.     
  72.         public Thread thread;
  73.         private BufferedImage bimg;
  74.         private Font font = new Font("Times New Roman", Font.PLAIN, 12);
  75.         private JPanel panel;
  76.     
  77.     
  78.         public Surface() {
  79.             setBackground(Color.black);
  80.             addMouseListener(new MouseAdapter() {
  81.                 public void mouseClicked(MouseEvent e) {
  82.                     if (thread == null) start(); else stop();
  83.                 }
  84.             });
  85.         }
  86.     
  87.         public Dimension getMinimumSize() {
  88.             return getPreferredSize();
  89.         }
  90.     
  91.         public Dimension getMaximumSize() {
  92.             return getPreferredSize();
  93.         }
  94.     
  95.         public Dimension getPreferredSize() {
  96.             int textH = getFontMetrics(font).getHeight();
  97.             return new Dimension(135,2+textH*4);
  98.         }
  99.     
  100.     
  101.         public void paint(Graphics g) {
  102.             if (bimg != null) {
  103.                 g.drawImage(bimg, 0, 0, this);
  104.             }
  105.         }
  106.     
  107.     
  108.         public void start() {
  109.             thread = new Thread(this);
  110.             thread.setPriority(Thread.MIN_PRIORITY);
  111.             thread.setName("PerformanceMonitor");
  112.             thread.start();
  113.         }
  114.     
  115.     
  116.         public synchronized void stop() {
  117.             thread = null;
  118.             setSurfaceState();
  119.             notify();
  120.         }
  121.     
  122.     
  123.         public void setSurfaceState() {
  124.             if (panel == null) {
  125.                 return;
  126.             }
  127.             Component cmps[] = panel.getComponents();
  128.             for (int i = 0; i < cmps.length; i++) {
  129.                 if (((DemoPanel) cmps[i]).surface != null) {
  130.                     ((DemoPanel) cmps[i]).surface.setMonitor(thread != null);
  131.                 }
  132.             }
  133.         }
  134.     
  135.     
  136.         public void setPanel(JPanel panel) {
  137.             this.panel = panel;
  138.         }
  139.     
  140.     
  141.         public void run() {
  142.     
  143.             Thread me = Thread.currentThread();
  144.     
  145.             while (thread == me && !isShowing() || getSize().width == 0) {
  146.                 try {
  147.                     thread.sleep(500);
  148.                 } catch (InterruptedException e) { return; }
  149.             }
  150.     
  151.             Dimension d = getSize();
  152.             bimg = (BufferedImage) createImage(d.width, d.height);
  153.             Graphics2D big = bimg.createGraphics();
  154.             big.setFont(font);
  155.             FontMetrics fm = big.getFontMetrics();
  156.             int ascent = fm.getAscent();
  157.             int descent = fm.getDescent();
  158.             setSurfaceState();
  159.     
  160.             while (thread == me && isShowing()) {
  161.                 big.setBackground(getBackground());
  162.                 big.clearRect(0, 0, d.width, d.height);
  163.                 if (panel == null) {
  164.                     continue;
  165.                 }
  166.                 Component cmps[] = panel.getComponents();
  167.                 big.setColor(Color.green);
  168.                 int ssH = 1;
  169.                 for (int i = 0; i < cmps.length; i++) {
  170.                     if (((DemoPanel) cmps[i]).surface != null) {
  171.                         String pStr = ((DemoPanel) cmps[i]).surface.perfStr;
  172.                         if (pStr != null) {
  173.                             ssH += ascent;
  174.                             big.drawString(pStr, 4, ssH+1);
  175.                             ssH += descent;
  176.                         }
  177.                     }
  178.                 }
  179.                 repaint();
  180.  
  181.                 try {
  182.                     thread.sleep(999);
  183.                 } catch (InterruptedException e) { break; }
  184.             }
  185.             thread = null;
  186.         }
  187.     } // End Surface
  188. } // End PeformanceMonitor
  189.