KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > RunWindow


1 /*
2  * @(#)RunWindow.java 1.34 06/08/29
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  * @(#)RunWindow.java 1.34 06/08/29
39  */

40
41 package java2d;
42
43 import static java.awt.Color JavaDoc.*;
44 import java.awt.*;
45 import javax.swing.*;
46 import java.awt.event.ActionListener JavaDoc;
47 import java.awt.event.ActionEvent JavaDoc;
48 import java.awt.event.MouseEvent JavaDoc;
49 import javax.swing.border.*;
50 import java.util.Date JavaDoc;
51
52
53 /**
54  * A separate window for running the Java2Demo. Go from tab to tab or demo to
55  * demo.
56  */

57 public class RunWindow extends JPanel implements Runnable JavaDoc, ActionListener JavaDoc {
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 JavaDoc 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 JavaDoc 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 JavaDoc(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 JavaDoc e) { }
168             }
169             Runnable JavaDoc pbUpdateRunnable = new Runnable JavaDoc() {
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 JavaDoc printDemoRunnable = new Runnable JavaDoc() {
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 JavaDoc r = Runtime.getRuntime();
214
215         for (int runNum = 0; runNum < numRuns && thread != null; runNum++) {
216
217             Date JavaDoc d = new Date JavaDoc();
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 JavaDoc initDemoRunnable = new Runnable JavaDoc() {
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 JavaDoc mouseClickedRunnable = new Runnable JavaDoc() {
244                             public void run() {
245                                 dg.mouseClicked(new MouseEvent JavaDoc(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 JavaDoc initPanelRunnable = new Runnable JavaDoc() {
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 JavaDoc cloneRunnable = new Runnable JavaDoc() {
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 JavaDoc resetRunnable = new Runnable JavaDoc() {
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 JavaDoc run) {
322         try {
323             SwingUtilities.invokeAndWait(run);
324         }catch(Exception JavaDoc e){
325             System.err.println("ERROR invokeAndWait : " + e);
326             e.printStackTrace();
327         }
328     }
329 }
330
Popular Tags