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

📄 linec.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.Cylinder;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.GraphicsConfiguration;

public class LineC extends Applet {
  
    private java.net.URL texImage = null;

    private SimpleUniverse u = null;

	Shape3D createBand() {
		Shape3D band = new Shape3D();
		band.setGeometry(createBandGeometry());
		band.setAppearance(createAppearance());
		return band;
	}

	Geometry createBandGeometry(){

		TriangleStripArray bandStrip; 
		Color3f cyan  = new Color3f(0.0f, 1.0f, 1.0f);
		Point2f TexPnt1 = new Point2f(0.0f, 0.0f);
		Point2f TexPnt2 = new Point2f(1.0f, 1.0f);

		// create triangle strip for twist
		int n = 60;
		int counts[] = {n};
		bandStrip = new TriangleStripArray(n,
										   TriangleStripArray.COORDINATES  | TriangleStripArray.COLOR_3
										   | TriangleStripArray.TEXTURE_COORDINATE_2,
										   counts);

		double a;
		int v;
		for(v = 0, a=0.0; v < n; v+=2, a=v*2.0*Math.PI/(n-2)){
			bandStrip.setCoordinate(v, new Point3d(
												   0.7*Math.sin(a),
												   0.3,
												   0.7*Math.cos(a)));
			bandStrip.setCoordinate(v+1, new Point3d(
													 0.7*Math.sin(a),
													 -0.3,
													 0.7*Math.cos(a)));
			bandStrip.setColor(v, cyan);
			bandStrip.setColor(v+1, cyan);
			bandStrip.setTextureCoordinate(v, TexPnt1);
			bandStrip.setTextureCoordinate(v+1, TexPnt2);
		}
		return bandStrip;
	}
	
	// create Appearance for Twist Strip
	//
	
	Appearance createAppearance(){

		Appearance app = new Appearance();

		PolygonAttributes myPA = new PolygonAttributes();
		myPA.setCullFace(PolygonAttributes.CULL_NONE);
		myPA.setPolygonMode(PolygonAttributes.POLYGON_LINE);
		app.setPolygonAttributes(myPA);

//		String filename = "grid4.gif";
		TextureLoader myTL = new TextureLoader(texImage, this);
//		ImageComponent2D image = myTL.getImage();
		Texture myTex=myTL.getTexture();
		app.setTexture(myTex);

		return app;
	}

	/////////////////////////////////////////////////
	//
	// create scene graph branch group
	//
	public BranchGroup createSceneGraph() {

		BranchGroup sceneRoot = new BranchGroup();

		// 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);
		sceneRoot.addChild(objSpin);

		Shape3D band = createBand();
		sceneRoot.addChild(band);
		
		// a bounding sphere specifies a region a behavior is active
		// create a sphere centered at the origin with radius of 1
		BoundingSphere bounds = new BoundingSphere();


		// make background white
		Background background = new Background(1.0f, 1.0f, 1.0f);
		background.setApplicationBounds(bounds);
		sceneRoot.addChild(background);

		// Let Java 3D perform optimizations on this scene graph.
		sceneRoot.compile();

		return sceneRoot;
	} // end of CreateSceneGraph method of TexturedLinesApp



    public LineC() {
    }

    public LineC(java.net.URL url) {
        texImage = url;
    }

    public void init() {
        if (texImage == null) {
  	    // the path to the image for an applet
  	    try {
	        texImage = new java.net.URL(getCodeBase().toString() +
					    "../misc/grid4.gif");
	    }
	    catch (java.net.MalformedURLException ex) {
	        System.out.println(ex.getMessage());
		System.exit(1);
	    }
	}
	setLayout(new BorderLayout());
        GraphicsConfiguration config =
           SimpleUniverse.getPreferredConfiguration();

        GraphicsConfiguration confi= 
               SimpleUniverse.getPreferredConfiguration();
    	Canvas3D c=new Canvas3D(confi);
	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();
    }

    //
    // 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/grid4.gif");
	    }
	    catch (java.net.MalformedURLException ex) {
	        System.out.println(ex.getMessage());
		System.exit(1);
	    }
	}
	new MainFrame(new LineC(url), 256, 256);
    }

}

⌨️ 快捷键说明

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