《计算机图形学 实验一:生成彩色立方体(含源代码)》由会员分享,可在线阅读,更多相关《计算机图形学 实验一:生成彩色立方体(含源代码)(4页珍藏版)》请在金锄头文库上搜索。
1、实验一实验目的:生成彩色立方体实验代码:/ColorCube1.javaimport java.applet.Applet; /可以插入 htmlimport java.awt.BorderLayout; /窗口采用 BorderLayout 方式布局import com.sun.j3d.utils.applet.MainFrame; /applicationimport com.sun.j3d.utils.geometry.ColorCube;/调用生成 ColorCube 的 Utilityimport com.sun.j3d.utils.geometry.Primitive;import
2、 com.sun.j3d.utils.universe.*; /观测位置的设置import javax.media.j3d.*; /核心类import javax.vecmath.*; /矢量计算import com.sun.j3d.utils.behaviors.mouse.*;public class ColorCube1 extends Applet public BranchGroup createSceneGraph() BranchGroup objRoot=new BranchGroup();/BranchGroup 的一个对象 objRoot(放置背景、灯光)BoundingS
3、phere bounds=new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0);/有效范围TransformGroup objTrans=new TransformGroup();objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);objRoot.addChild(objTrans);MouseRotate behavior = new MouseRotate
4、();behavior.setTransformGroup(objTrans);objRoot.addChild(behavior);behavior.setSchedulingBounds(bounds);MouseZoom behavior2 = new MouseZoom();behavior2.setTransformGroup(objTrans);objRoot.addChild(behavior2);behavior2.setSchedulingBounds(bounds);MouseTranslate behavior3 = new MouseTranslate();behavi
5、or3.setTransformGroup(objTrans);objRoot.addChild(behavior3);behavior3.setSchedulingBounds(bounds); Color3f bgColor=new Color3f(0.0f,0.0f,0.0f);/背景颜色Background bg=new Background(bgColor);bg.setApplicationBounds(bounds);objRoot.addChild(bg);Color3f directionalColor=new Color3f(1.f,1.f,1.f);Vector3f ve
6、c=new Vector3f(1.f,1.f,-1.0f);DirectionalLight directionalLight=new DirectionalLight(directionalColor,vec);directionalLight.setInfluencingBounds(bounds);objRoot.addChild(directionalLight);Appearance app=new Appearance();/外观材质Material material=new Material();/圆锥颜色/material.setEmissiveColor(new Color3
7、f(1.0f,1.0f,0.0f);material.setDiffuseColor(new Color3f(1.0f,1.0f,0.0f);/辐射光效果app.setMaterial(material);ColorCube cone=new ColorCube(0.2);objTrans.addChild(cone);/objRpile();return objRoot;public ColorCube1() setLayout(new BorderLayout();Canvas3D c=new Canvas3D(null);add(Center,c);BranchGroup scene=createSceneGraph();SimpleUniverse u=new SimpleUniverse(c);u.getViewingPlatform().setNominalViewingTransform();u.addBranchGraph(scene);public static void main(String args) new MainFrame(new ColorCube1(),400,300);运行截图: