// Java 3D Test Applet // ScaleInterpolatorTest.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 ScaleInterpolatorTest extends Applet { private AlphaPanel apanel = null; private ScaleInterpolator sinterp = null; public ScaleInterpolatorTest() { this.setLayout(new BorderLayout()); apanel = new AlphaPanel(); this.add(apanel, BorderLayout.NORTH); Panel spanel = new Panel(); this.add(spanel, 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")) { sinterp.setEnable(true); apanel.reset(); sbutton.setLabel("Stop"); } else if (blabel.equals("Stop")) { sinterp.setEnable(false); sbutton.setLabel("Start"); } } }); spanel.add(sbutton); spanel.add( new Label("Mimimum") ); TextField minfield = new TextField("0.1"); minfield.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { float s = 0.0f; try { s = Float.parseFloat(e.getActionCommand()); sinterp.setMinimumScale(s); } catch (NumberFormatException ex) {} } }); spanel.add(minfield); spanel.add( new Label("Maximum") ); TextField maxfield = new TextField("1.0"); maxfield.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { float s = 0.0f; try { s = Float.parseFloat(e.getActionCommand()); sinterp.setMaximumScale(s); } catch (NumberFormatException ex) {} } }); spanel.add(maxfield); 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); 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); 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 / 3, GeometryArray.COORDINATES); geom.setCoordinates(0, vertices); Shape3D grid = new Shape3D(geom); trans.addChild(grid); TransformGroup strans = new TransformGroup(); strans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); strans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); strans.setCapability(TransformGroup.ENABLE_PICK_REPORTING); trans.addChild(strans); 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 ); strans.addChild(shape); sinterp = new ScaleInterpolator(apanel.getAlpha(), strans); sinterp.setSchedulingBounds(bounds); root.addChild(sinterp); return root; } private Appearance createAppearance() { Appearance app = new Appearance(); app.setCapability(Appearance.ALLOW_MATERIAL_READ); app.setCapability(Appearance.ALLOW_MATERIAL_WRITE); //app.setPointAttributes( new PointAttributes(4.0f, false) ); //app.setPolygonAttributes( // new PolygonAttributes( PolygonAttributes.POLYGON_LINE, // PolygonAttributes.CULL_BACK, // 0.0f, false ) ); Material mat = new Material(); //mat.setCapability(Material.ALLOW_COMPONENT_READ); //mat.setCapability(Material.ALLOW_COMPONENT_WRITE); 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) { ScaleInterpolatorTest applet = new ScaleInterpolatorTest(); Frame frame = new MainFrame(applet, 500, 500); } }