// Java 3D Test Applet // PickMouseTest.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; import com.sun.j3d.utils.geometry.Primitive; import com.sun.j3d.utils.geometry.Sphere; import com.sun.j3d.utils.geometry.Cylinder; import com.sun.j3d.utils.geometry.Cone; import com.sun.j3d.utils.geometry.Box; import com.sun.j3d.utils.image.TextureLoader; import com.sun.j3d.utils.behaviors.picking.*; public class PickMouseTest extends Applet { private Canvas3D canvas = null; private boolean isStandalone = false; public PickMouseTest() { this(false); } public PickMouseTest(boolean isStandalone) { this.isStandalone = isStandalone; } public void init() { GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); 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(); BoundingSphere bounds = new BoundingSphere( new Point3d(), 100.0 ); PickRotateBehavior rotator = new PickRotateBehavior(root, canvas, bounds, PickObject.USE_GEOMETRY); root.addChild(rotator); PickTranslateBehavior translator = new PickTranslateBehavior(root, canvas, bounds, PickObject.USE_GEOMETRY); root.addChild(translator); PickZoomBehavior zoomer = new PickZoomBehavior(root, canvas, bounds, PickObject.USE_GEOMETRY); root.addChild(zoomer); TransformGroup trans = new TransformGroup(); root.addChild(trans); TransformGroup gtrans = new TransformGroup(); gtrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); gtrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); gtrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING); trans.addChild(gtrans); double[] vertices = { -0.8, -0.8, 0.0, -0.8, 0.8, 0.0, -0.4, -0.8, 0.0, -0.4, 0.8, 0.0, 0.0, -0.8, 0.0, 0.0, 0.8, 0.0, 0.4, -0.8, 0.0, 0.4, 0.8, 0.0, 0.8, -0.8, 0.0, 0.8, 0.8, 0.0, -0.8, -0.8, 0.0, 0.8, -0.8, 0.0, -0.8, -0.4, 0.0, 0.8, -0.4, 0.0, -0.8, 0.0, 0.0, 0.8, 0.0, 0.0, -0.8, 0.4, 0.0, 0.8, 0.4, 0.0, -0.8, 0.8, 0.0, 0.8, 0.8, 0.0 }; LineArray geom = new LineArray( vertices.length, GeometryArray.COORDINATES); geom.setCoordinates(0, vertices); geom.setCapability(Geometry.ALLOW_INTERSECT); Shape3D grid = new Shape3D(geom); grid.setCapability(Shape3D.ALLOW_GEOMETRY_READ); gtrans.addChild(grid); Appearance ap = createAppearance(); Transform3D bt3d = new Transform3D(); bt3d.set(new Vector3d(-0.4, 0.4, 0.0)); TransformGroup btrans = new TransformGroup(bt3d); btrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); btrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); btrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING); trans.addChild(btrans); Box box = new Box( 0.25f, 0.25f, 0.25f, Primitive.GENERATE_TEXTURE_COORDS | Primitive.ENABLE_GEOMETRY_PICKING, ap ); btrans.addChild(box); Transform3D cyt3d = new Transform3D(); cyt3d.set(new Vector3d(-0.4, -0.4, 0.0)); TransformGroup cytrans = new TransformGroup(cyt3d); cytrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); cytrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); cytrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING); trans.addChild(cytrans); Cylinder cylinder = new Cylinder( 0.3f, 0.6f, Primitive.GENERATE_TEXTURE_COORDS | Primitive.ENABLE_GEOMETRY_PICKING, ap ); cytrans.addChild(cylinder); Transform3D cnt3d = new Transform3D(); cnt3d.set(new Vector3d(0.4, 0.4, 0.0)); TransformGroup cntrans = new TransformGroup(cnt3d); cntrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); cntrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); cntrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING); Cone cone = new Cone( 0.3f, 0.6f, Primitive.GENERATE_TEXTURE_COORDS | Primitive.ENABLE_GEOMETRY_PICKING, ap); cntrans.addChild(cone); trans.addChild(cntrans); Transform3D st3d = new Transform3D(); st3d.set(new Vector3d(0.4, -0.4, 0.0)); TransformGroup strans = new TransformGroup(st3d); strans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); strans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); strans.setCapability(TransformGroup.ENABLE_PICK_REPORTING); Sphere sphere = new Sphere( 0.3f, Primitive.GENERATE_TEXTURE_COORDS | Primitive.ENABLE_GEOMETRY_PICKING, ap ); strans.addChild(sphere); trans.addChild(strans); return root; } private Appearance createAppearance() { Appearance app = new Appearance(); // Texture の生成 Image image = null; if (this.isStandalone) { // アプリケーションとして実行されている Toolkit toolkit = Toolkit.getDefaultToolkit(); image = toolkit.getImage("earth.jpg"); } else { // アプレットとして実行されている image = getImage(getCodeBase(), "earth.jpg"); } MediaTracker mt = new MediaTracker(this); mt.addImage(image, 0); mt.checkAll(true); try { mt.waitForID(0); } catch (InterruptedException e) { e.printStackTrace(); } Texture texture = new TextureLoader(image, this).getTexture(); app.setTexture(texture); return app; } public static void main(String[] args) { PickMouseTest applet = new PickMouseTest(true); // isStandalone = true; Frame frame = new MainFrame(applet, 500, 500); } }