1 36 37 40 41 package java2d; 42 43 import static java.awt.Color .*; 44 import java.awt.*; 45 import javax.swing.*; 46 import java.awt.event.ActionListener ; 47 import java.awt.event.ActionEvent ; 48 import java.awt.event.MouseEvent ; 49 import javax.swing.border.*; 50 import java.util.Date ; 51 52 53 57 public class RunWindow extends JPanel implements Runnable , ActionListener { 58 59 static JButton runB; 60 static int delay = 10; 61 static int numRuns = 20; 62 static boolean exit; 63 static JCheckBox zoomCB = new JCheckBox("Zoom"); 64 static JCheckBox printCB = new JCheckBox("Print"); 65 static boolean buffersFlag; 66 static int bufBeg, bufEnd; 67 68 private JTextField delayTextField, runsTextField; 69 private Thread thread; 70 private JProgressBar pb; 71 72 73 74 public RunWindow() { 75 76 setLayout(new GridBagLayout()); 77 EmptyBorder eb = new EmptyBorder(5,5,5,5); 78 setBorder(new CompoundBorder(eb, new BevelBorder(BevelBorder.LOWERED))); 79 80 Font font = new Font("serif", Font.PLAIN, 10); 81 82 runB = new JButton("Run"); 83 runB.setBackground(GREEN); 84 runB.addActionListener(this); 85 runB.setMinimumSize(new Dimension(70,30)); 86 Java2Demo.addToGridBag(this, runB, 0, 0, 1, 1, 0.0, 0.0); 87 88 pb = new JProgressBar(); 89 pb.setPreferredSize(new Dimension(100,30)); 90 pb.setMinimum(0); 91 Java2Demo.addToGridBag(this, pb, 1, 0, 2, 1, 1.0, 0.0); 92 93 JPanel p1 = new JPanel(new GridLayout(2,2)); 94 JPanel p2 = new JPanel(); 95 JLabel l = new JLabel("Runs:"); 96 l.setFont(font); 97 l.setForeground(BLACK); 98 p2.add(l); 99 p2.add(runsTextField = new JTextField(String.valueOf(numRuns))); 100 runsTextField.setPreferredSize(new Dimension(30,20)); 101 runsTextField.addActionListener(this); 102 p1.add(p2); 103 p2 = new JPanel(); 104 l = new JLabel("Delay:"); 105 l.setFont(font); 106 l.setForeground(BLACK); 107 p2.add(l); 108 p2.add(delayTextField = new JTextField(String.valueOf(delay))); 109 delayTextField.setPreferredSize(new Dimension(30,20)); 110 delayTextField.addActionListener(this); 111 p1.add(p2); 112 113 zoomCB.setHorizontalAlignment(JButton.CENTER); 114 zoomCB.setFont(font); 115 printCB.setFont(font); 116 p1.add(zoomCB); 117 p1.add(printCB); 118 printCB.addActionListener(this); 119 Java2Demo.addToGridBag(this, p1, 0, 1, 3, 1, 1.0, 1.0); 120 } 121 122 123 public void actionPerformed(ActionEvent e) { 124 if (e.getSource().equals(printCB)) { 125 Java2Demo.printCB.setSelected(printCB.isSelected()); 126 } else if (e.getSource().equals(delayTextField)) { 127 delay = Integer.parseInt(delayTextField.getText().trim()); 128 } else if (e.getSource().equals(runsTextField)) { 129 numRuns = Integer.parseInt(runsTextField.getText().trim()); 130 } else if (e.getActionCommand() == "Run") { 131 doRunAction(); 132 } else if (e.getActionCommand() == "Stop") { 133 stop(); 134 } 135 } 136 137 138 public void doRunAction() { 139 runB.setText("Stop"); 140 runB.setBackground(RED); 141 start(); 142 } 143 144 145 public void start() { 146 thread = new Thread (this); 147 thread.setPriority(Thread.NORM_PRIORITY+1); 148 thread.setName("RunWindow"); 149 thread.start(); 150 } 151 152 153 public synchronized void stop() { 154 if (thread != null) { 155 thread.interrupt(); 156 } 157 thread = null; 158 notifyAll(); 159 } 160 161 162 public void sleepPerTab() { 163 for (int j = 0; j < delay+1 && thread != null; j++) { 164 for (int k = 0; k < 10 && thread != null; k++) { 165 try { 166 thread.sleep(100); 167 } catch (Exception e) { } 168 } 169 Runnable pbUpdateRunnable = new Runnable () { 170 public void run() { 171 pb.setValue(pb.getValue() + 1); 172 } 173 }; 174 SwingUtilities.invokeLater(pbUpdateRunnable); 175 } 176 } 177 178 179 private void printDemo(final DemoGroup dg) { 180 Runnable printDemoRunnable = new Runnable () { 181 public void run() { 182 if (!Java2Demo.controls.toolBarCB.isSelected()) { 183 Java2Demo.controls.toolBarCB.setSelected(true); 184 dg.invalidate(); 185 } 186 for (Component comp : dg.getPanel().getComponents()) { 187 DemoPanel dp = (DemoPanel) comp; 188 if (dp.tools != null) { 189 if (dp.surface.animating != null) { 190 if (dp.surface.animating.thread != null) { 191 dp.tools.startStopB.doClick(); 192 } 193 } 194 dp.tools.printB.doClick(); 195 } 196 } 197 } 198 }; 199 invokeAndWait(printDemoRunnable); 200 } 201 202 private DemoGroup dg = null; 203 private DemoPanel dp = null; 204 205 public void run() { 206 207 System.out.println("\nJava2D Demo RunWindow : " + 208 numRuns + " Runs, " + 209 delay + " second delay between tabs\n" + 210 "java version: " + System.getProperty("java.version") + 211 "\n" + System.getProperty("os.name") + " " + 212 System.getProperty("os.version") + "\n"); 213 Runtime r = Runtime.getRuntime(); 214 215 for (int runNum = 0; runNum < numRuns && thread != null; runNum++) { 216 217 Date d = new Date (); 218 System.out.print("#" + runNum + " " + d.toString() + ", "); 219 r.gc(); 220 float freeMemory = (float) r.freeMemory(); 221 float totalMemory = (float) r.totalMemory(); 222 System.out.println(((totalMemory - freeMemory)/1024) + "K used"); 223 224 for (int i = 0; i < Java2Demo.tabbedPane.getTabCount() && thread != null; i++) { 225 226 final int mainTabIndex = i; 227 Runnable initDemoRunnable = new Runnable () { 228 public void run() { 229 pb.setValue(0); 230 pb.setMaximum(delay); 231 if (mainTabIndex != 0) { 232 dg = Java2Demo.group[mainTabIndex-1]; 233 dg.invalidate(); 234 } 235 Java2Demo.tabbedPane.setSelectedIndex(mainTabIndex); 236 } 237 }; 238 invokeAndWait(initDemoRunnable); 239 240 if (i != 0 && (zoomCB.isSelected() || buffersFlag)) { 241 dp = (DemoPanel) dg.getPanel().getComponent(0); 242 if (dg.tabbedPane == null && dp.surface != null) { 243 Runnable mouseClickedRunnable = new Runnable () { 244 public void run() { 245 dg.mouseClicked(new MouseEvent (dp.surface, MouseEvent.MOUSE_CLICKED, 0, 0, 10, 10, 1, false)); 246 } 247 }; 248 invokeAndWait(mouseClickedRunnable); 249 } 250 for (int j = 1; j < dg.tabbedPane.getTabCount() && thread != null; j++) { 251 252 final int subTabIndex = j; 253 254 Runnable initPanelRunnable = new Runnable () { 255 public void run() { 256 pb.setValue(0); 257 pb.setMaximum(delay); 258 dg.tabbedPane.setSelectedIndex(subTabIndex); 259 } 260 }; 261 invokeAndWait(initPanelRunnable); 262 263 final JPanel p = dg.getPanel(); 264 if (buffersFlag && p.getComponentCount() == 1) { 265 dp = (DemoPanel) p.getComponent(0); 266 if (dp.surface.animating != null) { 267 dp.surface.animating.stop(); 268 } 269 for (int k = bufBeg; k <= bufEnd && thread != null; k++) { 270 271 final int cloneIndex = k; 272 Runnable cloneRunnable = new Runnable () { 273 public void run(){ 274 dp.tools.cloneB.doClick(); 275 int n = p.getComponentCount(); 276 DemoPanel clone = (DemoPanel)p.getComponent(n-1); 277 if (clone.surface.animating != null) { 278 clone.surface.animating.stop(); 279 } 280 clone.tools.issueRepaint = true; 281 clone.tools.screenCombo.setSelectedIndex(cloneIndex); 282 clone.tools.issueRepaint = false; 283 } 284 }; 285 invokeAndWait(cloneRunnable); 286 } 287 } 288 if (printCB.isSelected()) { 289 printDemo(dg); 290 } 291 sleepPerTab(); 292 } 293 } else if (i != 0 && printCB.isSelected()) { 294 printDemo(dg); 295 sleepPerTab(); 296 } else { 297 sleepPerTab(); 298 } 299 } 300 if (runNum+1 == numRuns) { 301 System.out.println("Finished."); 302 if (exit && thread != null) { 303 System.out.println("System.exit(0)."); 304 System.exit(0); 305 } 306 } 307 } 308 Runnable resetRunnable = new Runnable () { 309 public void run() { 310 runB.setText("Run"); 311 runB.setBackground(GREEN); 312 pb.setValue(0); 313 } 314 }; 315 invokeAndWait(resetRunnable); 316 317 thread = null; 318 dg = null; 319 dp = null; 320 } 321 private static void invokeAndWait(Runnable run) { 322 try { 323 SwingUtilities.invokeAndWait(run); 324 }catch(Exception e){ 325 System.err.println("ERROR invokeAndWait : " + e); 326 e.printStackTrace(); 327 } 328 } 329 } 330 | Popular Tags |