// Java 3D Test Applet // SwitchValueInterpolatorTest.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.util.*; 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.Box; import com.sun.j3d.utils.behaviors.picking.*; public class SwitchValueInterpolatorTest extends Applet { private Canvas3D canvas = null; private AlphaPanel apanel = null; private SwitchValueInterpolator sinterp = null; private boolean isStandalone = false; public SwitchValueInterpolatorTest() { 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("FirstChildIndex") ); TextField ffield = new TextField("0"); ffield.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { int i = 0; try { i = Integer.parseInt(e.getActionCommand()); if (i < 0) i = 0; sinterp.setFirstChildIndex(i); } catch (NumberFormatException ex) {} } }); spanel.add(ffield); spanel.add( new Label("LastChildIndex") ); TextField lfield = new TextField("5"); lfield.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { int i = 0; try { i = Integer.parseInt(e.getActionCommand()); if (i < 0) i = 0; sinterp.setLastChildIndex(i); } catch (NumberFormatException ex) {} } }); spanel.add(lfield); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); 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 ); 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); 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); Transform3D t3d = new Transform3D(); TransformGroup trans = new TransformGroup(t3d); root.addChild(trans); Switch switchGroup = new Switch(); switchGroup.setCapability(Switch.ALLOW_SWITCH_READ); switchGroup.setCapability(Switch.ALLOW_SWITCH_WRITE); trans.addChild(switchGroup); switchGroup.addChild( createBox( 0.1f, new Vector3d(-0.8, 0.0, 0.0), new Color3f(1.0f, 0.0f, 0.0f) ) ); // red switchGroup.addChild( createBox( 0.1f, new Vector3d(-0.4, 0.0, 0.0), new Color3f(1.0f, 0.5f, 0.0f) ) ); // orange switchGroup.addChild( createBox( 0.1f, new Vector3d(0.0, 0.0, 0.0), new Color3f(1.0f, 1.0f, 0.0f) ) ); // yellow switchGroup.addChild( createBox( 0.1f, new Vector3d(0.4, 0.0, 0.0), new Color3f(0.5f, 1.0f, 0.0f) ) ); // yellow green switchGroup.addChild( createBox( 0.1f, new Vector3d(0.8, 0.0, 0.0), new Color3f(0.0f, 1.0f, 0.5f) ) ); // blue green sinterp = new SwitchValueInterpolator( apanel.getAlpha(), switchGroup, 0, 5 ); sinterp.setSchedulingBounds(bounds); root.addChild(sinterp); return root; } private TransformGroup createBox(float size, Vector3d pos, Color3f color) { Transform3D t3d = new Transform3D(); t3d.set(pos); TransformGroup trans = new TransformGroup(t3d); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); trans.setCapability(TransformGroup.ENABLE_PICK_REPORTING); Appearance ap = new Appearance(); Material mat = new Material(); mat.setDiffuseColor(color); mat.setShininess(100.0f); ap.setMaterial(mat); Box box = new Box( size, size, size, Primitive.GENERATE_NORMALS | Primitive.ENABLE_GEOMETRY_PICKING, ap ); trans.addChild(box); return trans; } public static void main(String[] args) { SwitchValueInterpolatorTest applet = new SwitchValueInterpolatorTest(); Frame frame = new MainFrame(applet, 500, 500); } }