home *** CD-ROM | disk | FTP | other *** search
- // VRML Generator
- // Copyright Justin Couch 1996
- //
- // Chapter 13: Late Night VRML 2.0 and Java
- //
- // The class that represents the root of the scene graph.
-
- package geometry;
-
- import java.io.*;
- import java.awt.*;
- import java.util.Vector;
- import java.util.Enumeration;
- import vrml.external.*;
- import vrml.external.field.*;
- import vrml.external.exception.*;
- import geometry.*;
-
- public class VrmlRoot extends GroupingNode
- {
- // constructor builds a copy of itself and is added to the scene
- // graph.
- public VrmlRoot()
- {
- Browser b = Browser.getBrowser();
-
- if(b != null)
- {
- try
- {
- // loop until we can access the scene root. This should not take
- // too long since we already have access to the main browser.
- while(node == null)
- node = b.getNode("root");
-
- System.out.println("Scene root obtained from the VRML browser");
- }
- catch(InvalidNodeException e)
- {
- System.out.println("Unable to obtain the root node from the VRML world");
- }
- _addChildren = (EventInMFNode)node.getEventIn("addChilren");
- _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
- _set_children = (EventInMFNode)node.getEventIn("children");
-
- have_browser = true;
- }
- _children = new Vector();
- }
-
- public VrmlRoot(Browser b)
- {
- if(b != null)
- {
- try
- {
- // loop until we can access the scene root. This should not take
- // too long since we already have access to the main browser.
- while(node == null)
- node = b.getNode("root");
-
- System.out.println("Scene root obtained from the VRML browser");
- }
- catch(InvalidNodeException e)
- {
- System.out.println("Unable to obtain the root node from the VRML world");
- }
-
- _addChildren = (EventInMFNode)node.getEventIn("addChildren");
- _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
- _set_children = (EventInMFNode)node.getEventIn("children");
-
- have_browser = true;
- }
- _children = new Vector();
- }
-
- public void finalize()
- {
- // force the clean up of internally allocated objects
- _children = null;
- }
-
- public void writeToFile(PrintStream fp)
- {
- Enumeration e = _children.elements();
-
- fp.println("#VRML V2.0 utf8");
- fp.println("#");
- fp.println("# Generated by the VermelGen");
- fp.println("# Software by The Virtual Light Company");
- fp.println("# Released under the GPL ftp://ftp.sunsite.edu/pub/GNU/copyinfo.txt");
- fp.println("");
-
- for(; e.hasMoreElements();)
- ((VrmlObject)(e.nextElement())).writeToFile(fp, 0);
- }
-
- // this needs to be here to satisfy the abstract class implementation. Just
- // provides a safety buffer in case someone, somehow calls this version
- // rather than the one above without the indent.
- public void writeToFile(PrintStream fp, int indent)
- {
- writeToFile(fp);
- }
-
- public void addChildren(VrmlObject child)
- {
- Node[] n = new Node[1];
- n[0] = child.node;
-
- _children.addElement(child);
- if(have_browser)
- _addChildren.setValue(n);
- }
-
- public void removeChildren(VrmlObject child)
- {
- Node[] n = new Node[1];
- n[0] = child.node;
-
- _children.removeElement(child);
- if(have_browser)
- _addChildren.setValue(n);
- }
-
- public void set_children(VrmlObject[] child_list)
- {
- int i;
- int num = child_list.length;
- Node[] node_list;
-
- // replace the current _children list with the new list.
- // force the garbage collection
- _children = null;
-
- // assign the new arrays.
- _children = new Vector(num);
- node_list = new Node[num];
-
- // copy the values across.
- for(i = 0; i < num; i++)
- {
- _children.addElement(child_list[i]);
- node_list[i] = child_list[i].node;
- }
-
- // set the values in the VRML scene
- if(have_browser)
- _set_children.setValue(node_list);
- }
- }
-