home *** CD-ROM | disk | FTP | other *** search
- // VRML Generator
- // Copyright Justin Couch 1996
- //
- // Chapter 13: Late Night VRML 2.0 and Java
- //
- // Shape class
-
- 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.VrmlObject;
-
- public class Group extends GroupingNode
- {
- private int num_children = 0;
-
- // constructor builds a copy of itself and is added to the scene
- // graph.
- public Group()
- {
- _children = new Vector();
-
- Browser b = Browser.getBrowser();
-
- if(b == null)
- return;
-
- node = b.createVrmlFromString("Transform {}")[0];
- _addChildren = (EventInMFNode)node.getEventIn("addChildren");
- _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
- _set_children = (EventInMFNode)node.getEventIn("children");
-
- have_browser = true;
- }
-
- public Group(Browser b)
- {
- _children = new Vector();
-
- if(b == null)
- return;
-
- node = b.createVrmlFromString("Transform {}")[0];
- _addChildren = (EventInMFNode)node.getEventIn("addChildren");
- _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
- _set_children = (EventInMFNode)node.getEventIn("children");
-
- have_browser = true;
- }
-
- public Group(float[] bboxCenter, float[] bboxSize)
- {
- _children = new Vector();
-
- Browser b = Browser.getBrowser();
-
- if(b == null)
- return;
-
- String vrml_string = "Transform { ";
-
- if((bboxCenter[0] != 0) && (bboxCenter[1] != 0) && (bboxCenter[2] != 0))
- {
- vrml_string += "bboxCenter " +
- bboxCenter[0] + " " +
- bboxCenter[1] + " " +
- bboxCenter[2] + " ";
-
- _bboxCenter = bboxCenter;
- }
-
- if((bboxSize[0] != -1) && (bboxSize[1] != -1) && (bboxSize[2] != -1))
- {
- vrml_string += "bboxCenter " +
- bboxSize[0] + " " +
- bboxSize[1] + " " +
- bboxSize[2] + " ";
-
- _bboxSize = bboxSize;
- }
-
- vrml_string += "}";
-
- node = b.createVrmlFromString(vrml_string)[0];
- _addChildren = (EventInMFNode)node.getEventIn("addChilren");
- _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
- _set_children = (EventInMFNode)node.getEventIn("children");
-
- have_browser = true;
- }
-
- public Group(Browser b, float[] bboxCenter, float[] bboxSize)
- {
- _children = new Vector();
-
- if(b == null)
- return;
-
- String vrml_string = "Transform { ";
-
- if((bboxCenter[0] != 0) && (bboxCenter[1] != 0) && (bboxCenter[2] != 0))
- {
- vrml_string += "bboxCenter " +
- bboxCenter[0] + " " +
- bboxCenter[1] + " " +
- bboxCenter[2] + " ";
-
- _bboxCenter = bboxCenter;
- }
-
- if((bboxSize[0] != -1) && (bboxSize[1] != -1) && (bboxSize[2] != -1))
- {
- vrml_string += "bboxCenter " +
- bboxSize[0] + " " +
- bboxSize[1] + " " +
- bboxSize[2] + " ";
-
- _bboxSize = bboxSize;
- }
-
- vrml_string += " }";
-
- node = b.createVrmlFromString(vrml_string)[0];
- _addChildren = (EventInMFNode)node.getEventIn("addChilren");
- _removeChildren = (EventInMFNode)node.getEventIn("removeChildren");
- _set_children = (EventInMFNode)node.getEventIn("children");
-
- have_browser = true;
- }
-
- public void finalize()
- {
- // force the clean up of internally allocated objects
- _children = null;
- _bboxCenter = null;
- _bboxSize = null;
- }
-
- public void writeToFile(PrintStream fp, int indent)
- {
- int i;
- StringBuffer buffer = new StringBuffer();
-
- for(i = 0; i < indent; i++)
- buffer.append(" ");
-
- fp.print(buffer.toString());
- if(name != null)
- fp.print("DEF " + name + " ");
-
- fp.println("Group {");
-
- // print the contents of the node
-
- if(num_children != 0)
- {
- Enumeration e = _children.elements();
-
- fp.print(buffer.toString() + " children " );
- if(num_children != 1)
- fp.println("[");
-
- for(;e.hasMoreElements();)
- ((VrmlObject)(e.nextElement())).writeToFile(fp, indent + 2);
-
- if(num_children != 1)
- fp.println(buffer.toString() + " ]");
- }
-
- if(_bboxCenter != null)
- fp.println(buffer.toString() + " bboxCenter " +
- _bboxCenter[0] + " " +
- _bboxCenter[1] + " " +
- _bboxCenter[2]);
-
- if(_bboxSize != null)
- fp.println(buffer.toString() + " bboxSize " +
- _bboxSize[0] + " " +
- _bboxSize[1] + " " +
- _bboxSize[2]);
-
- fp.println(buffer.toString() + "}");
- }
-
- public void addChildren(VrmlObject child)
- {
- Node[] n = new Node[1];
- n[0] = child.node;
-
- _children.addElement(child);
- if(have_browser)
- _addChildren.setValue(n);
- num_children++;
- }
-
- public void removeChildren(VrmlObject child)
- {
- Node[] n = new Node[1];
- n[0] = child.node;
-
- _children.removeElement(child);
- if(have_browser)
- _addChildren.setValue(n);
- num_children--;
- }
-
- 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);
- }
- }
-