< Zurück | Inhalt | Weiter >

8.2.1 Box












Box defines a simple six−sided cube, illustrated in figure 8.2. Unfortunately for Swing developers this name conflicts with the Swing class javax.swing.Box so if your application uses Swing you should reference the Java 3D class explicitly, as in the following:

com.sun.j3d.utils.geometry.Box box =

new com.sun.j3d.utils.geometry.Box( 0.5f, 0.5f, 0.5f, null);


Figure 8.2

The Box Primitive

Box contains the following Shape3Dobjects:

FRONT = 0;

BACK = 1;

RIGHT = 2;

LEFT = 3;

TOP = 4;


The identifiers are used as indices into the Primitive Group Node. In other words, to retrieve the FRONT

face of the cube, you must call:

Shape3D frontFace = box.getShape(Box.FRONT ); This is defined in Box.java as:

public Shape3D getShape(int partId)


if (partId < FRONT || partId > BOTTOM) return null;

return (Shape3D)((Group)getChild(0)).getChild(partId);


The faces of the Box are Shape3D objects and are children of the first Node added to the Group. The first Node of the Group is a TransformGroup object that allows the Box to be moved as a whole. Thus, modifying the scale, rotation, and translation of the TransformGroup’s Transform3D applies the scale, rotation, and translation to all of its child Nodes also.

The Sun example ConicWorld/BoxExample.java provides an example of creating Boxes and loading and applying texture images to Boxes.

//Set Appearance attributes

//first, create an appearance Appearance ap = new Appearance();

//create a colored material

Color3f aColor = new Color3f(0.1f, 0.1f, 0.1f); Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f); Color3f dColor = new Color3f(0.8f, 0.8f, 0.8f); Color3f sColor = new Color3f(1.0f, 1.0f, 1.0f);

Material m = new Material(aColor, eColor, dColor, sColor, 80.0f);

//enable lighting and assign material m.setLightingEnable(true); ap.setMaterial(m);

//render the Box as a wire frame

PolygonAttributes polyAttrbutes = new PolygonAttributes(); polyAttrbutes.setPolygonMode( PolygonAttributes.POLYGON_LINE ); polyAttrbutes.setCullFace(PolygonAttributes.CULL_NONE); ap.setPolygonAttributes(polyAttrbutes);

//create the box and assign the appearance

Box BoxObj = new Box(1.5f, 1.5f, 0.8f, Box.GENERATE_NORMALS | Box.GENERATE_TEXTURE_COORDS, ap);

//load and assign a texture image and set texture parameters TextureLoader tex = new TextureLoader("texture.jpg", "RGB", this); if (tex != null)


TextureAttributes texAttr = new TextureAttributes(); texAttr.setTextureMode(TextureAttributes.MODULATE); ap.setTextureAttributes(texAttr);