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

📄 textureimage.java

📁 java 3d 系列源码
💻 JAVA
字号:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.geometry.Box;
import javax.media.j3d.*;
import javax.vecmath.*;

public class TextureImage extends Applet {
  
 	private java.net.URL imageURL = null;    
	private SimpleUniverse u = null;  
	private Texture myTex;

    public BranchGroup createSceneGraph() {
	// Create the root of the branch graph
		BranchGroup sceneRoot = new BranchGroup();
		BoundingSphere bounds =new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
		
		Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
		Color3f yellow   = new Color3f(1.0f, 1.0f, 0.0f);
		Color3f cyan  = new Color3f(0.0f, 1.0f, 1.0f);

		TransformGroup objTrans = new TransformGroup();
		objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		Transform3D yAxis = new Transform3D();
		yAxis.rotY(Math.PI/4.0);
		objTrans.setTransform(yAxis);
		sceneRoot.addChild(objTrans);
		
//		TextureLoader myLoader=new TextureLoader (filename,this);
		Appearance app = new Appearance();
				
		TextureLoader myLoader=new TextureLoader (imageURL,this);
		if(myLoader==null)
			return null;
//		ImageComponent2D myImage=myLoader.getImage ();
//		myTex=(Texture2D)myLoader.getTexture();
		myTex=myLoader.getTexture();

		// Create appearance object for textured cube

		app.setTexture(myTex);
			
/*		TextureAttributes texAttr = new TextureAttributes();
		Transform3D myTrans = new Transform3D( );
		myTrans.rotZ( Math.PI/4.0 ); // 45 degrees
		texAttr.setTextureTransform( myTrans );
		app.setTextureAttributes(texAttr);
*/
/*		TextureAttributes texAttr = new TextureAttributes();
		Transform3D myTrans = new Transform3D( );
		myTrans.setScale(new Vector3d(2.0,4.0,0.0));
		texAttr.setTextureTransform( myTrans );
		app.setTextureAttributes(texAttr);
*/
/*		TextureAttributes texAttr = new TextureAttributes();
		Transform3D myTrans = new Transform3D( );
		myTrans.setTranslation(new Vector3f(0.5f,0.5f,0.0f));
		texAttr.setTextureTransform( myTrans );
		app.setTextureAttributes(texAttr);
*/
/*		TransparencyAttributes myTA = new TransparencyAttributes( );
		myTA.setTransparency( 0.5f );
		myTA.setTransparencyMode( TransparencyAttributes.BLENDED );
		app.setTransparencyAttributes (myTA);
		
		Material mat = new Material();
        mat.setDiffuseColor(cyan);
        mat.setSpecularColor(white);
        mat.setShininess(128);
		app.setMaterial (mat);
		TextureAttributes texAttr = new TextureAttributes();
		texAttr.setTextureMode(TextureAttributes.REPLACE );
		texAttr.setTextureBlendColor (new Color4f(0.0f,1.0f,1.0f,0.5f));
		app.setTextureAttributes(texAttr);
		
		AmbientLight lightA = new AmbientLight();
		lightA.setInfluencingBounds(bounds);
		sceneRoot.addChild(lightA);

		DirectionalLight lightD1 = new DirectionalLight();
		lightD1.setInfluencingBounds(bounds);
		Vector3f direction = new Vector3f(-0.2f, -1.0f, -1.0f);
		lightD1.setDirection(direction);
		lightD1.setColor(yellow);
		sceneRoot.addChild(lightD1);
*/	
/*		myTex.setBoundaryModeS(Texture2D.CLAMP );
		myTex.setBoundaryModeT(Texture2D.CLAMP );
		myTex.setBoundaryColor (new Color4f(0.0f,1.0f,1.0f,0.0f));
		TextureAttributes texAttr = new TextureAttributes();
		Transform3D myTrans = new Transform3D( );
		myTrans.setScale(2.0f);
		texAttr.setTextureTransform( myTrans );
		app.setTextureAttributes(texAttr);
*/
		
/*		myTex.setMagFilter( Texture.BASE_LEVEL_POINT );
//		myTex.setMinFilter( Texture.BASE_LEVEL_POINT );
		
		TextureAttributes texAttr = new TextureAttributes();
		Transform3D myTrans = new Transform3D( );
		myTrans.setScale(0.5f);
		texAttr.setTextureTransform( myTrans );
		app.setTextureAttributes(texAttr);
*/
		// Create textured cube and add it to the scene graph.
		Box myBox = new Box(0.4f, 0.4f, 0.4f,
								  Box.GENERATE_TEXTURE_COORDS
								  |Box.GENERATE_NORMALS , app);
		objTrans.addChild(myBox);
		
	// Have Java 3D perform optimizations on this scene graph.
		sceneRoot.compile();
		return sceneRoot;
    }

    public TextureImage() {
    }

    public TextureImage(java.net.URL url) {
        imageURL = url;
    }

    public void init() {
        if (imageURL == null) {
  	    // the path to the image for an applet
  	    try {
	        imageURL = new java.net.URL(getCodeBase().toString() +
					    "../misc/brick.jpg");
//			imageURL = new java.net.URL("file:///D:/gl/Java_3d_programing_practice/disk/source/misc/brick.jpg");
//			System.out.println(imageURL.toString());
//			myShow(imageURL.toString());
			
	    }
	    catch (java.net.MalformedURLException ex) {
	        System.out.println(ex.getMessage());
		System.exit(1);
	    }
	}
	setLayout(new BorderLayout());
        GraphicsConfiguration config =
           SimpleUniverse.getPreferredConfiguration();

        Canvas3D c = new Canvas3D(config);
	add("Center", c);

	// Create a simple scene and attach it to the virtual universe
	BranchGroup scene = createSceneGraph();
	u = new SimpleUniverse(c);

        // This will move the ViewPlatform back a bit so the
        // objects in the scene can be viewed.
	u.getViewingPlatform().setNominalViewingTransform();

	u.addBranchGraph(scene);
    }

    public void destroy() {
	u.removeAllLocales();
    }
    
    public void myShow(String s){
  	(new Frame(s)).show();
  }

    //
    // The following allows TextureImage to be run as an application
    // as well as an applet
    //
    public static void main(String[] args) {
        java.net.URL url = null;
        if (args.length > 0) {
	    try {
	        url = new java.net.URL("file:" + args[0]);
	    }
	    catch (java.net.MalformedURLException ex) {
	        System.out.println(ex.getMessage());
		System.exit(1);
	    }
	}
	else {
	    // the path to the image for an application
	    try {
	        url = new java.net.URL("file:../misc/brick.jpg");
	    }
	    catch (java.net.MalformedURLException ex) {
	        System.out.println(ex.getMessage());
		System.exit(1);
	    }
	}
	new MainFrame(new TextureImage(url), 256, 256);
    }

}

⌨️ 快捷键说明

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