1 36 37 40 41 42 package java2d; 43 44 import java.awt.*; 45 import java.awt.event.*; 46 import javax.swing.JPanel ; 47 import javax.swing.border.EtchedBorder ; 48 import javax.swing.border.TitledBorder ; 49 import java.awt.font.TextLayout ; 50 import java.awt.font.FontRenderContext ; 51 import java.awt.image.BufferedImage ; 52 import java.awt.geom.Rectangle2D ; 53 54 55 60 public class PerformanceMonitor extends JPanel { 61 62 Surface surf; 63 64 public PerformanceMonitor() { 65 setLayout(new BorderLayout()); 66 setBorder(new TitledBorder (new EtchedBorder (), "Performance")); 67 add(surf = new Surface()); 68 } 69 70 71 public class Surface extends JPanel implements Runnable { 72 73 public Thread thread; 74 private BufferedImage bimg; 75 private Font font = new Font("Times New Roman", Font.PLAIN, 12); 76 private JPanel 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 (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 panel) { 136 this.panel = panel; 137 } 138 139 140 public void run() { 141 142 Thread me = Thread.currentThread(); 143 144 while (thread == me && !isShowing() || getSize().width == 0) { 145 try { 146 thread.sleep(500); 147 } catch (InterruptedException 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 ) 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 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 e) { break; } 191 } 192 thread = null; 193 } 194 } } | Popular Tags |