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

📄 wallpaper.java

📁 使用java3d进行,obj文件的加载.和实现全方位的3维场景交互
💻 JAVA
字号:
package csu.chailei.test422;

import java.applet.Applet;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Choice;
import java.awt.event.*;
import java.awt.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.vp.*;
import csu.chailei.Resources;
/**   Simple Texture Mapping example*/
public class Wallpaper extends Applet implements ItemListener
{
	/**implements ItemListener
	 * @param args
	 */
	Choice choice;
	 private SimpleUniverse universe ;   
	 private BranchGroup scene;  
	 private Canvas3D canvas;   
	 private BoundingSphere bounds =new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 1000.0);  
	 private static java.net.URL texImage1 = null;
	 private static java.net.URL texImage2 = null;
	 private static java.net.URL texImage3= null;
	 private static java.net.URL bgImage1= null;
	 private static java.net.URL bgImage2= null;
	  public Group  createGeometry(int filter, float y, java.net.URL texImage) {  
		  /**         Create some Texture mapped objects        */  
		  Appearance appearance = new Appearance();       
		  TextureLoader tex = new TextureLoader(texImage, TextureLoader.GENERATE_MIPMAP , this);     
		  Texture texture = tex.getTexture();       
		  texture.setMinFilter(filter) ;   
		  appearance.setTexture(texture);  
		  TextureAttributes texAttr = new TextureAttributes();     
		  texAttr.setTextureMode(TextureAttributes.MODULATE);     
		  appearance.setTextureAttributes(texAttr);      
		  Color3f black = new Color3f(0.0f, 0.0f, 0.0f);   
		  Color3f white = new Color3f(1.0f, 1.0f, 1.0f);    
		  // Set up the material properties      
		  appearance.setMaterial(new Material(white, black, white, black, 1.0f));    
		  //use to build tree hierarchy     
		  Group topNode = new Group();  
		  Transform3D translate = new Transform3D(); 
		  translate.setTranslation(new Vector3f(.5f,y,-0.5f));   
		  TransformGroup gimmeSpace = new TransformGroup(translate); 
		  Cone cone =  new Cone(.4f,0.8f,Primitive.GENERATE_NORMALS|Primitive.GENERATE_TEXTURE_COORDS,appearance);      
		  gimmeSpace.addChild(cone);    
		  topNode.addChild(gimmeSpace); //cone at bottom    
		  translate = new Transform3D();     
		  translate.setTranslation(new Vector3f(-0.5f,y,-0.5f));      
		  gimmeSpace = new TransformGroup(translate);    
		  Sphere sphere = new Sphere(.4f,Primitive.GENERATE_NORMALS|Primitive.GENERATE_TEXTURE_COORDS,appearance);   
		  gimmeSpace.addChild(sphere);        
		  topNode.addChild(gimmeSpace); 
		  //cone at bottom       
		  return topNode;   
		  }
	  
	 
	  public void setupView() {       
		  /** Add some view related things to view branch side        
		   *  of scene graph */    
		  // add mouse interaction to the ViewingPlatform    
		  OrbitBehavior orbit = new OrbitBehavior(canvas,OrbitBehavior.REVERSE_ALL|OrbitBehavior.STOP_ZOOM);
		  orbit.setSchedulingBounds(bounds);
		  ViewingPlatform viewingPlatform = universe.getViewingPlatform();
		  // This will move the ViewPlatform back a bit so the
		  // objects in the scene can be viewed.       
		  viewingPlatform.setNominalViewingTransform();    
		  viewingPlatform.setViewPlatformBehavior(orbit);   
		  }
	  public BranchGroup createSceneGraph() {     
		  // Create the root of the branch graph  
		  BranchGroup objRoot = new BranchGroup();  
		  // Create a simple Shape3D node; add it to the scene graph.    
		  // Set up the texture map   
		  // the path to the image      
		  objRoot.addChild(createGeometry( Texture.BASE_LEVEL_POINT,1.0f,texImage1)); 
		  objRoot.addChild(createGeometry( Texture.MULTI_LEVEL_POINT,0.0f,texImage2));
		  objRoot.addChild(createGeometry( Texture.MULTI_LEVEL_LINEAR,-1.0f,texImage3));   
		  //throw in some light so we aren't stumbling       
		  //around in the dark     
		  Color3f lightColor = new Color3f(.5f,.5f,.5f);
		  AmbientLight ambientLight= new AmbientLight(lightColor);
		  ambientLight.setInfluencingBounds(bounds);   
		  objRoot.addChild(ambientLight);    
		  DirectionalLight directionalLight = new DirectionalLight(); 
		  directionalLight.setColor(lightColor);      
		  directionalLight.setInfluencingBounds(bounds);   
		  objRoot.addChild(directionalLight);   
		  //bg
		  //Color3f bgColor=new Color3f(0.3f,0.01f,0.1f);
		  TextureLoader bgTexture=new TextureLoader(bgImage1,this);
		  Background bg=new Background(bgTexture.getImage());
           bg.setApplicationBounds(bounds);
           objRoot.addChild(bg);
		  return objRoot;  
		  }
	  public Wallpaper() {    
		  
	  }
	  
	  public void init() {      
		  scene = createSceneGraph();
		  setLayout(new BorderLayout());
		  GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); 
		  canvas = new Canvas3D(config); 
		  add("Center", canvas);        
		  // Create a simple scene and attach it to the virtual universe     
		  universe = new SimpleUniverse(canvas);  
		  setupView();        
		  universe.addBranchGraph(scene);
		  choice = new Choice();
			choice.addItem("ColorCube");
			choice.addItem("Sphere");
			choice.addItem("ObjFile");
			choice.addItem("Sphere+sky");
			choice.addItem("ColorCube+rotate");
			choice.addItemListener(this);
			add("North", choice);
		  }
	  public void destroy() {      
		  universe.removeAllLocales();
		  }
	  public void itemStateChanged(ItemEvent e) {
			// TODO Auto-generated method stub
			int index = choice.getSelectedIndex();

			switch (index) {
			case 0: /* stone + light */
				System.out.println("0");
				break;
			case 1: /* stone */
				System.out.println("1");
				scene.addChild(new ColorCube(0.4f));
				break;
			case 2: /* light */
				System.out.println("2");
				break;
			case 3: /* sky */
				System.out.println("3");
				break;
			case 4: /* stone + sky */
				System.out.println("4");
				break;
			default: /* both */
				System.out.println("5");
				break;
			}
		}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 try{
			 if (args.length == 0)
			 {
			       texImage1= Resources.getResource("/resouce\\images\\bg.jpg");
			       texImage2= Resources.getResource("/resouce\\images\\a.jpg");
			       texImage3= Resources.getResource("/resouce\\images\\Java3d.jpg");
			       bgImage1=Resources.getResource("/resouce\\images\\bg1.jpg");
				 // texImage = new java.net.URL("file:./images/speedchase.jpg");  
			 }   
			 else
			 {texImage1 = new java.net.URL(args[0]);}
			 }       
		 catch (java.net.MalformedURLException ex) 
		 {
			 System.out.println(ex.getMessage());     
		 System.exit(1);       
		 }       
		 new MainFrame(new Wallpaper(),400,400);}
	}


⌨️ 快捷键说明

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