import java.awt.*; import javax.swing.*; import javax.media.j3d.*; import javax.vecmath.*; import com.sun.j3d.utils.universe.*; import com.sun.j3d.utils.geometry.Box; import com.sun.j3d.utils.behaviors.mouse.*; public class BasicBox extends JFrame { public static void main (String[] argv) { int width = 400; createFrame(0, 0, width, Color.WHITE, 255); createFrame(width, 0, width, Color.WHITE, 128); createFrame(2*width, 0, width, Color.BLACK, 128); } public static void createFrame (int x, int y, int width, Color bgColor, int alpha) { BasicBox frame = new BasicBox("Box", bgColor); frame.setSize(250, 250); frame.addBox(0.50f, new Color(0, 128, 0, alpha), Color.WHITE); frame.addBox(0.25f, new Color(0, 0, 255, alpha), Color.WHITE); frame.addLight(new Vector3f(0f, 0f, -1f), Color.WHITE); frame.finish(); frame.setSize(width, width); frame.setLocation(x, y); frame.setVisible(true); frame.show(); } public BasicBox (String name, Color bgColor) { super(name); getContentPane().setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas3D = new Canvas3D(config); universe = new SimpleUniverse(canvas3D); universe.getViewingPlatform().setNominalViewingTransform(); View view = canvas3D.getView(); view.setBackClipDistance(1e6); view.setFrontClipDistance(0.01); view.setProjectionPolicy(javax.media.j3d.View.PERSPECTIVE_PROJECTION); getContentPane().add("Center", canvas3D); root = new BranchGroup(); Background bg = new Background(new Color3f(bgColor)); bg.setApplicationBounds(new BoundingSphere(new Point3d(), 1e6)); root.addChild(bg); trans = new TransformGroup(); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); root.addChild(trans); addBehavior(trans, new MouseRotate()); addBehavior(trans, new MouseTranslate()); addBehavior(trans, new MouseZoom()); } public void addBox (float w, Color faceColor, Color lineColor) { int transp = 255-faceColor.getAlpha(); Appearance app = createAppearance(transp, faceColor); trans.addChild(new Box(w, w, w, app)); trans.addChild(lineShape(w, lineColor)); } public static Shape3D lineShape (double w, Color color) { Appearance app = createLineAppearance(color, 2.0); int stripLengths[] = {5,5,5,5,5,5}; Point3d[][][] a = new Point3d[2][2][2]; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) a[i][j][k] = new Point3d(-w+2*i*w, -w+2*j*w, -w+2*k*w); LineStripArray l = new LineStripArray(30, GeometryArray.COORDINATES, stripLengths); set(l, 0, a[0][0][0]); set(l, 1, a[1][0][0]); set(l, 2, a[1][1][0]); set(l, 3, a[0][1][0]); set(l, 4, a[0][0][0]); set(l, 5, a[0][0][1]); set(l, 6, a[1][0][1]); set(l, 7, a[1][1][1]); set(l, 8, a[0][1][1]); set(l, 9, a[0][0][1]); set(l, 10,a[0][0][0]); set(l, 11,a[0][1][0]); set(l, 12,a[0][1][1]); set(l, 13,a[0][0][1]); set(l, 14,a[0][0][0]); set(l, 15,a[1][0][0]); set(l, 16,a[1][1][0]); set(l, 17,a[1][1][1]); set(l, 18,a[1][0][1]); set(l, 19,a[1][0][0]); set(l, 20,a[0][0][0]); set(l, 21,a[0][0][1]); set(l, 22,a[1][0][1]); set(l, 23,a[1][0][0]); set(l, 24,a[0][0][0]); set(l, 25,a[0][1][0]); set(l, 26,a[0][1][1]); set(l, 27,a[1][1][1]); set(l, 28,a[1][1][0]); set(l, 29,a[0][1][0]); return new Shape3D(l, app); } public static void set (LineStripArray l, int i, Point3d a) { l.setCoordinate(i, a); } public static Appearance createLineAppearance (Color color, double lineWidth) { Appearance app = new Appearance(); ColoringAttributes colorAttr = new ColoringAttributes(); colorAttr.setColor(new Color3f(color)); app.setColoringAttributes(colorAttr); PolygonAttributes polyAttr = new PolygonAttributes(); polyAttr.setCullFace(PolygonAttributes.CULL_NONE); app.setPolygonAttributes(polyAttr); LineAttributes lineAttr = new LineAttributes(); lineAttr.setLineWidth((float)lineWidth); lineAttr.setLineAntialiasingEnable(true); app.setLineAttributes(lineAttr); return app; } public static Appearance createAppearance (int transp, Color color) { Appearance app = new Appearance(); Material mat = new Material(); mat.setAmbientColor(new Color3f(color)); mat.setDiffuseColor(new Color3f(color)); mat.setEmissiveColor(new Color3f(color)); mat.setSpecularColor(new Color3f(color)); mat.setShininess(50); app.setMaterial(mat); if (transp > 0) { float val = Math.max(0.0f, Math.min(1.0f, (transp+1e-6f)/255f)); TransparencyAttributes ta = new TransparencyAttributes(); ta.setTransparencyMode(TransparencyAttributes.NICEST); ta.setTransparency(val); app.setTransparencyAttributes(ta); } return app; } public void addBehavior (TransformGroup tr, MouseBehavior b) { b.setTransformGroup(tr); b.setSchedulingBounds(new BoundingSphere(new Point3d(), 1e6)); root.addChild(b); } public void addLight (Vector3f direction, Color color) { DirectionalLight light = new DirectionalLight(new Color3f(color), direction); light.setInfluencingBounds(new BoundingSphere(new Point3d(), 1e6)); root.addChild(light); } public void finish () { universe.addBranchGraph(root); universe.getViewingPlatform().setNominalViewingTransform(); } public static Transform3D initialLocation () { Matrix3d mx = new Matrix3d(); mx.rotX(-Math.PI/3); Matrix3d mz = new Matrix3d(); mz.rotZ(-Math.PI/6); Matrix3d mt = new Matrix3d(); mt.mul(mx, mz); Transform3D tr = new Transform3D(); tr.setRotation(mt); return tr; } private SimpleUniverse universe; private BranchGroup root; private TransformGroup trans; }