// Java 3Dテスト用アプレット // Shape3DTest.java // Copyright (c) 1999 ENDO Yasuyuki // mailto:yasuyuki@javaopen.org // http://www.javaopen.org/j3dbook/index.html import java.applet.*; import java.awt.*; import javax.media.j3d.*; import javax.vecmath.*; import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.universe.SimpleUniverse; public class Shape3DTest extends Applet { public Shape3DTest() { GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas = new Canvas3D(config); this.setLayout(new BorderLayout()); this.add(canvas, BorderLayout.CENTER); SimpleUniverse universe = new SimpleUniverse(canvas); universe.getViewingPlatform().setNominalViewingTransform(); BranchGroup scene = createSceneGraph(); universe.addBranchGraph(scene); } private BranchGroup createSceneGraph() { BranchGroup root = new BranchGroup(); Point3d[] vertices = new Point3d[8]; vertices[0] = new Point3d(-0.5, 0.5, 0.5); vertices[1] = new Point3d(-0.5, -0.5, 0.5); vertices[2] = new Point3d(0.5, -0.5, 0.5); vertices[3] = new Point3d(0.5, 0.5, 0.5); vertices[4] = new Point3d(-0.5, 0.5, -0.5); vertices[5] = new Point3d(-0.5, -0.5, -0.5); vertices[6] = new Point3d(0.5, -0.5, -0.5); vertices[7] = new Point3d(0.5, 0.5, -0.5); int[] indices = { 0, 1, 2, 3, 4, 5, 1, 0, 4, 0, 3, 7, 3, 2, 6, 7, 7, 6, 5, 4, 1, 5, 6, 2 }; Color3f[] colors = { new Color3f(1.0f, 0.0f, 0.0f), // red new Color3f(0.0f, 1.0f, 0.0f), // green new Color3f(0.0f, 0.0f, 1.0f), // blue new Color3f(0.0f, 1.0f, 1.0f), // cyan new Color3f(1.0f, 0.0f, 1.0f), // magenta new Color3f(1.0f, 1.0f, 0.0f) }; // yellow int[] colorIndices = { 0, 0, 0, 0, // red 1, 1, 1, 1, // green 2, 2, 2, 2, // blue 3, 3, 3, 3, // cyan 4, 4, 4, 4, // magenta 5, 5, 5, 5 }; // yellow IndexedQuadArray geometry = new IndexedQuadArray( vertices.length, GeometryArray.COORDINATES | GeometryArray.COLOR_3, indices.length); geometry.setCoordinates(0, vertices); geometry.setCoordinateIndices(0, indices); geometry.setColors(0, colors); geometry.setColorIndices(0, colorIndices); Shape3D shape = new Shape3D(geometry); Transform3D t3d = new Transform3D(); t3d.setRotation( new AxisAngle4d(0.57, 0.57, -0.57, Math.PI / 4.0)); TransformGroup trans = new TransformGroup(t3d); trans.addChild(shape); root.addChild(trans); return root; } public static void main(String[] args) { Shape3DTest applet = new Shape3DTest(); MainFrame frame = new MainFrame(applet, 500, 500); } }