java3d生成简单的点线图
2012-11-15 15:50:52 来源:WEB开发网核心提示:import java.applet.Applet;import java.awt.BorderLayout;import com.sun.j3d.utils.applet.MainFrame;import com.sun.j3d.utils.geometry.Primitive;import com.sun.j3d.
import java.applet.Applet; import java.awt.BorderLayout; import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.geometry.Primitive; import com.sun.j3d.utils.universe.*; import javax.media.j3d.*; import javax.vecmath.*; import com.sun.j3d.utils.behaviors.mouse.*; public class DisplayLinesPoints extends Applet { public BranchGroup createBranchGroupSceneGraph() { BranchGroup BranchGroupRoot=new BranchGroup(); BoundingSphere bounds= new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0); Color3f bgColor=new Color3f(1.0f,1.0f,1.0f); Background bg=new Background(bgColor); bg.setApplicationBounds(bounds); BranchGroupRoot.addChild(bg); Color3f directionalColor=new Color3f(1.f,0.f,0.f); Vector3f vec=new Vector3f(0.f,0.f,-1.0f); DirectionalLight directionalLight=new DirectionalLight(directionalColor,vec); directionalLight.setInfluencingBounds(bounds); BranchGroupRoot.addChild(directionalLight); Appearance app=new Appearance(); Material material=new Material(); material.setDiffuseColor(new Color3f(1.0f,1.0f,0.0f)); app.setMaterial(material); TransformGroup transformgroup=new TransformGroup(); transformgroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); transformgroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); BranchGroupRoot.addChild(transformgroup); MouseRotate mouserotate=new MouseRotate(); mouserotate.setTransformGroup(transformgroup); //BranchGroupRoot.addChild(mouserotate); mouserotate.setSchedulingBounds(bounds); MouseZoom mousezoom=new MouseZoom(); mousezoom.setTransformGroup(transformgroup); BranchGroupRoot.addChild(mouserotate); mouserotate.setSchedulingBounds(bounds); MouseTranslate mousetranslate=new MouseTranslate(); mousetranslate.setTransformGroup(transformgroup); BranchGroupRoot.addChild(mousetranslate); mousetranslate.setSchedulingBounds(bounds); Shape3D shapelinespoints=new ShapeLinesPoints(); Shape3D shapepoints=new ShapePoints(); transformgroup.addChild(shapelinespoints); transformgroup.addChild(shapepoints); BranchGroupRoot.compile(); return BranchGroupRoot; } public DisplayLinesPoints() { setLayout(new BorderLayout()); Canvas3D c=new Canvas3D(SimpleUniverse.getPreferredConfiguration()); add("Center",c); BranchGroup BranchGroupScene=createBranchGroupSceneGraph(); SimpleUniverse u=new SimpleUniverse(c); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(BranchGroupScene); } public static void main(String[] args) { new MainFrame(new DisplayLinesPoints(),500,500); } } class ShapeLinesPoints extends Shape3D { public ShapeLinesPoints() { float[][][][] cubelines=new float[6][6][6][3]; int i,j,k,c; for (i=0;i<6;i++) for (j=0;j<6;j++) for (k=0;k<6;k++) { cubelines[i][j][k][0]=0.2f*k-0.5f; cubelines[i][j][k][1]=0.2f*j-0.5f; cubelines[i][j][k][2]=0.2f*i-0.5f; } Point3f[] vertexes=new Point3f[500]; for(i=0;i<500;i++) vertexes[i]=new Point3f(); c=0; for(k=0;k<6;k++) for(j=0;j<6;j++) { vertexes[2*c].x=cubelines[k][0][j][0]; vertexes[2*c].y=cubelines[k][0][j][1]; vertexes[2*c].z=cubelines[k][0][j][2]; vertexes[2*c+1].x=cubelines[k][5][j][0]; vertexes[2*c+1].y=cubelines[k][5][j][1]; vertexes[2*c+1].z=cubelines[k][5][j][2]; c=c+1; } for(k=0;k<6;k++) for(j=0;j<6;j++) { vertexes[2*c].x=cubelines[k][j][0][0]; vertexes[2*c].y=cubelines[k][j][0][1]; vertexes[2*c].z=cubelines[k][j][0][2]; vertexes[2*c+1].x=cubelines[k][j][5][0]; vertexes[2*c+1].y=cubelines[k][j][5][1]; vertexes[2*c+1].z=cubelines[k][j][5][2]; c=c+1; } for(k=0;k<6;k++) for(j=0;j<6;j++) { vertexes[2*c].x=cubelines[0][k][j][0]; vertexes[2*c].y=cubelines[0][k][j][1]; vertexes[2*c].z=cubelines[0][k][j][2]; vertexes[2*c+1].x=cubelines[5][k][j][0]; vertexes[2*c+1].y=cubelines[5][k][j][1]; vertexes[2*c+1].z=cubelines[5][k][j][2]; c=c+1; } //�������е����ɫ���� Color3f[] colors=new Color3f[500]; for(i=0;i<500;i++) colors[i]=new Color3f(1.0f,0.f,1.f); LineArray lines=new LineArray(500,LineArray.COORDINATES|LineArray.COLOR_3); lines.setCoordinates (0,vertexes); lines.setColors(0,colors); LineAttributes lineattributes=new LineAttributes(); lineattributes.setLineWidth(1.0f); lineattributes.setLineAntialiasingEnable(true); lineattributes.setLinePattern(0); Appearance app=new Appearance(); app.setLineAttributes(lineattributes); this.setGeometry(lines); this.setAppearance(app); } } class ShapePoints extends Shape3D { public ShapePoints() { float[][][][] cubelines=new float[6][6][6][3]; int i,j,k,c; Point3f[][][] vertexes=new Point3f[50][50][50]; Color3f[][][] pointcolors=new Color3f[50][50][50]; for(i=0;i<6;i++) for(j=0;j<6;j++) for(k=0;k<6;k++) { vertexes[i][j][k]=new Point3f(); pointcolors[i][j][k]=new Color3f(); } //����ÿ�����x.y.z���ֲ for(i=0;i<5;i++) for(j=0;j<5;j++) for(k=0;k<5;k++) { //��0.5ʹԭ����С�ǵ�λ�����ԭ��Ĵ�������ת��Ϊ����λ�����ϵԭ��������� cubelines[i][j][k][0]=0.2f*k-0.5f; cubelines[i][j][k][1]=0.2f*j-0.5f; cubelines[i][j][k][2]=0.2f*i-0.5f; vertexes[i][j][k].x=cubelines[i][j][k][0]+0.1f; vertexes[i][j][k].y=cubelines[i][j][k][1]+0.1f; vertexes[i][j][k].z=cubelines[i][j][k][2]+0.1f; pointcolors[i][j][k].x=0.2f*i; pointcolors[i][j][k].y=0.2f*j; pointcolors[i][j][k].z=0.2f*k; } Point3f[] vertexes0=new Point3f[125]; Color3f[] pointcolors0=new Color3f[125]; for(i=0;i<125;i++) { vertexes0[i]=new Point3f(); pointcolors0[i]=new Color3f(); } c=0; for(i=0;i<5;i++) for(j=0;j<5;j++) for(k=0;k<5;k++) { vertexes0[c]=vertexes[i][j][k]; pointcolors0[c]=pointcolors[i][j][k]; c=c+1; } int vCount=125; PointArray points=new PointArray(vCount,PointArray.COORDINATES|IndexedPointArray.COLOR_3); points.setCoordinates(0,vertexes0); points.setColors(0,pointcolors0); PointAttributes pointsattributes=new PointAttributes(); pointsattributes.setPointSize(10.0f); pointsattributes.setPointAntialiasingEnable(true); Appearance app=new Appearance(); app.setPointAttributes(pointsattributes); this.setGeometry(points); this.setAppearance(app); } }
更多精彩
赞助商链接