// Java 3D Test Applet // EarthApplet.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 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.Sphere; 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; public class RasterTest extends Applet { private Canvas3D canvas = null; private TransparencyAttributes tattr = null; private boolean isStandalone = false; public RasterTest() { this(false); } public RasterTest(boolean isStandalone) { this.isStandalone = isStandalone; this.setLayout(new BorderLayout()); Panel panel = new Panel(); this.add(panel, BorderLayout.SOUTH); Choice choice = new Choice(); choice.add("BLENDED"); choice.add("FASTEST"); choice.add("NICEST"); choice.add("NONE"); choice.add("SCREEN_DOOR"); choice.select(3); // NONE choice.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { String state = (String)e.getItem(); if (state.equals("BLENDED")) { tattr.setTransparencyMode(TransparencyAttributes.BLENDED); } else if (state.equals("FASTEST")) { tattr.setTransparencyMode(TransparencyAttributes.FASTEST); } else if (state.equals("NICEST")) { tattr.setTransparencyMode(TransparencyAttributes.NICEST); } else if (state.equals("NONE")) { tattr.setTransparencyMode(TransparencyAttributes.NONE); } else if (state.equals("SCREEN_DOOR")) { tattr.setTransparencyMode(TransparencyAttributes.SCREEN_DOOR); } } }); panel.add(choice); panel.add( new Label("Transparency:") ); TextField field = new TextField("0.0"); field.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { float v = 0.0f; try { v = Float.parseFloat(e.getActionCommand()); tattr.setTransparency(v); } catch (Exception ex) {} } }); panel.add(field); } public void init() { GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); this.canvas = new Canvas3D(config); this.add(this.canvas, BorderLayout.CENTER); SimpleUniverse universe = new SimpleUniverse(canvas); universe.getViewingPlatform().setNominalViewingTransform(); universe.addBranchGraph(createSceneGraph()); } private BranchGroup createSceneGraph() { BranchGroup root = new BranchGroup(); TransformGroup trans = new TransformGroup(); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); trans.setCapability(TransformGroup.ENABLE_PICK_REPORTING); BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0); 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); ImageComponent2D image = readImage(); Raster raster = new Raster( new Point3f(0.0f, 0.0f, 0.0f), Raster.RASTER_COLOR, 0, 0, 128, 128, image, null ); Appearance app = createAppearance(); Shape3D shape = new Shape3D(raster, app); shape.setCapability(Shape3D.ENABLE_PICK_REPORTING); shape.setBounds(new BoundingSphere(new Point3d(), 0.5)); trans.addChild(shape); root.addChild(trans); return root; } private ImageComponent2D readImage() { Image image = null; if (this.isStandalone) { // アプリケーションとして実行されている Toolkit toolkit = Toolkit.getDefaultToolkit(); image = toolkit.getImage("face.gif"); } else { // アプレットとして実行されている image = getImage(getCodeBase(), "face.gif"); } MediaTracker mt = new MediaTracker(this); mt.addImage(image, 0); mt.checkAll(true); try { mt.waitForID(0); } catch (InterruptedException e) { e.printStackTrace(); } TextureLoader tr = new TextureLoader(image, this); return tr.getImage(); } private Appearance createAppearance() { Appearance app = new Appearance(); tattr = new TransparencyAttributes(TransparencyAttributes.NONE, 0.0f); tattr.setCapability(TransparencyAttributes.ALLOW_MODE_READ); tattr.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE); tattr.setCapability(TransparencyAttributes.ALLOW_VALUE_READ); tattr.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE); app.setTransparencyAttributes(tattr); return app; } public static void main(String[] args) { RasterTest applet = new RasterTest(true); // isStandalone = true; Frame frame = new MainFrame(applet, 500, 500); } }