Skip to main content

(Cu)Enhanced form of ColorCube class that was provided with 3D api tutorial

No replies
irfansattar
Offline
Joined: 2008-12-22
Points: 0

/*
* Cube.java December 19,2008 11:00
*This software class is improved form of ColorCube class,implemented using
*IndexedQuadArray data Structure, distributed with java j3d tutorial material.
*It avails more flexible methods and constructers. Anyone can use, modify
*and distribute it for any purpose without any damage warrantee caused by
*Cube.java.
* Implemented By Irfan Sattar Pucit Lahore Pakistan.
* FOR COMMENTS
* irfan.sattar.mit@gmail.com
*/

import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.color.*;
import java.util.*;
import java.awt.*;
public class Cube extends Shape3D{
private Geometry geo;
private Appearance ap;
private IndexedQuadArray vertex;
private static final int FACEMUL = 4;
private static final int FRONT_FACE=0;
private static final int BACK_FACE=1;
private static final int TOP_FACE=2;
private static final int BOTTUM_FACE=3;
private static final int LEFT_FACE=4;
private static final int RIGHT_FACE=5;
//default Construture..
public Cube()
{
ap=new Appearance();
// Default Appearance
PolygonAttributes poly = new PolygonAttributes();
poly.setPolygonMode(PolygonAttributes.POLYGON_FILL);
poly.setCullFace(PolygonAttributes.CULL_NONE);
ap.setPolygonAttributes(poly);
this.creatCube(0f,0f,0f,.5f,new Color3f(Color.red));
geo=vertex;
this.setAppearance(ap);
this.setGeometry(geo);
}
//Creat Cube at location 0f,0f,of of size '.5f'.The whole cube will be
//red.
public Cube(float len)
{
ap=new Appearance();
// Default Appearance
PolygonAttributes poly = new PolygonAttributes();
poly.setPolygonMode(PolygonAttributes.POLYGON_FILL);
poly.setCullFace(PolygonAttributes.CULL_NONE);
ap.setPolygonAttributes(poly);
this.creatCube(0f,0f,0f,len,new Color3f(Color.red));
geo=vertex;
this.setAppearance(ap);
this.setGeometry(geo);
}
//Creat Cube at location x,y,z of size len.
public Cube(float x,float y,float z, float len)
{
ap=new Appearance();
// Default Appearance
PolygonAttributes poly = new PolygonAttributes();
poly.setPolygonMode(PolygonAttributes.POLYGON_FILL);
poly.setCullFace(PolygonAttributes.CULL_NONE);
ap.setPolygonAttributes(poly);
this.creatCube(x,y,z,len,new Color3f(Color.red));
geo=vertex;
this.setAppearance(ap);
this.setGeometry(geo);
}
//Creat Cube at location x,y,z of size 'len'.The whole cube will have color
//entered in arguments of 'col'.
public Cube(float x, float y, float z, float len, Color3f col)
{
creatCube(x,y,z,len,col);
geo=vertex;
this.setGeometry(geo);
ap=new Appearance();
// Default Appearance
PolygonAttributes poly = new PolygonAttributes();
poly.setPolygonMode(PolygonAttributes.POLYGON_FILL);
poly.setCullFace(PolygonAttributes.CULL_NONE);
ap.setPolygonAttributes(poly);
this.setAppearance(ap);
}
private void creatCube(float x, float y, float z, float len, Color3f col)
{
vertex = new IndexedQuadArray(8, IndexedQuadArray.COORDINATES |
IndexedQuadArray.COLOR_3, 24);
//cube vertex definition...
vertex.setCoordinate(0, new Point3f(x,y,z));
vertex.setCoordinate(1, new Point3f(x-len,y,z));
vertex.setCoordinate(2, new Point3f(x,y-len,z));
vertex.setCoordinate(3, new Point3f(x-len,y-len,z));
vertex.setCoordinate(4, new Point3f(x,y,z-len));
vertex.setCoordinate(5, new Point3f(x-len,y,z-len));
vertex.setCoordinate(6, new Point3f(x,y-len,z-len));
vertex.setCoordinate(7, new Point3f(x-len,y-len,z-len));
//setting color work
vertex.setColor(7, col);
//setting coordinate indices..
//front face
vertex.setCoordinateIndex(0,0);
vertex.setCoordinateIndex(1,1);
vertex.setCoordinateIndex(2,3);
vertex.setCoordinateIndex(3,2);
//Back face
vertex.setCoordinateIndex(4,7);
vertex.setCoordinateIndex(5,6);
vertex.setCoordinateIndex(7,5);
vertex.setCoordinateIndex(6,4);
//top face
vertex.setCoordinateIndex(8,3);
vertex.setCoordinateIndex(9,2);
vertex.setCoordinateIndex(11,7);
vertex.setCoordinateIndex(10,6);
//Bottum face
vertex.setCoordinateIndex(12,1);
vertex.setCoordinateIndex(13,0);
vertex.setCoordinateIndex(15,5);
vertex.setCoordinateIndex(14,4);
//Left face
vertex.setCoordinateIndex(16,6);
vertex.setCoordinateIndex(17,2);
vertex.setCoordinateIndex(19,4);
vertex.setCoordinateIndex(18,0);
//Right face
vertex.setCoordinateIndex(20,7);
vertex.setCoordinateIndex(21,3);
vertex.setCoordinateIndex(23,5);
vertex.setCoordinateIndex(22,1);
//Setting color indices..same
for(int i=0; i<24; i++)
vertex.setColorIndex(i, 7);
}
//Sets single color for various color points in cube.
public void setColor( Color3f col)
{
vertex.setColor(6, col);
}
//Sets color of any vertex 0-23 of color setted by setColor.
public void setVertexColor(int index)
{
vertex.setColorIndex(index, 6);
}
//Sets specific cube face color.FRONT_FACE,BACK_FACE,TOP_FACE,BOTTUM_FACE
//LEFT_FACE,RIGHT_FACE int constants defined can be used or enter (0-5)
public void setFaceColor(int face, Color3f col)
{
Color3f color=new Color3f(col);
vertex.setColor(face, color);
for(int i=0; i < 4; i++)
vertex.setColorIndex(FACEMUL*face+i, face);
}

}