home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch12 / geometry / VrmlRoot.bak < prev    next >
Text File  |  1997-01-05  |  3KB  |  146 lines

  1. // VRML Generator
  2. // Copyright Justin Couch 1996
  3. //
  4. // Chapter 13: Late Night VRML 2.0 and Java
  5. //
  6. // The class that represents the root of the scene graph.
  7.  
  8. package geometry;
  9.  
  10. import java.io.*;
  11. import java.awt.*;
  12. import java.util.Vector;
  13. import java.util.Enumeration;
  14. import vrml.external.*;
  15. import vrml.external.field.*;
  16. import vrml.external.exception.*;
  17. import geometry.*;
  18.  
  19. public class VrmlRoot extends GroupingNode
  20. {
  21.     // constructor builds a copy of itself and is added to the scene
  22.     // graph.
  23.     public VrmlRoot()
  24.     {
  25.         Browser b = Browser.getBrowser();
  26.  
  27.         if(b != null)
  28.         {
  29.             try
  30.             {
  31.                 node = b.getNode("root");
  32.             }
  33.             catch(InvalidNodeException e)
  34.             {
  35.                 System.out.println("Unable to obtain the root node from the VRML world");
  36.             }
  37.             _addChildren = (EventInMFNode)node.getEventIn("addChilren");
  38.             _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
  39.             _set_children = (EventInMFNode)node.getEventIn("children");
  40.  
  41.             have_browser = true;
  42.         }
  43.         _children = new Vector();
  44.     }
  45.  
  46.     public VrmlRoot(Browser b)
  47.     {
  48.         if(b != null)
  49.         {
  50.             try
  51.             {
  52.                 while(node == null)
  53.                     node = b.getNode("root");
  54.  
  55.                 System.out.println("Scene root obtained from the VRML browser");
  56.             }
  57.             catch(InvalidNodeException e)
  58.             {
  59.                 System.out.println("Unable to obtain the root node from the VRML world");
  60.             }
  61.  
  62.             _addChildren = (EventInMFNode)node.getEventIn("addChildren");
  63.             _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
  64.             _set_children = (EventInMFNode)node.getEventIn("children");
  65.  
  66.             have_browser = true;
  67.         }
  68.         _children = new Vector();
  69.     }
  70.  
  71.     public void finalize()
  72.     {
  73.         // force the clean up of internally allocated objects
  74.         _children = null;
  75.     }
  76.  
  77.     public void writeToFile(PrintStream fp)
  78.     {
  79.         Enumeration e = _children.elements();
  80.  
  81.         fp.println("#VRML V2.0 utf8");
  82.         fp.println("#");
  83.         fp.println("# Generated by the VermelGen");
  84.         fp.println("# Software by The Virtual Light Company");
  85.         fp.println("# Released under the GPL ftp://ftp.sunsite.edu/pub/GNU/copyinfo.txt");
  86.         fp.println("");
  87.  
  88.         for(; e.hasMoreElements();)
  89.             ((VrmlObject)(e.nextElement())).writeToFile(fp, 0);
  90.     }
  91.  
  92.     // this needs to be here to satisfy the abstract class implementation. Just
  93.     // provides a safety buffer in case someone, somehow calls this version
  94.     // rather than the one above without the indent.
  95.     public void writeToFile(PrintStream fp, int indent)
  96.     {
  97.         writeToFile(fp);
  98.     }
  99.  
  100.     public void addChildren(VrmlObject child)
  101.     {
  102.         Node[] n = new Node[1];
  103.         n[0] = child.node;
  104.  
  105.         _children.addElement(child);
  106.         if(have_browser)
  107.             _addChildren.setValue(n);
  108.     }
  109.  
  110.     public void removeChildren(VrmlObject child)
  111.     {
  112.         Node[] n = new Node[1];
  113.         n[0] = child.node;
  114.  
  115.         _children.removeElement(child);
  116.         if(have_browser)
  117.             _addChildren.setValue(n);
  118.     }
  119.  
  120.     public void set_children(VrmlObject[] child_list)
  121.     {
  122.         int    i;
  123.         int num = child_list.length;
  124.         Node[]     node_list;
  125.  
  126.         // replace the current _children list with the new list.
  127.         // force the garbage collection
  128.         _children = null;
  129.  
  130.         // assign the new arrays.
  131.         _children = new Vector(num);
  132.         node_list = new Node[num];
  133.  
  134.         // copy the values across.
  135.         for(i = 0; i < num; i++)
  136.         {
  137.             _children.addElement(child_list[i]);
  138.             node_list[i] = child_list[i].node;
  139.         }
  140.  
  141.         // set the values in the VRML scene
  142.         if(have_browser)
  143.             _set_children.setValue(node_list);
  144.     }
  145. }
  146.