KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > CloningFeature


1 /*
2  * @(#)CloningFeature.java 1.23 05/11/17
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  * @(#)CloningFeature.java 1.23 05/11/17
39  */

40
41
42 package java2d;
43
44 import java.awt.Component JavaDoc;
45 import java.awt.Color JavaDoc;
46 import java.awt.Font JavaDoc;
47 import java.awt.BorderLayout JavaDoc;
48 import java.awt.Dimension JavaDoc;
49 import java.awt.event.MouseEvent JavaDoc;
50 import javax.swing.JPanel JavaDoc;
51 import javax.swing.JScrollPane JavaDoc;
52 import javax.swing.JTextArea JavaDoc;
53 import javax.swing.border.EmptyBorder JavaDoc;
54 import javax.swing.border.SoftBevelBorder JavaDoc;
55 import javax.swing.border.CompoundBorder JavaDoc;
56
57 /**
58  * Illustration of how to use the clone feature of the demo.
59  */

60 public class CloningFeature extends JPanel JavaDoc implements Runnable JavaDoc {
61
62     private Thread JavaDoc thread;
63     private JTextArea JavaDoc ta;
64
65
66     public CloningFeature() {
67
68         setLayout(new BorderLayout JavaDoc());
69         EmptyBorder JavaDoc eb = new EmptyBorder JavaDoc(5,5,5,5);
70         SoftBevelBorder JavaDoc sbb = new SoftBevelBorder JavaDoc(SoftBevelBorder.RAISED);
71         setBorder(new CompoundBorder JavaDoc(eb, sbb));
72
73         ta = new JTextArea JavaDoc("Cloning Demonstrated\n\nClicking once on a demo\n");
74         ta.setMinimumSize(new Dimension JavaDoc(300,500));
75         JScrollPane JavaDoc scroller = new JScrollPane JavaDoc();
76         scroller.getViewport().add(ta);
77         ta.setFont(new Font JavaDoc("Dialog", Font.PLAIN, 14));
78         ta.setForeground(Color.black);
79         ta.setBackground(Color.lightGray);
80         ta.setEditable(false);
81
82         add("Center", scroller);
83
84         start();
85     }
86
87     public void start() {
88         thread = new Thread JavaDoc(this);
89         thread.setPriority(Thread.MAX_PRIORITY);
90         thread.setName("CloningFeature");
91         thread.start();
92     }
93
94     public void stop() {
95         if (thread != null) {
96             thread.interrupt();
97         }
98         thread = null;
99     }
100
101
102     public void run() {
103
104
105         int index = Java2Demo.tabbedPane.getSelectedIndex();
106         if (index == 0) {
107            Java2Demo.tabbedPane.setSelectedIndex(1);
108            try { thread.sleep(3333); } catch (Exception JavaDoc e) { return; }
109         }
110
111         if (!Java2Demo.controls.toolBarCB.isSelected()) {
112             Java2Demo.controls.toolBarCB.setSelected(true);
113             try { thread.sleep(2222); } catch (Exception JavaDoc e) { return; }
114         }
115
116         index = Java2Demo.tabbedPane.getSelectedIndex()-1;
117         DemoGroup dg = Java2Demo.group[index];
118         DemoPanel dp = (DemoPanel) dg.getPanel().getComponent(0);
119         if (dp.surface == null) {
120             ta.append("Sorry your zeroth component is not a Surface.");
121             return;
122         }
123
124         dg.mouseClicked(new MouseEvent JavaDoc(dp.surface, MouseEvent.MOUSE_CLICKED, 0, 0, 10, 10, 1, false));
125
126         try { thread.sleep(3333); } catch (Exception JavaDoc e) { return; }
127
128         ta.append("Clicking the ToolBar double document button\n");
129         try { thread.sleep(3333); } catch (Exception JavaDoc e) { return; }
130
131         dp = (DemoPanel) dg.clonePanels[0].getComponent(0);
132
133         if (dp.tools != null) {
134             for (int i = 0; i < 3 && thread != null; i++) {
135                 ta.append(" Cloning\n");
136                 dp.tools.cloneB.doClick();
137                 try { thread.sleep(3333); } catch (Exception JavaDoc e) { return; }
138             }
139         }
140
141         ta.append("Changing attributes \n");
142
143         try { thread.sleep(3333); } catch (Exception JavaDoc e) { return; }
144
145         Component JavaDoc cmps[] = dg.clonePanels[0].getComponents();
146         for (int i = 0; i < cmps.length && thread != null; i++) {
147             if ((dp = (DemoPanel) cmps[i]).tools == null) {
148                 continue;
149             }
150             switch (i) {
151                 case 0 : ta.append(" Changing AntiAliasing\n");
152                          dp.tools.aliasB.doClick();
153                          break;
154                 case 1 : ta.append(" Changing Composite & Texture\n");
155                          dp.tools.compositeB.doClick();
156                          dp.tools.textureB.doClick();
157                          break;
158                 case 2 : ta.append(" Changing Screen\n");
159                          dp.tools.screenCombo.setSelectedIndex(4);
160                          break;
161                 case 3 : ta.append(" Removing a clone\n");
162                          dp.tools.cloneB.doClick();
163             }
164             try { thread.sleep(3333); } catch (Exception JavaDoc e) { return; }
165         }
166
167         ta.append("\nAll Done!");
168     }
169 }
170
Popular Tags