/*--------------*/ /* Ex73.java */ /*--------------*/ /*-----------------------------------------*/ /* ラジオボタンの設定は本書では説明省略 */ /*-----------------------------------------*/ import java.applet.Applet; import java.awt.*; //BorderLayout,GraphicsConfiguration,event import javax.media.j3d.*; //Java3d import com.sun.j3d.utils.applet.MainFrame; //MainFrame import com.sun.j3d.utils.universe.*; //SimpleUniverse import com.sun.j3d.utils.geometry.*; //Primitive import com.sun.j3d.utils.behaviors.mouse.*; //Mouse import com.sun.j3d.utils.image.TextureLoader; //TextureLoader import javax.vecmath.*; //Point3f,Color3f import java.awt.event.*; //Checkbox public class Ex73 extends Applet implements ItemListener{ private SimpleUniverse simpleU; private BoundingSphere bounds = new BoundingSphere(new Point3d(), 10.0); private Appearance ap; private Texture texture1, texture2; private Cone cone; private Shape3D quad3D; //ラジオボタン------- private CheckboxGroup radio1; //モード private Checkbox radio11, radio12, radio13; private CheckboxGroup radio2; //テクスチャ private Checkbox radio21, radio22; private CheckboxGroup radio3; //物体 private Checkbox radio31, radio32; private BranchGroup branchG1; private BranchGroup branchG11, branchG12; public void init() { //アプレットの中央にCanvas3Dを追加 setLayout(new BorderLayout()); Canvas3D canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration()); add(canvas, BorderLayout.CENTER); //シーングラフの生成 simpleU = new SimpleUniverse(canvas); simpleU.getViewingPlatform().setNominalViewingTransform(); //シーングラフの作成 simpleU.addBranchGraph(createSceneGraph()); //GUIの作成 createGUI(); } public BranchGroup createSceneGraph() { BranchGroup branchG0 = new BranchGroup(); branchG1 = new BranchGroup(); branchG11 = new BranchGroup(); branchG12 = new BranchGroup(); //背景色の設定(設定が無い場合は黒) Background background = new Background(new Color3f(0.3f, 0.3f, 0.3f)); background.setApplicationBounds(bounds); branchG0.addChild(background); //マウス操作用のノード TransformGroup transG0 = new TransformGroup(); branchG0.addChild(transG0); transG0.addChild(branchG1); //Appearanceを作成 ap = new Appearance(); ColoringAttributes ca = new ColoringAttributes(); ca.setColor(new Color3f( 0.0f, 0.0f, 1.0f)); //青 ap.setColoringAttributes(ca); PolygonAttributes pa = new PolygonAttributes(); pa.setCullFace(PolygonAttributes.CULL_NONE); //裏面も表示する ap.setPolygonAttributes(pa); //テクスチャ java.net.URL texFile = null; TextureLoader loader; //--ひし形ファイル try{ texFile = new java.net.URL(getCodeBase().toString() + "diamond128.gif"); }catch(java.net.MalformedURLException ex){ System.out.println(ex.getMessage()); } loader = new TextureLoader(texFile, this); texture1 = loader.getTexture(); //--java3dファイル try{ texFile = new java.net.URL(getCodeBase().toString() + "java3d.jpg"); }catch(java.net.MalformedURLException ex){ System.out.println(ex.getMessage()); } loader = new TextureLoader(texFile, this); texture2 = loader.getTexture(); //テクスチャ座標の自動生成 TexCoordGeneration texGen = new TexCoordGeneration(TexCoordGeneration.OBJECT_LINEAR, // new TexCoordGeneration(TexCoordGeneration.EYE_LINEAR, // new TexCoordGeneration(TexCoordGeneration.SPHERE_MAP, TexCoordGeneration.TEXTURE_COORDINATE_2, new Vector4f(2.0f, 0.0f, 0.0f, 0.0f), new Vector4f(0.0f, 2.0f, 0.0f, 0.0f)); ap.setTexCoordGeneration(texGen); ap.setTexture(texture1); //円錐の生成 cone = new Cone(0.5f, 1.0f, ap); branchG11.addChild(cone); branchG1.addChild(branchG11); //四角形ポリゴンの頂点座標設定 Point3f[] vertex = new Point3f[4]; vertex[0] = new Point3f( 0.5f, 0.5f, 0.0f); vertex[1] = new Point3f( -0.5f, 0.5f, 0.0f); vertex[2] = new Point3f( -0.5f, -0.5f, 0.0f); vertex[3] = new Point3f( 0.5f, -0.5f, 0.0f); //四角形ポリゴンの生成 QuadArray quadA = new QuadArray(vertex.length, GeometryArray.COORDINATES); quadA.setCoordinates(0, vertex); quad3D = new Shape3D(quadA, ap); branchG12.addChild(quad3D); //Capability設定 ap.setCapability(Appearance.ALLOW_TEXGEN_WRITE); ap.setCapability(Appearance.ALLOW_TEXTURE_WRITE); branchG1.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE); branchG1.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND); branchG1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); branchG11.setCapability(BranchGroup.ALLOW_DETACH); branchG12.setCapability(BranchGroup.ALLOW_DETACH); //マウスによる物体移動 transG0.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); transG0.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); MouseRotate mouseRot = new MouseRotate(transG0); mouseRot.setSchedulingBounds(bounds); transG0.addChild(mouseRot); branchG0.compile(); return branchG0; } //GUIの作成 private void createGUI(){ Panel panel0 = new Panel(new GridLayout(3,1)); //----- texGen radio1 = new CheckboxGroup(); radio11 = new Checkbox("OBJECT_LINEAR", true, radio1); radio12 = new Checkbox("EYE_LINEAR", false, radio1); radio13 = new Checkbox("SPHERE_MAP", false, radio1); Panel panel1 = new Panel(); panel1.add(radio11); panel1.add(radio12); panel1.add(radio13); radio11.addItemListener(this); radio12.addItemListener(this); radio13.addItemListener(this); panel0.add(panel1); //----- file radio2 = new CheckboxGroup(); radio21 = new Checkbox("ひし形", true, radio2); radio22 = new Checkbox("java3d", false, radio2); Panel panel2 = new Panel(); panel2.add(radio21); panel2.add(radio22); radio21.addItemListener(this); radio22.addItemListener(this); panel0.add(panel2); //----- 円錐/ポリゴン radio3 = new CheckboxGroup(); radio31 = new Checkbox("円錐", true, radio3); radio32 = new Checkbox("ポリゴン", false, radio3); Panel panel3 = new Panel(); panel3.add(radio31); panel3.add(radio32); radio31.addItemListener(this); radio32.addItemListener(this); panel0.add(panel3); add(panel0, BorderLayout.SOUTH); } //ラジオボタンイベント処理 public void itemStateChanged(ItemEvent e){ TexCoordGeneration texGen; TextureLoader loader; Texture texture; //----- texGen if(e.getItemSelectable()==radio11){ texGen = new TexCoordGeneration( TexCoordGeneration.OBJECT_LINEAR , TexCoordGeneration.TEXTURE_COORDINATE_2, new Vector4f(2.0f, 0.0f, 0.0f, 0.0f), new Vector4f(0.0f, 2.0f, 0.0f, 0.0f)); ap.setTexCoordGeneration(texGen); }else if(e.getItemSelectable()==radio12){ texGen = new TexCoordGeneration( TexCoordGeneration.EYE_LINEAR , TexCoordGeneration.TEXTURE_COORDINATE_2, new Vector4f(2.0f, 0.0f, 0.0f, 0.0f), new Vector4f(0.0f, 2.0f, 0.0f, 0.0f)); ap.setTexCoordGeneration(texGen); }else if(e.getItemSelectable()==radio13){ texGen = new TexCoordGeneration( TexCoordGeneration.SPHERE_MAP , TexCoordGeneration.TEXTURE_COORDINATE_2, new Vector4f(2.0f, 0.0f, 0.0f, 0.0f), new Vector4f(0.0f, 2.0f, 0.0f, 0.0f)); ap.setTexCoordGeneration(texGen); } //----- file if(e.getItemSelectable()==radio21){ ap.setTexture(texture1); }else if(e.getItemSelectable()==radio22){ ap.setTexture(texture2); } //----- 円錐/ポリゴン if(e.getItemSelectable()==radio31){ branchG1.removeAllChildren(); branchG1.addChild(branchG11); }else if(e.getItemSelectable()==radio32){ branchG1.removeAllChildren(); branchG1.addChild(branchG12); } } public void destroy(){ simpleU.cleanup(); } public static void main(String[] args){ //Application MainFrame frame = new MainFrame(new Ex73(), 512, 512); } }