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

📄 textext.java

📁 java 3d 系列源码
💻 JAVA
字号:
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.*;
import java.awt.Font;
import com.sun.j3d.utils.applet.MainFrame; 
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.GraphicsConfiguration;

public class TexText extends Applet {

	public BranchGroup createSceneGraph() {
		// Create the root of the branch graph
		BranchGroup objRoot = 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);
    	objRoot.addChild(bg);



		//环境光的使用
 		AmbientLight lightA = new AmbientLight();
 		lightA.setEnable(true);
 		lightA.setColor(new Color3f( 0.1f, 0.1f, 0.1f ));
 		lightA.setInfluencingBounds(bounds);
		objRoot.addChild(lightA);
		
		//方向光的使用
		DirectionalLight lightD1 = new DirectionalLight();
		lightD1.setDirection (new Vector3f(1.0f,.0f,0.0f));
		lightD1.setColor (new Color3f(1.0f,1.0f,1.0f));
		lightD1.setInfluencingBounds(bounds);
		 // customize DirectionalLight object
		objRoot.addChild(lightD1);

		Transform3D t3D = new Transform3D();
		t3D.setTranslation(new Vector3f(0.0f, -0.2f, -3.0f));
		TransformGroup objMove = new TransformGroup(t3D);
		objRoot.addChild(objMove);
		
		
		// Create the transform group node and initialize it to the 
		// identity. Add it to the root of the subgraph.
		TransformGroup objSpin = new TransformGroup();
		objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		objMove.addChild(objSpin);

		Appearance textAppear = new Appearance();

		PolygonAttributes polyAttrib = new PolygonAttributes();
		polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
		textAppear.setPolygonAttributes(polyAttrib);

		TexCoordGeneration tcg = new TexCoordGeneration(TexCoordGeneration.OBJECT_LINEAR,
														TexCoordGeneration.TEXTURE_COORDINATE_2);
		textAppear.setTexCoordGeneration(tcg);

		String filename = "brick.jpg";
		
		TextureLoader myTL = new TextureLoader(filename, this);
		ImageComponent2D myIC = myTL.getImage();

		Texture2D myTex = (Texture2D)myTL.getTexture();


		textAppear.setTexture(myTex);

		
		// Create a simple shape leaf node, add it to the scene graph.
		//         Font3D font3D = new Font3D(new Font("Helvetica", Font.PLAIN, 1),
		//                                  new FontExtrusion());
		Font3D font3D = new Font3D(new Font("宋体", Font.PLAIN, 1),
								   new FontExtrusion());
		Text3D textGeom = new Text3D(font3D, new String("Java 3D"));
		textGeom.setAlignment(Text3D.ALIGN_CENTER);
		Shape3D textShape = new Shape3D();
		textShape.setGeometry(textGeom);
		textShape.setAppearance(textAppear);
		objSpin.addChild(textShape);

		return objRoot;
	}


	public TexText() {
		setLayout(new BorderLayout());
		GraphicsConfiguration config = 
               SimpleUniverse.getPreferredConfiguration();
    	Canvas3D canvas3D=new Canvas3D(config);
		add("Center", canvas3D);

		BranchGroup scene = createSceneGraph();

		// SimpleUniverse is a Convenience Utility class
		SimpleUniverse simpleU = new SimpleUniverse(canvas3D);

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

		simpleU.addBranchGraph(scene);
	}

	//  The following allows this to be run as an application
	//  as well as an applet

	public static void main(String[] args) {
		
		Frame frame = new MainFrame(new TexText(), 512, 200);
	}

} 

⌨️ 快捷键说明

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