001: /*
002: * $RCSfile: AlternateAppearanceScopeTest.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.2 $
041: * $Date: 2007/02/09 17:21:31 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.alternate_appearance;
046:
047: import java.applet.Applet;
048: import java.awt.*;
049: import java.awt.event.*;
050: import com.sun.j3d.utils.applet.MainFrame;
051: import com.sun.j3d.utils.geometry.*;
052: import com.sun.j3d.utils.universe.*;
053: import javax.media.j3d.*;
054: import javax.vecmath.*;
055: import javax.swing.*;
056: import javax.swing.event.*;
057: import javax.swing.border.*;
058: import com.sun.j3d.utils.behaviors.vp.*;
059:
060: public class AlternateAppearanceScopeTest extends JApplet implements
061: ActionListener {
062:
063: Material mat1, altMat;
064: Appearance app, otherApp;
065: JComboBox altAppMaterialColor;
066: JComboBox appMaterialColor;
067: JComboBox altAppScoping;
068: JComboBox override;
069: private Group content1 = null;
070: private Group content2 = null;
071: BoundingSphere worldBounds;
072: AlternateAppearance altApp;
073: Shape3D[] shapes1, shapes2;
074: boolean shape1Enabled = false, shape2Enabled = false;
075: // Globally used colors
076: Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
077: Color3f red = new Color3f(1.0f, 0.0f, 0.0f);
078: Color3f green = new Color3f(0.0f, 1.0f, 0.0f);
079: Color3f blue = new Color3f(0.0f, 0.0f, 1.0f);
080: Color3f[] colors = { white, red, green, blue };
081:
082: private SimpleUniverse u;
083:
084: public AlternateAppearanceScopeTest() {
085: }
086:
087: public void init() {
088: Container contentPane = getContentPane();
089:
090: Canvas3D c = new Canvas3D(SimpleUniverse
091: .getPreferredConfiguration());
092: contentPane.add("Center", c);
093:
094: BranchGroup scene = createSceneGraph();
095: // SimpleUniverse is a Convenience Utility class
096: u = new SimpleUniverse(c);
097:
098: // add mouse behaviors to the viewingPlatform
099: ViewingPlatform viewingPlatform = u.getViewingPlatform();
100:
101: // This will move the ViewPlatform back a bit so the
102: // objects in the scene can be viewed.
103: viewingPlatform.setNominalViewingTransform();
104:
105: OrbitBehavior orbit = new OrbitBehavior(c,
106: OrbitBehavior.REVERSE_ALL);
107: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
108: 0.0, 0.0), 100.0);
109: orbit.setSchedulingBounds(bounds);
110: viewingPlatform.setViewPlatformBehavior(orbit);
111:
112: u.addBranchGraph(scene);
113:
114: // Create GUI
115: JPanel p = new JPanel();
116: BoxLayout boxlayout = new BoxLayout(p, BoxLayout.Y_AXIS);
117: p.add(createScopingPanel());
118: p.add(createMaterialPanel());
119: p.setLayout(boxlayout);
120:
121: contentPane.add("South", p);
122: }
123:
124: public void destroy() {
125: u.cleanup();
126: }
127:
128: BranchGroup createSceneGraph() {
129: BranchGroup objRoot = new BranchGroup();
130:
131: // Create influencing bounds
132: worldBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), // Center
133: 1000.0); // Extent
134:
135: Transform3D t = new Transform3D();
136: // move the object upwards
137: t.set(new Vector3f(0.0f, 0.1f, 0.0f));
138: // Shrink the object
139: t.setScale(0.8);
140:
141: TransformGroup trans = new TransformGroup(t);
142: trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
143: trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
144:
145: otherApp = new Appearance();
146: altMat = new Material();
147: altMat.setCapability(Material.ALLOW_COMPONENT_WRITE);
148: altMat.setDiffuseColor(new Color3f(0.0f, 1.0f, 0.0f));
149: otherApp.setMaterial(altMat);
150:
151: altApp = new AlternateAppearance();
152: altApp.setAppearance(otherApp);
153: altApp.setCapability(AlternateAppearance.ALLOW_SCOPE_WRITE);
154: altApp.setCapability(AlternateAppearance.ALLOW_SCOPE_READ);
155: altApp.setInfluencingBounds(worldBounds);
156: objRoot.addChild(altApp);
157:
158: // Build foreground geometry into two groups. We'll
159: // create three directional lights below, one each with
160: // scope to cover the first geometry group only, the
161: // second geometry group only, or both geometry groups.
162: Appearance app1 = new Appearance();
163: mat1 = new Material();
164: mat1.setCapability(Material.ALLOW_COMPONENT_WRITE);
165: mat1.setDiffuseColor(new Color3f(1.0f, 0.0f, 0.0f));
166: app1.setMaterial(mat1);
167: content1 = new SphereGroup(0.05f, // radius of spheres
168: 0.4f, // x spacing
169: 0.2f, // y spacing
170: 3, // number of spheres in X
171: 5, // number of spheres in Y
172: app1, // appearance
173: true); // alt app override = true
174: trans.addChild(content1);
175: shapes1 = ((SphereGroup) content1).getShapes();
176:
177: content2 = new SphereGroup(0.05f, // radius of spheres
178: .4f, // x spacing
179: 0.2f, // y spacing
180: 2, // number of spheres in X
181: 5, // number of spheres in Y
182: app1, // appearance
183: true); // alt app override = true
184: trans.addChild(content2);
185: shapes2 = ((SphereGroup) content2).getShapes();
186:
187: // Add lights
188: DirectionalLight light1 = null;
189: light1 = new DirectionalLight();
190: light1.setEnable(true);
191: light1.setColor(new Color3f(0.2f, 0.2f, 0.2f));
192: light1.setDirection(new Vector3f(1.0f, 0.0f, -1.0f));
193: light1.setInfluencingBounds(worldBounds);
194: objRoot.addChild(light1);
195:
196: DirectionalLight light2 = new DirectionalLight();
197: light2.setEnable(true);
198: light2.setColor(new Color3f(0.2f, 0.2f, 0.2f));
199: light2.setDirection(new Vector3f(-1.0f, 0.0f, 1.0f));
200: light2.setInfluencingBounds(worldBounds);
201: objRoot.addChild(light2);
202:
203: // Add an ambient light to dimly illuminate the rest of
204: // the shapes in the scene to help illustrate that the
205: // directional lights are being scoped... otherwise it looks
206: // like we're just removing shapes from the scene
207: AmbientLight ambient = new AmbientLight();
208: ambient.setEnable(true);
209: ambient.setColor(new Color3f(1.0f, 1.0f, 1.0f));
210: ambient.setInfluencingBounds(worldBounds);
211: objRoot.addChild(ambient);
212:
213: objRoot.addChild(trans);
214:
215: return objRoot;
216: }
217:
218: JPanel createScopingPanel() {
219: JPanel panel = new JPanel();
220: panel.setBorder(new TitledBorder("Scopes"));
221:
222: String values[] = { "Scoped Set1", "Scoped Set2",
223: "Universal Scope" };
224: altAppScoping = new JComboBox(values);
225: altAppScoping.addActionListener(this );
226: altAppScoping.setSelectedIndex(2);
227: panel.add(new JLabel("Scoping"));
228: panel.add(altAppScoping);
229:
230: String enables[] = { "Enabled Set1", "Enabled Set2",
231: "Enabled set1&2", "Disabled set1&2" };
232:
233: override = new JComboBox(enables);
234: override.addActionListener(this );
235: override.setSelectedIndex(3);
236: panel.add(new JLabel("Alternate Appearance Override"));
237: panel.add(override);
238:
239: return panel;
240:
241: }
242:
243: JPanel createMaterialPanel() {
244: JPanel panel = new JPanel();
245: panel.setBorder(new TitledBorder("Appearance Attributes"));
246:
247: String colorVals[] = { "WHITE", "RED", "GREEN", "BLUE" };
248:
249: altAppMaterialColor = new JComboBox(colorVals);
250: altAppMaterialColor.addActionListener(this );
251: altAppMaterialColor.setSelectedIndex(2);
252: panel.add(new JLabel("Alternate Appearance MaterialColor"));
253: panel.add(altAppMaterialColor);
254:
255: appMaterialColor = new JComboBox(colorVals);
256: appMaterialColor.addActionListener(this );
257: appMaterialColor.setSelectedIndex(1);
258: panel.add(new JLabel("Normal Appearance MaterialColor"));
259: panel.add(appMaterialColor);
260:
261: return panel;
262:
263: }
264:
265: public void actionPerformed(ActionEvent e) {
266: Object target = e.getSource();
267: if (target == altAppMaterialColor) {
268: altMat.setDiffuseColor(colors[altAppMaterialColor
269: .getSelectedIndex()]);
270: } else if (target == altAppScoping) {
271: for (int i = 0; i < altApp.numScopes(); i++) {
272: altApp.removeScope(0);
273: }
274: if (altAppScoping.getSelectedIndex() == 0) {
275: altApp.addScope(content1);
276: } else if (altAppScoping.getSelectedIndex() == 1) {
277: altApp.addScope(content2);
278: }
279: } else if (target == override) {
280: int i;
281: if (override.getSelectedIndex() == 0) {
282: if (!shape1Enabled) {
283: for (i = 0; i < shapes1.length; i++)
284: shapes1[i].setAppearanceOverrideEnable(true);
285: shape1Enabled = true;
286: }
287:
288: if (shape2Enabled) {
289: for (i = 0; i < shapes2.length; i++)
290: shapes2[i].setAppearanceOverrideEnable(false);
291: shape2Enabled = false;
292: }
293: } else if (override.getSelectedIndex() == 1) {
294: if (!shape2Enabled) {
295: for (i = 0; i < shapes2.length; i++)
296: shapes2[i].setAppearanceOverrideEnable(true);
297: shape2Enabled = true;
298: }
299:
300: if (shape1Enabled) {
301: for (i = 0; i < shapes1.length; i++)
302: shapes1[i].setAppearanceOverrideEnable(false);
303: shape1Enabled = false;
304: }
305: } else if (override.getSelectedIndex() == 2) {
306: if (!shape1Enabled) {
307: for (i = 0; i < shapes1.length; i++)
308: shapes1[i].setAppearanceOverrideEnable(true);
309: shape1Enabled = true;
310: }
311: if (!shape2Enabled) {
312: for (i = 0; i < shapes2.length; i++)
313: shapes2[i].setAppearanceOverrideEnable(true);
314: shape2Enabled = true;
315: }
316: } else {
317: if (shape1Enabled) {
318: for (i = 0; i < shapes1.length; i++)
319: shapes1[i].setAppearanceOverrideEnable(false);
320: shape1Enabled = false;
321: }
322:
323: if (shape2Enabled) {
324: for (i = 0; i < shapes2.length; i++)
325: shapes2[i].setAppearanceOverrideEnable(false);
326: shape2Enabled = false;
327: }
328: }
329:
330: } else if (target == appMaterialColor) {
331: mat1.setDiffuseColor(colors[appMaterialColor
332: .getSelectedIndex()]);
333: }
334:
335: }
336:
337: public static void main(String[] args) {
338: Frame frame = new MainFrame(new AlternateAppearanceScopeTest(),
339: 800, 800);
340: }
341:
342: }
|