⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplelight.java

📁 Java具有平台独立、面向对象、多线程等许多优点
💻 JAVA
字号:
import java.applet.Applet; 
import java.awt.BorderLayout; 
import com.sun.j3d.utils.applet.MainFrame; 
import com.sun.j3d.utils.geometry.Sphere; 
import com.sun.j3d.utils.universe.*; 
import javax.media.j3d.*; 

import javax.vecmath.*; 


public class SimpleLight extends Applet{ 

	Appearance createAppearance() {
		Appearance appear = new Appearance();
		Material material = new Material();
		appear.setMaterial(material);
 		return appear;
 	}
	public BranchGroup createSceneGraph() { 
		BranchGroup sceneRoot = new BranchGroup();
		
	    BoundingSphere bounds =new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
   		// Set up the background Color
    	Color3f bgColor = new Color3f(1.0f, 1.0f, 1.0f);
    	Background bg = new Background(bgColor);
    	bg.setApplicationBounds(bounds);
    	sceneRoot.addChild(bg);

		
		//环境光的使用
 		AmbientLight lightA = new AmbientLight();
 		lightA.setEnable(true);
 		lightA.setColor(new Color3f( 0.1f, 0.1f, 0.1f ));
 		lightA.setInfluencingBounds(bounds);
		sceneRoot.addChild(lightA);
		
		//方向光的使用
/*		DirectionalLight lightD1 = new DirectionalLight();
		lightD1.setDirection (new Vector3f(1.0f,0.0f,0.0f));
		lightD1.setColor (new Color3f(0.0f,1.0f,1.0f));
		lightD1.setInfluencingBounds(bounds);
		 // customize DirectionalLight object
		sceneRoot.addChild(lightD1);
		
*/		
		//点光源的使用
/*		PointLight lightP =new PointLight();
		lightP.setPosition(new Point3f(-2.0f,0.0f,0.0f));
		lightP.setColor (new Color3f(0.0f,1.0f,1.0f));
		lightP.setAttenuation (new Point3f(0.1f,0.2f,0.2f));
		lightP.setInfluencingBounds (bounds);
		sceneRoot.addChild (lightP);
*/
		//聚光灯的使用
		SpotLight lightS=new SpotLight ();
//		lightS.setEnable (true);
		lightS.setPosition (new Point3f(0.5f,0.0f,10.0f));
		lightS.setColor (new Color3f(0.0f,1.0f,1.0f));
		lightS.setAttenuation (new Point3f(1.0f,0.0f,0.0f));
		lightS.setDirection (new Vector3f(0.0f,0.0f,-1.0f));
		lightS.setSpreadAngle (15.0f * (float)Math.PI / 180.0f);
		lightS.setConcentration (100);
		lightS.setInfluencingBounds (bounds);
		sceneRoot.addChild (lightS);
		
		Sphere  ball1=new  Sphere(0.5f,Sphere.GENERATE_NORMALS,createAppearance());	 	
		Sphere  ball2=new  Sphere(0.5f,Sphere.GENERATE_NORMALS,createAppearance());	 	
		
		Transform3D tr = new Transform3D(); 
		tr.setScale(0.8); 
		tr.setTranslation(new Vector3f(0.5f, 0.0f, 0.0f)); 
		TransformGroup objTransr = new TransformGroup(tr); 
		objTransr.addChild(ball1); 

		Transform3D tl = new Transform3D(); 
		tl.setScale(0.8); 
		tl.setTranslation(new Vector3f(-0.5f, 0.0f, 0.0f)); 
		TransformGroup objTransl = new TransformGroup(tl); 
		objTransl.addChild(ball2); 

 		sceneRoot.addChild(objTransr);
		sceneRoot.addChild (objTransl);
		System.out.println("hello");

 		return sceneRoot;
	} 

	public SimpleLight() { 
		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 SimpleLight(), 512, 256); 
	} 
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -