// Java 3Dテスト用アプレット // AppearanceTest.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; public class AppearanceTest extends Applet { private Color3f color = new Color3f(1.0f, 1.0f, 1.0f); private ColoringAttributes cattr = null; public AppearanceTest() { Panel panel = new Panel(); this.setLayout(new BorderLayout()); this.add(panel, BorderLayout.NORTH); TuplePanel tp = new TuplePanel(color); tp.addTupleEventListener( new TupleEventListener() { public void tupleStateChanged(TupleEvent e) { cattr.setColor(e.getColor3f()); } }); panel.add(tp); Choice choice = new Choice(); choice.add("FASTEST"); choice.add("NICEST"); choice.add("SHADE_FLAT"); choice.add("SHADE_GOURAUD"); choice.select(3); choice.addItemListener(new ItemListener() { public void itemStateChanged( ItemEvent e ){ String item = (String)e.getItem(); if (item.equals("FASTEST")) { cattr.setShadeModel(ColoringAttributes.FASTEST); } else if (item.equals("NICEST")) { cattr.setShadeModel(ColoringAttributes.NICEST); } else if (item.equals("SHADE_FLAT")) { cattr.setShadeModel(ColoringAttributes.SHADE_FLAT); } else if (item.equals("SHADE_GOURAUD")) { cattr.setShadeModel(ColoringAttributes.SHADE_GOURAUD); } } }); panel.add(choice); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D 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(); Point3d[] triverts = new Point3d[6]; triverts[0] = new Point3d(-0.9, 0.4, 0.0); triverts[1] = new Point3d(-0.6, 0.0, 0.0); triverts[2] = new Point3d(-0.2, 0.7, 0.0); triverts[3] = new Point3d(0.1, 0.4, 0.0); triverts[4] = new Point3d(0.3, 0.0, 0.0); triverts[5] = new Point3d(0.6, 0.5, 0.0); TriangleArray tarray = new TriangleArray( triverts.length, GeometryArray.COORDINATES); tarray.setCoordinates(0, triverts); Appearance ap = createAppearance(); Shape3D triangles = new Shape3D(tarray, ap); root.addChild(triangles); Point3d[] stripverts = new Point3d[8]; stripverts[0] = new Point3d(-0.9, -0.6, 0.0); stripverts[1] = new Point3d(-0.5, -0.8, 0.0); stripverts[2] = new Point3d(-0.5, -0.4, 0.0); stripverts[3] = new Point3d(0.0, -0.6, 0.0); stripverts[4] = new Point3d(0.0, -0.2, 0.0); stripverts[5] = new Point3d(0.4, -0.3, 0.0); stripverts[6] = new Point3d(0.5, 0.1, 0.0); stripverts[7] = new Point3d(0.8, 0.0, 0.0); int[] vcounts = { stripverts.length }; TriangleStripArray sarray = new TriangleStripArray( stripverts.length, GeometryArray.COORDINATES | GeometryArray.COLOR_3, vcounts); sarray.setCoordinates(0, stripverts); Color3f[] colors = new Color3f[8]; colors[0] = new Color3f(1.0f, 1.0f, 1.0f); // white colors[1] = new Color3f(1.0f, 0.0f, 0.0f); // red colors[2] = new Color3f(0.0f, 1.0f, 1.0f); // cyan colors[3] = new Color3f(0.0f, 0.0f, 0.0f); // black colors[4] = new Color3f(1.0f, 1.0f, 0.0f); // yellow colors[5] = new Color3f(0.0f, 0.0f, 1.0f); // blue colors[6] = new Color3f(0.0f, 1.0f, 0.0f); // green colors[7] = new Color3f(1.0f, 0.0f, 1.0f); // magenta sarray.setColors(0, colors); Shape3D tstrip = new Shape3D(sarray, ap); root.addChild(tstrip); return root; } private Appearance createAppearance() { Appearance ap = new Appearance(); ap.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE); cattr = new ColoringAttributes(); cattr.setCapability(ColoringAttributes.ALLOW_COLOR_READ); cattr.setCapability(ColoringAttributes.ALLOW_COLOR_WRITE); cattr.setCapability(ColoringAttributes.ALLOW_SHADE_MODEL_WRITE); ap.setColoringAttributes(cattr); return ap; } public static void main(String[] args) { AppearanceTest applet = new AppearanceTest(); MainFrame frame = new MainFrame(applet, 500, 500); } }