KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > DemoPanel


1 /*
2  * @(#)DemoPanel.java 1.22 06/08/09
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  * @(#)DemoPanel.java 1.22 06/08/09
39  */

40
41
42 package java2d;
43
44 import java.awt.*;
45 import javax.swing.JPanel JavaDoc;
46 import javax.swing.border.EmptyBorder JavaDoc;
47 import javax.swing.border.SoftBevelBorder JavaDoc;
48 import javax.swing.border.CompoundBorder JavaDoc;
49
50 import static java2d.CustomControlsContext.State.*;
51
52 /**
53  * The panel for the Surface, Custom Controls & Tools.
54  * Other component types welcome.
55  */

56 public class DemoPanel extends JPanel JavaDoc {
57
58     public Surface surface;
59     public CustomControlsContext ccc;
60     public Tools tools;
61     public String JavaDoc className;
62
63
64     public DemoPanel(Object JavaDoc obj) {
65         setLayout(new BorderLayout());
66         try {
67             if (obj instanceof String JavaDoc) {
68                 className = (String JavaDoc) obj;
69                 obj = Class.forName(className).newInstance();
70             }
71             if (obj instanceof Component) {
72                 add((Component) obj);
73             }
74             if (obj instanceof Surface) {
75                 add("South", tools = new Tools(surface = (Surface) obj));
76             }
77             if (obj instanceof CustomControlsContext) {
78                 ccc = (CustomControlsContext) obj;
79                 Component cmps[] = ccc.getControls();
80                 String JavaDoc cons[] = ccc.getConstraints();
81                 for (int i = 0; i < cmps.length; i++) {
82                     add(cmps[i], cons[i]);
83                 }
84             }
85         } catch (Exception JavaDoc e) {
86             e.printStackTrace();
87         }
88     }
89
90
91     public void start() {
92     if (surface != null)
93         surface.startClock();
94         if (tools != null && surface != null) {
95             if (tools.startStopB != null && tools.startStopB.isSelected()) {
96                    surface.animating.start();
97             }
98         }
99         if (ccc != null
100             && Java2Demo.ccthreadCB != null
101                 && Java2Demo.ccthreadCB.isSelected())
102         {
103             ccc.handleThread(START);
104         }
105     }
106
107
108     public void stop() {
109         if (surface != null) {
110             if (surface.animating != null) {
111                 surface.animating.stop();
112             }
113             surface.bimg = null;
114         }
115         if (ccc != null) {
116             ccc.handleThread(STOP);
117         }
118     }
119
120
121     public void setDemoBorder(JPanel JavaDoc p) {
122         int top = (p.getComponentCount()+1 >= 3) ? 0 : 5;
123         int left = ((p.getComponentCount()+1) % 2) == 0 ? 0 : 5;
124         EmptyBorder JavaDoc eb = new EmptyBorder JavaDoc(top,left,5,5);
125         SoftBevelBorder JavaDoc sbb = new SoftBevelBorder JavaDoc(SoftBevelBorder.RAISED);
126         setBorder(new CompoundBorder JavaDoc(eb, sbb));
127     }
128 }
129
Popular Tags