// Java 3D Test Applet // TransparencyInterpolatorTest.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.behaviors.mouse.MouseRotate; import com.sun.j3d.utils.image.TextureLoader; public class TransparencyInterpolatorTest extends Applet { private boolean isStandalone = false; private AlphaPanel apanel = null; private TransparencyInterpolator tinterp = null; public TransparencyInterpolatorTest() { this(false); } public TransparencyInterpolatorTest(boolean isStandalone) { isStandalone = isStandalone; this.setLayout(new BorderLayout()); apanel = new AlphaPanel(); this.add(apanel, BorderLayout.NORTH); Panel tpanel = new Panel(); this.add(tpanel, BorderLayout.SOUTH); final Button sbutton = new Button("Stop"); sbutton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { String blabel = sbutton.getLabel(); if (blabel.equals("Start")) { tinterp.setEnable(true); apanel.reset(); sbutton.setLabel("Stop"); } else if (blabel.equals("Stop")) { tinterp.setEnable(false); sbutton.setLabel("Start"); } } }); tpanel.add(sbutton); tpanel.add( new Label("Mimimum") ); TextField minfield = new TextField("0.0"); minfield.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { float t = 0.0f; try { t = Float.parseFloat(e.getActionCommand()); if (t < 0.0f) t = 0.0f; if (t > 1.0f) t = 1.0f; tinterp.setMinimumTransparency(t); } catch (NumberFormatException ex) {} } }); tpanel.add(minfield); tpanel.add( new Label("Maximum") ); TextField maxfield = new TextField("1.0"); maxfield.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { float t = 0.0f; try { t = Float.parseFloat(e.getActionCommand()); if (t < 0.0f) t = 0.0f; if (t > 1.0f) t = 1.0f; tinterp.setMaximumTransparency(t); } catch (NumberFormatException ex) {} } }); tpanel.add(maxfield); } 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(); universe.addBranchGraph(createSceneGraph()); } private BranchGroup createSceneGraph() { BranchGroup root = new BranchGroup(); BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0); Background background = createBackground(bounds); root.addChild(background); DirectionalLight light = new DirectionalLight( new Color3f(1.0f, 1.0f, 1.0f), new Vector3f(-0.57f, -0.57f, -0.57f) ); light.setInfluencingBounds(bounds); root.addChild(light); AmbientLight alight = new AmbientLight(); alight.setInfluencingBounds(bounds); root.addChild(alight); TransformGroup trans = new TransformGroup(); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); trans.setCapability(TransformGroup.ENABLE_PICK_REPORTING); root.addChild(trans); MouseRotate rotator = new MouseRotate(); rotator.setSchedulingBounds(bounds); rotator.setTransformGroup(trans); root.addChild(rotator); Heart heart = new Heart(); Appearance app = createAppearance(); Shape3D shape = new Shape3D( heart.getGeometry(), app ); trans.addChild(shape); tinterp = new TransparencyInterpolator( apanel.getAlpha(), app.getTransparencyAttributes() ); tinterp.setSchedulingBounds(bounds); root.addChild(tinterp); return root; } private Background createBackground(Bounds bounds) { // Texture の生成 Image image = null; if (isStandalone) { // アプリケーションとして実行されている Toolkit toolkit = Toolkit.getDefaultToolkit(); image = toolkit.getImage("bg.jpg"); } else { // アプレットとして実行されている image = getImage(getCodeBase(), "bg.jpg"); } MediaTracker mt = new MediaTracker(this); mt.addImage(image, 0); mt.checkAll(true); try { mt.waitForID(0); } catch (InterruptedException e) { e.printStackTrace(); } TextureLoader loader = new TextureLoader(image, this); Background bg = new Background(loader.getImage()); bg.setCapability(Background.ALLOW_COLOR_READ); bg.setCapability(Background.ALLOW_COLOR_WRITE); bg.setCapability(Background.ALLOW_IMAGE_WRITE); bg.setApplicationBounds(bounds); return bg; } private Appearance createAppearance() { Appearance app = new Appearance(); app.setCapability(Appearance.ALLOW_MATERIAL_READ); app.setCapability(Appearance.ALLOW_MATERIAL_WRITE); Material mat = new Material(); mat.setDiffuseColor( new Color3f(1.0f, 0.0f, 0.0f) ); mat.setShininess(100.0f); TransparencyAttributes tattr = new TransparencyAttributes(TransparencyAttributes.FASTEST, 0.0f); tattr.setCapability(TransparencyAttributes.ALLOW_VALUE_READ); tattr.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE); app.setTransparencyAttributes(tattr); app.setMaterial(mat); return app; } public static void main(String[] args) { TransparencyInterpolatorTest applet = new TransparencyInterpolatorTest(); Frame frame = new MainFrame(applet, 500, 500); } }