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

  1. // VRML Generator
  2. // Copyright Justin Couch 1996
  3. //
  4. // Chapter 13: Late Night VRML 2.0 and Java
  5. //
  6. // Shape class
  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.VrmlObject;
  18.  
  19. public class Group extends GroupingNode
  20. {
  21.     private int num_children = 0;
  22.  
  23.     // constructor builds a copy of itself and is added to the scene
  24.     // graph.
  25.     public Group()
  26.     {
  27.         _children = new Vector();
  28.  
  29.         Browser b = Browser.getBrowser();
  30.  
  31.         if(b == null)
  32.             return;
  33.  
  34.         node = b.createVrmlFromString("Group {}")[0];
  35.         _addChildren = (EventInMFNode)node.getEventIn("addChildren");
  36.         _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
  37.         _set_children = (EventInMFNode)node.getEventIn("children");
  38.  
  39.         have_browser = true;
  40.     }
  41.  
  42.     public Group(Browser b)
  43.     {
  44.         _children = new Vector();
  45.  
  46.         if(b == null)
  47.             return;
  48.  
  49.         node = b.createVrmlFromString("Group {}")[0];
  50.         _addChildren = (EventInMFNode)node.getEventIn("addChildren");
  51.         _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
  52.         _set_children = (EventInMFNode)node.getEventIn("children");
  53.  
  54.         have_browser = true;
  55.     }
  56.  
  57.     public Group(float[] bboxCenter, float[] bboxSize)
  58.     {
  59.         _children = new Vector();
  60.  
  61.         Browser b = Browser.getBrowser();
  62.  
  63.         if(b == null)
  64.             return;
  65.  
  66.         String vrml_string = "Transform { ";
  67.  
  68.         if((bboxCenter[0] != 0) && (bboxCenter[1] != 0) && (bboxCenter[2] != 0))
  69.         {
  70.             vrml_string += "bboxCenter " +
  71.                             bboxCenter[0] + " " +
  72.                             bboxCenter[1] + " " +
  73.                             bboxCenter[2] + " ";
  74.  
  75.             _bboxCenter = bboxCenter;
  76.         }
  77.  
  78.         if((bboxSize[0] != -1) && (bboxSize[1] != -1) && (bboxSize[2] != -1))
  79.         {
  80.             vrml_string += "bboxCenter " +
  81.                             bboxSize[0] + " " +
  82.                             bboxSize[1] + " " +
  83.                             bboxSize[2] + " ";
  84.  
  85.             _bboxSize = bboxSize;
  86.         }
  87.  
  88.         vrml_string += "}";
  89.  
  90.         node = b.createVrmlFromString(vrml_string)[0];
  91.         _addChildren = (EventInMFNode)node.getEventIn("addChilren");
  92.         _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
  93.         _set_children = (EventInMFNode)node.getEventIn("children");
  94.  
  95.         have_browser = true;
  96.     }
  97.  
  98.     public Group(Browser b, float[] bboxCenter, float[] bboxSize)
  99.     {
  100.         _children = new Vector();
  101.  
  102.         if(b == null)
  103.             return;
  104.  
  105.         String vrml_string = "Transform { ";
  106.  
  107.         if((bboxCenter[0] != 0) && (bboxCenter[1] != 0) && (bboxCenter[2] != 0))
  108.         {
  109.             vrml_string += "bboxCenter " +
  110.                             bboxCenter[0] + " " +
  111.                             bboxCenter[1] + " " +
  112.                             bboxCenter[2] + " ";
  113.  
  114.             _bboxCenter = bboxCenter;
  115.         }
  116.  
  117.         if((bboxSize[0] != -1) && (bboxSize[1] != -1) && (bboxSize[2] != -1))
  118.         {
  119.             vrml_string += "bboxCenter " +
  120.                             bboxSize[0] + " " +
  121.                             bboxSize[1] + " " +
  122.                             bboxSize[2] + " ";
  123.  
  124.             _bboxSize = bboxSize;
  125.         }
  126.  
  127.         vrml_string += " }";
  128.  
  129.         node = b.createVrmlFromString(vrml_string)[0];
  130.         _addChildren = (EventInMFNode)node.getEventIn("addChilren");
  131.         _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
  132.         _set_children = (EventInMFNode)node.getEventIn("children");
  133.  
  134.         have_browser = true;
  135.     }
  136.  
  137.     public void finalize()
  138.     {
  139.         // force the clean up of internally allocated objects
  140.         _children = null;
  141.         _bboxCenter = null;
  142.         _bboxSize = null;
  143.     }
  144.  
  145.     public void writeToFile(PrintStream fp, int indent)
  146.     {
  147.         int    i;
  148.         StringBuffer buffer = new StringBuffer();
  149.  
  150.         for(i = 0; i < indent; i++)
  151.             buffer.append("  ");
  152.  
  153.         fp.print(buffer.toString());
  154.         if(name != null)
  155.             fp.print("DEF " + name + " ");
  156.  
  157.         fp.println("Group {");
  158.  
  159.         // print the contents of the node
  160.  
  161.         if(num_children != 0)
  162.         {
  163.             Enumeration e = _children.elements();
  164.  
  165.             fp.print(buffer.toString() + "  children " );
  166.             if(num_children != 1)
  167.                 fp.println("[");
  168.  
  169.             for(;e.hasMoreElements();)
  170.                 ((VrmlObject)(e.nextElement())).writeToFile(fp, indent + 2);
  171.  
  172.             if(num_children != 1)
  173.                 fp.println(buffer.toString() + "  ]");
  174.         }
  175.  
  176.         if(_bboxCenter != null)
  177.             fp.println(buffer.toString() + " bboxCenter " +
  178.                         _bboxCenter[0] + " " +
  179.                         _bboxCenter[1] + " " +
  180.                         _bboxCenter[2]);
  181.  
  182.         if(_bboxSize != null)
  183.             fp.println(buffer.toString() + " bboxSize " +
  184.                         _bboxSize[0] + " " +
  185.                         _bboxSize[1] + " " +
  186.                         _bboxSize[2]);
  187.  
  188.         fp.println(buffer.toString() + "}");
  189.     }
  190.  
  191.     public void addChildren(VrmlObject child)
  192.     {
  193.         Node[] n = new Node[1];
  194.         n[0] = child.node;
  195.  
  196.         _children.addElement(child);
  197.         if(have_browser)
  198.             _addChildren.setValue(n);
  199.         num_children++;
  200.     }
  201.  
  202.     public void removeChildren(VrmlObject child)
  203.     {
  204.         Node[] n = new Node[1];
  205.         n[0] = child.node;
  206.  
  207.         _children.removeElement(child);
  208.         if(have_browser)
  209.             _addChildren.setValue(n);
  210.         num_children--;
  211.     }
  212.  
  213.     public void set_children(VrmlObject[] child_list)
  214.     {
  215.         int    i;
  216.         int num = child_list.length;
  217.         Node[]     node_list;
  218.  
  219.         // replace the current _children list with the new list.
  220.         // force the garbage collection
  221.         _children = null;
  222.  
  223.         // assign the new arrays.
  224.         _children = new Vector(num);
  225.         node_list = new Node[num];
  226.  
  227.         // copy the values across.
  228.         for(i = 0; i < num; i++)
  229.         {
  230.             _children.addElement(child_list[i]);
  231.             node_list[i] = child_list[i].node;
  232.         }
  233.  
  234.         // set the values in the VRML scene
  235.         if(have_browser)
  236.             _set_children.setValue(node_list);
  237.     }
  238. }
  239.