Example 23.15: The world class
The next requirement is a class to represent a VRML world. A world will hold various nodes as well as comments and the requisite version string. Example 23.15 is this class.
package elharo.vrml;
import java.util.Vector;
import java.util.Enumeration;
public class world {
String version = "#VRML V1.0 ascii\n\n";
// a Vector to hold the different objects
// in the world
Vector nodes;
public world() {
nodes = new Vector();
nodes.addElement(version);
}
public void comment(String s) {
nodes.addElement("#" + s);
}
public void addShape(shape s) {
nodes.addElement(s.draw());
}
public void draw() {
for (Enumeration e = nodes.elements();
e.hasMoreElements();) {
Object o = e.nextElement();
// There are two possible classes in the
// Vector, shapes and Strings
if (o instanceof shape) {
shape sh = (shape) o;
System.out.println(sh.draw());
}
else if (o instanceof String) {
System.out.println((String) o);
}
}
}
}
In Example 23.15 a VRML world is represented as a Vector that contains shapes and Strings. When the world is drawn all of these are printed on System.out. This can be stored in a file and served off a web server.
Copyright 1996 Elliotte Rusty Harold
elharo@sunsite.unc.edu
This Chapter
Examples
Home