Example 23.14: The Cube class

The Cube class needs to add fields that match the fields in the VRML cube node, that is height, width and depth. There are three polymorphic constructors, one that takes no arguments and just uses the defaults, one that takes three doubles for height, width and depth but uses default location, and one that lets the user specify both location and dimension. Methods are also provided to set these fields. Finally draw must be implemented. Example 23.14 has the code.

package elharo.vrml;

public class Cube extends shape {

  double width = 1.0;
  double height = 1.0;
  double depth = 1.0;
  
  public Cube() {
  }
  
  public Cube(
   double height, double width, double depth) {
 
    this.width = width;
    this.height = height;
    this.depth = depth;  
  }
    
  public Cube(
   double height, double width, double depth,
   double x, double y, double z) {
 
    this.width = width;
    this.height = height;
    this.depth = depth; 
    this.x = x;
    this.y = y;
    this.z = z; 
  }

  public void sizeTo(
    double width, 
    double height, 
    double depth) {
 
    this.width = width;
    this.height = height;
    this.depth = depth;  
  }
  
  public void sizeRelative(
    double width, 
    double height, 
    double depth) {
 
    this.width += width;
    this.height += height;
    this.depth += depth;  
  }

  public String draw() {
  
    String node1 = "Translation { \n" +
      "translation " + x + " " + y + " " + 
        "z + \n}\n\n";
      
    String node2 = "Cube {\n" + 
      "  height " + height +
      "\n  width " + width + 
      "\n  depth " + depth + 
      "\n}\n\n";
  
    String node3 = "Translation { \n" +
      "  translation " + -x + " " + -y + " " + 
        -z + "\n" + "}\n\n"; 

  
    return node1 + node2 + node3;
    
  }

}

Copyright 1996 Elliotte Rusty Harold
elharo@sunsite.unc.edu
This Chapter
Examples
Home