// Java 3D Test Applet // TexGenTest.java // Copyright (c) 1999 ENDO Yasuyuki // mailto:yasuyuki@javaopen.org // http://www.javaopen.org/j3dbook/index.html import java.applet.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.text.*; 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.Cone; import com.sun.j3d.utils.image.TextureLoader; import com.sun.j3d.utils.behaviors.mouse.MouseRotate; import com.sun.j3d.utils.behaviors.mouse.MouseTranslate; import com.sun.j3d.utils.behaviors.mouse.MouseZoom; import com.sun.j3d.utils.image.TextureLoader; public class TexGenTest extends Applet { private boolean isStandalone = false; public TexGenTest() { this(false); } public TexGenTest(boolean isStandalone) { this.isStandalone = isStandalone; this.setLayout(new BorderLayout()); } public void init() { GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas = new Canvas3D(config); this.add(canvas, BorderLayout.CENTER); SimpleUniverse universe = new SimpleUniverse(canvas); universe.getViewingPlatform().setNominalViewingTransform(); BranchGroup scene = createSceneGraph(); scene.compile(); universe.addBranchGraph(scene); } private BranchGroup createSceneGraph() { BranchGroup root = new BranchGroup(); root.setCapability(BranchGroup.ALLOW_DETACH); Bounds bounds = new BoundingSphere(new Point3d(), 100.0); Light light = new DirectionalLight(); light.setInfluencingBounds(bounds); root.addChild(light); Background bg = new Background(new Color3f(0.5f, 0.5f, 0.5f)); bg.setApplicationBounds(bounds); root.addChild(bg); TransformGroup trans = new TransformGroup(); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); MouseRotate rotator = new MouseRotate(trans); rotator.setSchedulingBounds(bounds); root.addChild(rotator); MouseTranslate translator = new MouseTranslate(trans); translator.setSchedulingBounds(bounds); root.addChild(translator); MouseZoom zoomer = new MouseZoom(trans); zoomer.setSchedulingBounds(bounds); root.addChild(zoomer); Appearance ap = createAppearance(); Cone cone = new Cone( 0.5f, 1.0f, Primitive.GENERATE_NORMALS, ap ); trans.addChild(cone); root.addChild(trans); return root; } private Appearance createAppearance() { Appearance app = new Appearance(); Image image = null; if (this.isStandalone) { // アプリケーションとして実行されている final Toolkit toolkit = Toolkit.getDefaultToolkit(); image = toolkit.getImage("fisheye.jpg"); } else { // アプレットとして実行されている image = getImage(getCodeBase(), "fisheye.jpg"); } MediaTracker mt = new MediaTracker(this); mt.addImage(image, 0); mt.checkAll(true); try { mt.waitForID(0); } catch (InterruptedException e) { e.printStackTrace(); } Texture2D texture2d = (Texture2D)new TextureLoader(image, this).getTexture(); app.setTexture(texture2d); TexCoordGeneration texgen = new TexCoordGeneration( TexCoordGeneration.SPHERE_MAP, TexCoordGeneration.TEXTURE_COORDINATE_2 ); app.setTexCoordGeneration(texgen); return app; } public static void main(String[] args) { TexGenTest applet = new TexGenTest(true); // isStandalone = true; Frame frame = new MainFrame(applet, 500, 500); } }