1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.jbeans.j3d.universe;
24
25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">class="comment"> ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment"> ------------------ Import classes (packages) ------------------- */
26 import java.awt.*;
27 import java.net.*;
28 import java.util.*;
29 import javax.media.j3d.*;
30
31
32
33
34 /***
35 * This class is basically the same as the SimpleUniverse. It, however, uses
36 * Locale to view and edit the SceneGraph nodes.
37 *
38 * @version $Revision: 1.3 $
39 * @author Masahiro Takatsuka (masa@jbeans.net)
40 * @see VirtualUniverse
41 */
42
43
44
45
46 public class Universe extends VirtualUniverse {
47 /***
48 * Locale reference needed to create the "view" portion
49 * of the scene graph.
50 */
51 private Locale locale;
52
53 /***
54 * Viewer reference needed to create the "view" portion
55 * of the scene graph.
56 */
57 private com.sun.j3d.utils.universe.Viewer viewer;
58
59 /***
60 * An array of viewers
61 */
62 private java.util.List viewers;
63
64 /***
65 * ViewingPlatform reference needed to create the "view" portion
66 * of the scene graph.
67 */
68 private ViewingPlatform viewingPlatform;
69
70 /***
71 * An array of viewers
72 */
73 private java.util.List viewingPlatforms;
74
75 /***
76 * World
77 */
78 private World world;
79
80 private int initialNumChild;
81
82 /***
83 * Creates a locale, a single ViewingPlatform, and
84 * and a Viewer object (both with their default values).
85 *
86 * @see Locale
87 * @see Viewer
88 * @see ViewingPlatform
89 */
90 Universe() {
91 this(null, 1, null, null);
92 }
93
94 /***
95 * Creates a locale, a single ViewingPlatform (with default values), and
96 * and a Viewer object. The Viewer object uses default values for
97 * everything but the canvas.
98 *
99 * @param canvas The canvas to associate with the Viewer object. Passing
100 * in null will cause this parameter to be ignored and a canvas to be
101 * created by the utility.
102 *
103 * @see Locale
104 * @see Viewer
105 * @see ViewingPlatform
106 */
107 Universe(Canvas3D canvas) {
108 this(null, 1, canvas, null);
109 }
110
111 /***
112 * Creates the "view" side of the scene graph. The passed in parameters
113 * override the default values where appropriate.
114 *
115 * @param origin The origin used to set the origin of the Locale object.
116 * If this object is null, then 0.0 is used.
117 * @param numTransforms The number of transforms to be in the
118 * MultiTransformGroup object.
119 * @param canvas The canvas to draw into. If this is null, it is
120 * ignored and a canvas will be created by the utility.
121 * @param userConfig The URL to the user's configuration file, used
122 * by the Viewer object. Passing in null causes the default values
123 * to be used.
124 *
125 * @see Locale
126 * @see Viewer
127 * @see ViewingPlatform
128 * @see MultiTransformGroup
129 */
130 Universe(HiResCoord origin, int numTransforms, Canvas3D canvas, URL userConfig) {
131
132 if (origin == null) {
133 this.locale = new Locale(this);
134 } else {
135 this.locale = new Locale(this, origin);
136 }
137
138 setViewingPlatformAndViewer(new ViewingPlatform(numTransforms),
139 new Viewer(canvas, userConfig));
140
141 setupWorld();
142 }
143
144 /***
145 * Creates the "view" side of the scene graph.
146 *
147 * @param viewingPlatform The viewingPlatform to use to create
148 * the "view" side of the scene graph.
149 * @param viewer The viewer object to use to create
150 * the "view" side of the scene graph.
151 */
152 Universe(ViewingPlatform viewingPlatform, com.sun.j3d.utils.universe.Viewer viewer) {
153 this.locale = new Locale(this);
154
155 setViewingPlatformAndViewer(viewingPlatform, viewer);
156
157
158 setupWorld();
159 }
160
161 private final void setViewingPlatformAndViewer(ViewingPlatform viewingPlatform, com.sun.j3d.utils.universe.Viewer viewer) {
162
163 if (this.viewingPlatforms == null) {
164 this.viewingPlatforms = Collections.synchronizedList(new ArrayList());
165 }
166 if (this.viewers == null) {
167 this.viewers = Collections.synchronizedList(new ArrayList());
168 }
169
170
171
172 this.viewingPlatform = viewingPlatform;
173 this.viewingPlatforms.add(this.viewingPlatform);
174 this.viewer = viewer;
175 this.viewers.add(this.viewer);
176
177
178 this.viewer.setViewingPlatform(this.viewingPlatform);
179 this.viewingPlatform.setViewer(this.viewer);
180 this.locale.setView(this.viewer.getView());
181
182 TransformGroup tg = this.viewingPlatform.getViewPlatformTransform();
183 tg.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ);
184
185
186
187 this.locale.addBranchGraph(this.viewingPlatform);
188 }
189
190 protected World createNewWorld(int numTransformGroup) {
191 World world = new World(numTransformGroup);
192 TransformGroup tg = world.getWorldTransform();
193 tg.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ);
194
195 return world;
196 }
197
198 public final void setWorld(World world) {
199 if (this.world != null) {
200 this.world.detach();
201 }
202 this.world = world;
203 this.locale.addBranchGraph(this.world);
204 }
205
206 public int getNumOfChildren() {
207 return this.world.getWorldTransform().numChildren();
208 }
209
210 public final void setInitialNumChildren() {
211 this.initialNumChild = this.world.getWorldTransform().numChildren();
212 }
213
214 protected final int getInitialNumChild() {
215 return this.initialNumChild;
216 }
217
218 private final void setupWorld() {
219 World world = createNewWorld(1);
220 setWorld(world);
221 }
222
223 /***
224 * Returns the Locale object associated with this scene graph.
225 *
226 * @return The Locale object used in the construction of this scene
227 * graph.
228 */
229 public final javax.media.j3d.Locale getLocale() {
230 return this.locale;
231 }
232
233 /***
234 * Returns the Viewer object associated with this scene graph.
235 * SimpleUniverse creates a single Viewer object for use in the
236 * scene graph.
237 *
238 * @return The Viewer object associated with this scene graph.
239 */
240 public final com.sun.j3d.utils.universe.Viewer getViewer() {
241 return this.viewer;
242 }
243
244 /***
245 * Returns the Viewer object associated with this scene graph.
246 * SimpleUniverse creates a single Viewer object for use in the
247 * scene graph.
248 *
249 * @return The Viewer object associated with this scene graph.
250 */
251 public final com.sun.j3d.utils.universe.Viewer getViewer(int index) {
252 return (com.sun.j3d.utils.universe.Viewer) this.viewers.get(index);
253 }
254
255 /***
256 * adds a viewer to the specified viewingPlatform.
257 */
258 private final void addViewer(ViewingPlatform platform, com.sun.j3d.utils.universe.Viewer viewer) {
259
260
261 this.viewer = viewer;
262 this.viewers.add(this.viewer);
263
264
265 this.viewer.setViewingPlatform(platform);
266 this.locale.setView(this.viewer.getView());
267
268 }
269 public final void addViewer(com.sun.j3d.utils.universe.Viewer viewer) {
270 addViewer(this.viewingPlatform, viewer);
271 }
272
273 /***
274 * adds a viewer to the specified viewingPlatform.
275 */
276 public final void addViewer(int index, com.sun.j3d.utils.universe.Viewer viewer) {
277
278
279 ViewingPlatform vp = (ViewingPlatform) this.viewingPlatforms.get(index);
280 if (vp != null) {
281 addViewer(vp, viewer);
282 }
283 }
284
285 /***
286 * removes the specified viewer.
287 */
288 public final void removeViewer(com.sun.j3d.utils.universe.Viewer viewer) {
289 if ((this.viewers.size() == 1) ||
290 (!this.viewers.contains(viewer))) {
291 return;
292 }
293 if (this.viewer == viewer) {
294 com.sun.j3d.utils.universe.Viewer newViewer = (Viewer) this.viewers.get(0);
295 this.viewer = newViewer;
296 }
297 if (viewer instanceof Viewer) {
298 com.sun.j3d.utils.universe.ViewingPlatform platform = ((Viewer) viewer).getViewingPlatform();
299 if (platform instanceof ViewingPlatform) {
300 ((ViewingPlatform) platform).removeViewer(viewer);
301 }
302 }
303 this.viewers.remove(viewer);
304 }
305
306 /***
307 * Returns the ViewingPlatform object associated with this scene graph.
308 *
309 * @return The ViewingPlatform object of this scene graph.
310 */
311 public final ViewingPlatform getViewingPlatform() {
312 return this.viewingPlatform;
313 }
314
315 /***
316 * Returns the ViewingPlatform object associated with this scene graph.
317 *
318 * @return The ViewingPlatform object of this scene graph.
319 */
320 public final ViewingPlatform getViewingPlatform(int index) {
321 return (ViewingPlatform) this.viewingPlatforms.get(index);
322 }
323
324 /***
325 * add a viewing platform.
326 */
327 public final void addViewingPlatform(ViewingPlatform viewingPlatform) {
328
329
330 this.viewingPlatform = viewingPlatform;
331 this.viewingPlatforms.add(this.viewingPlatform);
332
333 TransformGroup tg = this.viewingPlatform.getViewPlatformTransform();
334 tg.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ);
335
336
337
338 this.locale.addBranchGraph(this.viewingPlatform);
339 }
340
341 /***
342 * removes the specified viewing platform.
343 */
344 public final void removeViewingPlatform(ViewingPlatform viewingPlatform) {
345 if ((this.viewingPlatforms.size() == 1) ||
346 (!this.viewingPlatforms.contains(viewingPlatform))) {
347 return;
348 }
349 if (this.viewingPlatform == viewingPlatform) {
350 ViewingPlatform newPlatform = (ViewingPlatform) this.viewingPlatforms.get(0);
351 this.viewingPlatform = newPlatform;
352 }
353
354
355 this.locale.detachBranchGraph(viewingPlatform);
356 this.viewingPlatforms.remove(viewingPlatform);
357 }
358
359 /***
360 * Returns the Canvas3D object associated with this Java 3D Universe.
361 *
362 * @return A reference to the Canvas3D object associated with the
363 * Viewer object. This method is equivalent to calling getCanvas(0).
364 *
365 * @see Viewer
366 */
367 public Canvas3D getCanvas() {
368 return getCanvas(0);
369 }
370
371 /***
372 * Returns the Canvas3D object at the specified index associated with
373 * this Java 3D Universe.
374 *
375 * @param canvasNum The index of the Canvas3D object to retrieve.
376 * If there is no Canvas3D object for the given index, null is returned.
377 *
378 * @return A reference to the Canvas3D object associated with the
379 */
380 public Canvas3D getCanvas(int canvasNum) {
381 return this.viewer.getCanvas3D();
382 }
383
384 public World getWorld() {
385 return this.world;
386 }
387
388 /***
389 * Used to add Nodes to the geometry side (as opposed to the view side)
390 * of the scene graph. This is a short cut to getting the Locale object
391 * and calling that object's addBranchGraph() method.
392 *
393 * @param BranchGroup The BranchGroup to attach to this Universe's Locale.
394 */
395 public void addBranchGraph(BranchGroup bg) {
396 this.world.addBranchGraph(bg);
397 }
398
399 /***
400 * Detaches the specified BranchGroup object.
401 */
402 public final void removeBranchGraph(BranchGroup bg) {
403
404 this.world.detachBranchGraph(bg);
405 }
406
407 public final void removeAllBranchGraph() {
408 this.world.detachAllBranchGraph();
409 }
410
411 /***
412 * Finds the preferred <code>GraphicsConfiguration</code> object
413 * for the system. This object can then be used to create the
414 * Canvas3D objet for this system.
415 *
416 * @return The best <code>GraphicsConfiguration</code> object for
417 * the system.
418 */
419 public static GraphicsConfiguration getPreferredConfiguration() {
420 GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
421
422
423
424
425 String stereo = (String) java.security.AccessController.doPrivileged(
426 new java.security.PrivilegedAction() {
427 public Object run() {
428 return System.getProperties().getProperty("j3d.stereo");
429 }
430 });
431
432
433 if (stereo != null) {
434 if (stereo.equals("REQUIRED")) {
435 template.setStereo(template.REQUIRED);
436 } else if (stereo.equals("PREFERRED")) {
437 template.setStereo(template.PREFERRED);
438 }
439 } else {
440 template.setStereo(template.PREFERRED);
441 }
442
443
444 return GraphicsEnvironment.getLocalGraphicsEnvironment().
445 getDefaultScreenDevice().getBestConfiguration(template);
446 }
447 }