KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > PerformanceMonitor


1 /*
2  * @(#)PerformanceMonitor.java 1.39 06/08/25
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)PerformanceMonitor.java 1.39 06/08/25
39  */

40
41
42 package java2d;
43
44 import java.awt.*;
45 import java.awt.event.*;
46 import javax.swing.JPanel JavaDoc;
47 import javax.swing.border.EtchedBorder JavaDoc;
48 import javax.swing.border.TitledBorder JavaDoc;
49 import java.awt.font.TextLayout JavaDoc;
50 import java.awt.font.FontRenderContext JavaDoc;
51 import java.awt.image.BufferedImage JavaDoc;
52 import java.awt.geom.Rectangle2D JavaDoc;
53
54
55 /**
56  * Displays the time for a Surface to paint. Displays the number
57  * of frames per second on animated demos. Up to four surfaces fit
58  * in the display area.
59  */

60 public class PerformanceMonitor extends JPanel JavaDoc {
61
62     Surface surf;
63
64     public PerformanceMonitor() {
65         setLayout(new BorderLayout());
66         setBorder(new TitledBorder JavaDoc(new EtchedBorder JavaDoc(), "Performance"));
67         add(surf = new Surface());
68     }
69
70
71     public class Surface extends JPanel JavaDoc implements Runnable JavaDoc {
72     
73         public Thread JavaDoc thread;
74         private BufferedImage JavaDoc bimg;
75         private Font font = new Font("Times New Roman", Font.PLAIN, 12);
76         private JPanel JavaDoc panel;
77     
78     
79         public Surface() {
80             setBackground(Color.black);
81             addMouseListener(new MouseAdapter() {
82                 public void mouseClicked(MouseEvent e) {
83                     if (thread == null) start(); else stop();
84                 }
85             });
86         }
87     
88         public Dimension getMinimumSize() {
89             return getPreferredSize();
90         }
91     
92         public Dimension getMaximumSize() {
93             return getPreferredSize();
94         }
95     
96         public Dimension getPreferredSize() {
97             int textH = getFontMetrics(font).getHeight();
98             return new Dimension(135,2+textH*4);
99         }
100     
101     
102         public void paint(Graphics g) {
103             if (bimg != null) {
104                 g.drawImage(bimg, 0, 0, this);
105             }
106         }
107     
108     
109         public void start() {
110             thread = new Thread JavaDoc(this);
111             thread.setPriority(Thread.MIN_PRIORITY);
112             thread.setName("PerformanceMonitor");
113             thread.start();
114         }
115     
116     
117         public synchronized void stop() {
118             thread = null;
119             setSurfaceState();
120             notify();
121         }
122     
123     
124         public void setSurfaceState() {
125             if (panel != null) {
126                 for (Component comp : panel.getComponents()) {
127                     if (((DemoPanel) comp).surface != null) {
128                         ((DemoPanel) comp).surface.setMonitor(thread != null);
129                     }
130                 }
131             }
132         }
133     
134     
135         public void setPanel(JPanel JavaDoc panel) {
136             this.panel = panel;
137         }
138     
139     
140         public void run() {
141     
142             Thread JavaDoc me = Thread.currentThread();
143     
144             while (thread == me && !isShowing() || getSize().width == 0) {
145                 try {
146                     thread.sleep(500);
147                 } catch (InterruptedException JavaDoc e) { return; }
148             }
149     
150             Dimension d = new Dimension(0, 0);
151             Graphics2D big = null;
152             FontMetrics fm = null;
153             int ascent = 0;
154             int descent = 0;
155             
156             while (thread == me && isShowing()) {
157
158                 if (getWidth() != d.width || getHeight() != d.height) {
159                     d = getSize();
160                     bimg = (BufferedImage JavaDoc) createImage(d.width, d.height);
161                     big = bimg.createGraphics();
162                     big.setFont(font);
163                     fm = big.getFontMetrics();
164                     ascent = fm.getAscent();
165                     descent = fm.getDescent();
166                     setSurfaceState();
167                 }
168
169                 big.setBackground(getBackground());
170                 big.clearRect(0, 0, d.width, d.height);
171                 if (panel == null) {
172                     continue;
173                 }
174                 big.setColor(Color.green);
175                 int ssH = 1;
176                 for (Component comp : panel.getComponents()) {
177                     if (((DemoPanel) comp).surface != null) {
178                         String JavaDoc pStr = ((DemoPanel) comp).surface.perfStr;
179                         if (pStr != null) {
180                             ssH += ascent;
181                             big.drawString(pStr, 4, ssH+1);
182                             ssH += descent;
183                         }
184                     }
185                 }
186                 repaint();
187
188                 try {
189                     thread.sleep(999);
190                 } catch (InterruptedException JavaDoc e) { break; }
191             }
192             thread = null;
193         }
194     } // End Surface
195
} // End PeformanceMonitor
196
Popular Tags