/** * @file Cube.java * @author R. Daniel Bergeron * @see Tetrahedron.java *
* modified Monday 09 November 1998 * modified by Robert S Laramee *
* description The cube constructor was modified so that it is passed a * reference to the new BSPTree data structure * Same goes for the makefaces() method */ import java.awt.*; import java.math.*; import java.util.*; //import matrix.*; class Cube extends Solid { private Face _faces[]; private Point4 _vtx[]; // vertices of the cube /** * The cube constructor has been modified so that it is passed a * reference to the BSPTree. * @param xform -transformation matrix * @param rgb -the color of cube * @param tree -the bsp tree */ public Cube ( Matrix4 xform, Color rgb, BSPTree tree ) { super( xform, rgb ); makeFaces(tree); } //-------- Cube ( Color rgb ) ------------- public Cube ( Color rgb, BSPTree tree ) { super( rgb ); makeFaces(tree); } //-------- Cube ( Cube ) ----------------------------------- public Cube ( Cube l ) { super( l ); } //---------------------- copy () ----------------- public Shape copy ( ) { return new Cube( this ); } /** * ---------------------- makeFaces ( ) * The makeFaces() method was modified. It gets a reference to the BSP * Tree list so each of the new faces can be added to the tree. * @param tree -a reference to the BSP Tree */ private void makeFaces (BSPTree tree) { Point4 temp[]={ new Point4( 0, 0, 0 ), // 0: left lower front new Point4( 0, 0,-1 ), // 1: left lower back new Point4( 0, 1, 0 ), // 2: left upper front new Point4( 0, 1,-1 ), // 3: left upper back new Point4( 1, 0, 0 ), // 4: right lower front new Point4( 1, 0,-1 ), // 5: right lower back new Point4( 1, 1, 0 ), // 6: right upper front new Point4( 1, 1,-1 ) // 7: right upper back }; _vtx = temp; _faces = new Face[6]; for (int i = 0; i < _vtx.length; i++) _vtx[i] = _xform.timesPoint( _vtx[i] ); Color cols[] = { _color, darken(_color, 0.6), darken(_color, 0.7), darken(_color, 0.8), darken(_color, 0.9), darken(_color, 0.5)}; _faces[0] = new Face(cols[0],_vtx[0],_vtx[4],_vtx[6],_vtx[2]); // front _faces[1] = new Face(cols[1],_vtx[1],_vtx[3],_vtx[7],_vtx[5]); // back _faces[2] = new Face(cols[2],_vtx[0],_vtx[2],_vtx[3],_vtx[1]); // left _faces[3] = new Face(cols[3],_vtx[4],_vtx[5],_vtx[7],_vtx[6]); // right _faces[4] = new Face(cols[4],_vtx[0],_vtx[1],_vtx[5],_vtx[4]); // bottm _faces[5] = new Face(cols[5],_vtx[2],_vtx[6],_vtx[7],_vtx[3]); // top /** add the faces to the BSP Tree */ for (int i = 0; i < _faces.length; i++) { tree.addFace(new String("cube_face["+i+"]"), _faces[i]); } } //---------------------- draw (Graphics, Matrix4) ----------------- public void draw ( Graphics g, Matrix4 view ) { for (int i = 0; i < _faces.length; i++ ) _faces[i].draw(g, view); } //---------------------- brighten (Color, double brightenRatio ) -------- // This uses the "brightenRatio" parameter as a % amount to brighten the // Color. So if you want the Color to be 110% of its current // brightness, pass in 1.1 as the brightenRatio parameter. // It is actually public Color brighten ( Color rgb, double brightenRatio ) { if ( brightenRatio < 0 ) brightenRatio = -brightenRatio; return new Color ( Math.min(255, (int)(rgb.getRed()*brightenRatio)), Math.min(255, (int)(rgb.getGreen()*brightenRatio)), Math.min(255, (int)(rgb.getBlue()*brightenRatio)) ); } //---------------------- darken (Color, double darkenRatio ) -------- // This uses the "darkenRatio" parameter as a % amount to darken the // Color. So if you want the Color to be 90% of its current // brightness, pass in .9 as the darkenRatio parameter. public Color darken ( Color rgb, double darkenRatio ) { if ( darkenRatio < 0 ) darkenRatio = -darkenRatio; if ( darkenRatio > 1 ) darkenRatio = 1; return new Color ( (int)(rgb.getRed()*darkenRatio), (int)(rgb.getGreen()*darkenRatio), (int)(rgb.getBlue()*darkenRatio) ); } }