📄 texline.java
字号:
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
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 javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.GraphicsConfiguration;
public class TexLine extends Applet {
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;
}
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(filename, this);
ImageComponent2D image = myTL.getImage();
Texture2D myTex=(Texture2D)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;
}
// Create a simple scene and attach it to the virtual universe
public TexLine() {
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 u = new SimpleUniverse(canvas3D);
// 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 method allows this to be run as an application
public static void main(String[] args) {
Frame frame = new MainFrame(new TexLine(), 256, 256);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -