// Java 3D Test Applet // MaterialTest.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; public class MaterialTest extends Applet { private Material material = new Material(); private Scrollbar scrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 1, 1, 1, 129); private TextField textField = new TextField("1.0"); public MaterialTest() { this.setLayout(new BorderLayout()); Panel panel = new Panel(); panel.setLayout(new BorderLayout()); this.add(panel, BorderLayout.SOUTH); scrollbar.addAdjustmentListener( new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent e) { float value = (float)e.getValue(); material.setShininess(value); textField.setText( Float.toString(value) ); } }); panel.add(scrollbar, BorderLayout.CENTER); textField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { float value = 1.0f; try { value = Float.parseFloat(e.getActionCommand()); if (value < 1.0f) value = 1.0f; if (value > 128.0f) value = 128.0f; material.setShininess(value); scrollbar.setValue( (int)value ); System.out.println("value=" + value); } catch (NumberFormatException ex) {} } }); panel.add(textField, BorderLayout.WEST); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas = new Canvas3D(config); 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(); DirectionalLight light = new DirectionalLight( new Color3f(1.0f, 1.0f, 1.0f), new Vector3f(-0.5f, -0.5f, -0.7f) ); BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0); light.setInfluencingBounds(bounds); root.addChild(light); material.setCapability(Material.ALLOW_COMPONENT_WRITE); material.setDiffuseColor( new Color3f(0.0f, 0.0f, 0.8f) ); material.setShininess(1.0f); Appearance ap = new Appearance(); ap.setMaterial(material); Sphere sphere = new Sphere(0.6f, ap); root.addChild(sphere); return root; } public static void main(String[] args) { MaterialTest applet = new MaterialTest(); Frame frame = new MainFrame(applet, 500, 500); } }