The Cube Class
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
Wyszukiwarka
Podobne podstrony:
14 00 23 4dikk53bthsvvyrmc72wtpmjdtkgljd7yfr6uaqJunkers ZW 14 2 DV AE 23RKdI TRiL s I sem 5 wykład 23, 30 XI, 7, 14, 21 XII 2011Komunikat FOR 23 10 14 Nowa ustawa przeciwko prywatnym inwestycjom w polski lupekwięcej podobnych podstron