Using Polyhedra to Teach OOP
and Coordinate Geometry Concepts

Chapter 5:
Jumping to Java

by Kirby Urner
First posted: Nov 21, 1998
Last modified: Aug 25, 1999

In this demonstration, I implement the whole business all over again in Java. The code follows pretty much the same blueprints as I used for the VFP version.

Polyhedra from the synergetics concentric hierarchy get subclassed off a generic Shape class. Each is initialized with default properties, including color (user-modifiable) and volume (which changes during scaling).

Although spatial coordinates come in quadray format, with a Formats class standing by to do the quadray-to-xyz conversion as before, the info per each shape is hard coded into arrays (double[][] points and double[][] edges), not distilled from external archive tables. This is because Java has none of the native versatility with large data tables which Visual FoxPro does, although it back-ends into databases via the JDBC API.

For this version, I got around to adding some of the features missing from earlier drafts. For example, the VRML output defaults to using cylinders for edges, not the more primitive wire mesh (kept as an option). This means the VRML views and Povray renderings look more alike than before. As a consequence of adding this feature, I also got rid of redundant writes of spherical nubs at the ends of cylinders, plus made these nubs independently sizable and colorable. You can also elect to make the edges invisible, leaving only the vertices.

image

I also started experimenting with a Mesh (subclass of shape), which fits into a polyhedron's triangular or square faces (two different subclasses of Mesh for these circumstances). The MeshManager inner class updates an array of face-embedded Mesh objects everytime their "owner" is translated or rotated. Additionally, the Mesh subclasses know how to bulge or curve away from a common center, thereby giving us a basis for doing geodesic spheres of various kinds (including a color-coded "CYMK sphere" as per my Color Coordinated Quadrays paper).

To add new or modify existing "scenarios" (scenes composed of pre-defined shapes), you need to modify and recompile Main.java, which will require unjarring polys.jar in a subdirectory com/pdx4d/polys off your classpath. You'll need to become more familiar with the Java way of doing things if this sounds unintelligible. Or move on to the Python language implementation (Chapter 7), as Python lets you modify and reload modules on the fly in a command line environment.

If you cut and paste an existing scenario, and follow the pattern for including a one line description, and tweek it to follow the numbering schema (add a new case statement to the public method), then you'll be able to grow your Main class to include a large library of scenes of your own devising.

For example, the script at left, pasted into Main.java as a scenario, generates the Povray graphic at right (a different command line option would create it in VRML):

 

 Sphere oSph = new Sphere();
 Icosahedron oIco = new Icosahedron();

 oIco.addmesh(6); // = 6-frequency
 oIco.setcolor("Magenta");
 oIco.edgesvisible=false;
 oIco.setdim(0.04,0.08);
 oIco.setcolor("Magenta","Blue");
 oIco.oMesh.curvemesh(); //geodesic

 oSph.scale(1.8); // internal sphere

 POVwriter.camfactor = 9;
 writeshape(oIco);
 writeshape(oSph);
 writedone();   
image
The classes are Java 1.1 compatible, will not all run under Java 1.0 (but should in Java 1.2 and above). To run the program, unzip files in a directory and enter:

jre -cp .;polys.jar Main

or, if you have the JDK (Java Development Kit) installed instead of the JRE (Java Runtime Environment), try:

java -cp .;polys.jar Main

This will list out some help explaining what additional parameters will generate VRML or Povray files, suitable for viewing and/or rendering. Of course you'll need to download a VRML viewer and Povray separately, if you haven't already.

Yet more challenging than recoding Main.java would be to build some kind of GUI (graphical user interface) importing from polys.jar and invoking its methods behind the scenes. This is where I'm headed, to gain more experience with the new Swing classes (I have two Java 1.0 applets at Synergetics on the Web due for a rewrite).

I've included all the Java source code for the polys package inside the jar file, along with the class files, for those of you who want to poke around in the internals, e.g. maybe add a new polyhedron or two by extending the abstract Shape class. Check readme.txt for more information (no warranties expressed or implied, blah blah blah).

You'll need the jar utility to unpack and repack these files, but as a Java programmer you should have this utility anyway, which is now a part of the standard JDK.

 

On-line Resources:

Return to Symbols and Operators


oregon.gif - 8.3 K
Oregon Curriculum Network