import javax.media.j3d.*;
import javax.vecmath.*;

class Box {

	double x0,x1,y0,y1,z0,z1;

	Shape3D shape;

	BranchGroup BG;

	public Box(double x0, double x1, double y0, double y1, double z0, double z1, Color3f color) {
		this.x0 = x0;
		this.x1 = x1;
		this.y0 = y0;
		this.y1 = y1;
		this.z0 = z0;
		this.z1 = z1;

		BG = new BranchGroup();

		BG.setCapability(BranchGroup.ALLOW_DETACH);

		double [] x = new double[8];
		double [] y = new double[8];
		double [] z = new double[8];

		x[0] = x0;
		y[0] = y0;
		z[0] = z0;

		x[1] = x1;
		y[1] = y0;
		z[1] = z0;
		
		x[2] = x1;
		y[2] = y1;
		z[2] = z0;

		x[3] = x0;
		y[3] = y1;
		z[3] = z0;

		x[4] = x1;
		y[4] = y0;
		z[4] = z1;

		x[5] = x1;
		y[5] = y1;
		z[5] = z1;

		x[6] = x0;
		y[6] = y1;
		z[6] = z1;

		x[7] = x0;
		y[7] = y0;
		z[7] = z1;

		int index [] = {0,1,2,3,2,3,6,5,7,4,5,6,1,0,7,4,0,7,6,3,1,4,5,2};

		double [] qverts = new double[24*3];
		for(int i=0;i<24;i++) {
			int ind = index[i];	
			qverts[i*3] = x[ind];
			qverts[i*3+1] = y[ind];
			qverts[i*3+2] = z[ind];
		}

        QuadArray boxGeometry = new QuadArray( 24, QuadArray.COORDINATES);
 
        boxGeometry.setCapability(QuadArray.ALLOW_COORDINATE_WRITE);
        boxGeometry.setCapability(QuadArray.ALLOW_COORDINATE_READ);
		boxGeometry.setCapability(QuadArray.ALLOW_COLOR_WRITE);
		boxGeometry.setCapability(QuadArray.ALLOW_INTERSECT);
		boxGeometry.setCapability(QuadArray.ALLOW_COUNT_READ);
		boxGeometry.setCapability(QuadArray.ALLOW_FORMAT_READ);

        boxGeometry.setCoordinates( 0, qverts );

		Appearance boxAppearance = new Appearance();

		if(color != null) {

			Material mm = new Material();
			mm.setLightingEnable(true);
			mm.setEmissiveColor(color);
			boxAppearance.setMaterial(mm);
			boxAppearance.setCapability(Appearance.ALLOW_MATERIAL_READ);
			boxAppearance.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
			ColoringAttributes boxColoring = new ColoringAttributes();
			boxColoring.setColor(color);			
			boxAppearance.setColoringAttributes(boxColoring);
		} else {

		}


		// Make a wireframe
		PolygonAttributes pa = new PolygonAttributes();
		pa.setPolygonMode(pa.POLYGON_LINE);
		pa.setCullFace(pa.CULL_NONE); 
		
		boxAppearance.setPolygonAttributes(pa);

        shape = new Shape3D(boxGeometry, boxAppearance);

		BG.addChild(shape);

	}


	public BranchGroup getBranchGroup() {
		return BG;
	}

	public Shape3D getShape3D() {
		return shape;
	}


}