📄 simpletexture.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 com.sun.j3d.utils.geometry.Cylinder;import javax.media.j3d.*;import javax.vecmath.*;public class SimpleTexture extends Applet { public BranchGroup createSceneGraph(String filename) { // 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); // Create the transform group node and initialize it to the // identity. Enable the TRANSFORM_WRITE capability so that // our behavior code can modify it at runtime. Add it to the // root of the subgraph. TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); sceneRoot.addChild(objTrans); TextureLoader myLoader=new TextureLoader (filename,this); ImageComponent2D myImage=myLoader.getImage (); if ( myImage == null ) { System.err.println( "Cannot load texture '" + filename + "'" ); return null; } Texture2D myTex=(Texture2D)myLoader.getTexture(); // Create appearance object for textured cube Appearance app = new Appearance(); //允许光照 app.setTexture(myTex); TextureAttributes texAttr = new TextureAttributes(); Transform3D myTrans = new Transform3D( ); myTrans.rotZ( Math.PI/4.0 ); // 45 degrees texAttr.setTextureTransform( myTrans ); // Create textured cylinder and add it to the scene graph. Cylinder cylin=new Cylinder(0.35f,1.0f,Cylinder.GENERATE_TEXTURE_COORDS ,app); objTrans.addChild (cylin); // Create a new Behavior object that will perform the desired // operation on the specified transform object and add it into // the scene graph. Transform3D yAxis = new Transform3D();/* Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0); RotationInterpolator rotator =new RotationInterpolator(rotationAlpha, objTrans, yAxis, 0.0f, (float) Math.PI*2.0f); rotator.setSchedulingBounds(bounds); objTrans.addChild(rotator);*//* DirectionalLight lightD1 = new DirectionalLight(); Vector3f direction = new Vector3f(-0.0f, -1.0f, -1.0f); Color3f cyan=new Color3f (0.0f,1.0f,1.0f); //direction.normalize(); lightD1.setDirection(direction); lightD1.setColor (cyan); lightD1.setInfluencingBounds(bounds); sceneRoot.addChild(lightD1);*/ // Have Java 3D perform optimizations on this scene graph. sceneRoot.compile(); return sceneRoot; } public SimpleTexture(String filename) { filename = "coca2.jpg"; 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(filename); if(scene==null) { System.err.println( "Cannot create the scene graph" ); return; } SimpleUniverse 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); } // // The following allows TextureImage to be run as an application // as well as an applet // public static void main(String[] args) { new MainFrame(new SimpleTexture(null), 256, 256); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -