一个java3d程序,作为熟悉3d技术的基础(可以使用鼠标对场景内的3d对象进行操作)

所有源代码如下:

/********************StringEle.java**********************/

package light3D;

import java.awt.Font;
import java.awt.geom.QuadCurve2D;
import javax.media.j3d.Appearance;
import javax.media.j3d.Font3D;
import javax.media.j3d.FontExtrusion;
import javax.media.j3d.Material;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Text3D;
import javax.vecmath.Color3f;
import javax.vecmath.Point3f;

public class StringEle extends Shape3D{

	private String str= "";
	private Point3f points;
	
	StringEle(String str,Point3f point)
	{
	   this.str=str;
	   this.points=point;
	   FontExtrusion fe=new FontExtrusion();
	   QuadCurve2D.Double curve=new QuadCurve2D.Double();
	   curve.setCurve(0,0,0.0,0.0,0.01,0);
	   FontExtrusion fe1=new FontExtrusion();
	   fe1.setExtrusionShape(curve);
	   Font3D f3d=new Font3D(new Font("",Font.PLAIN,1),fe1);
	   Text3D txt=new Text3D(f3d,str,points);
	   Appearance app=new Appearance();
	   Material m=new Material();
	   m.setDiffuseColor(new Color3f(0.0f,1.0f,0.0f));
	   app.setMaterial(m);
	   this.setGeometry(txt);
	   this.setAppearance(app);  
	}
}

/***********************LineShape.java***********************/

package light3D;

import javax.media.j3d.Appearance;
import javax.media.j3d.LineArray;
import javax.media.j3d.LineAttributes;
import javax.media.j3d.Shape3D;

public class LineShape extends Shape3D{
	private float vert[]=new float[192*2*3];
    
    private float color[]=new float[192*2*3];
    
    public static float points[]=new float[1536];
    
    public LineShape(){
    	setCoordData();
    	setColorData();
    	setPointData();
    	LineArray line=new LineArray(1152,LineArray.COORDINATES|LineArray.COLOR_3);
    	line.setCoordinates(0, vert);
    	line.setColors(0, color);
    	LineAttributes linea = new LineAttributes();
    	linea.setLineWidth(0.01f);
    	linea.setLineAntialiasingEnable(true);
    	Appearance app = new Appearance();
    	app.setLineAttributes(linea);
    	this.setGeometry(line);
    	this.setAppearance(app);
    	this.setPickable(false);
    }
    
    
    public void setCoordData(){
    	int array_num=0;
    	
        //设置X方向上线段对应的数据
    	for(float z=3.5f;z>=-3.5f;z=z-1){
    		for(float y=3.5f;y>=-3.5f;y=y-1){
    			for(int x=-1;x<2;x=x+2,array_num=array_num+3){
    				vert[array_num]=3.5f*x;
    				vert[array_num+1]=1.0f*y;
    				vert[array_num+2]=1.0f*z;
    			}
    		}
    	}
    	
    	//设置y方向上线段对应的数据
    	for(float z=3.5f;z>=-3.5f;z=z-1){
    		for(float x=3.5f;x>=-3.5f;x=x-1){
    			for(int y=-1;y<2;y=y+2,array_num=array_num+3){
    				vert[array_num]=1.0f*x;
    				vert[array_num+1]=3.5f*y;
    				vert[array_num+2]=1.0f*z;
    			}
    		}
    	}
    	
    	//设置z方向上线段对应的数据
    	for(float y=3.5f;y>=-3.5f;y=y-1){
    		for(float x=3.5f;x>=-3.5f;x=x-1){
    			for(int z=-1;z<2;z=z+2,array_num=array_num+3){
    				vert[array_num]=1.0f*x;
    				vert[array_num+1]=1.0f*y;
    				vert[array_num+2]=3.5f*z;
    			}
    		}
    	}
    		
    }
    
    //设置线段颜色数据
    public void setColorData(){
    	for(int i=0;i<1152;i++){
    		color[i]=0.5f;
    	}
    }
    
    //设置节点数据
    public void setPointData(){
    	int point_num=0;
    	for(float x=3.5f;x>=-3.5f;x=x-1){
    		for(float y=3.5f;y>=-3.5f;y=y-1){
    			for(float z=3.5f;z>=-3.5f;z=z-1,point_num=point_num+3){
    			   points[point_num]=1.0f*x;
    			   points[point_num+1]=1.0f*y;
    			   points[point_num+2]=1.0f*z;
    			}
    		}
    	}
    }
}

/*********************MyMousePickBehavior.java****************************/

package light3D;

import java.awt.Color;
import javax.media.j3d.Appearance;
import javax.media.j3d.Bounds;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Material;
import javax.swing.JTextArea;
import javax.vecmath.Color3f;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.picking.behaviors.PickMouseBehavior;
import com.sun.j3d.utils.picking.PickResult;
import com.sun.j3d.utils.picking.PickTool;

public class MyMousePickBehavior extends PickMouseBehavior{
	 int data[][]=new int[8][8];
	 Appearance appear=null;
	 Material mater=null;
	 JTextArea jta=null;
	public MyMousePickBehavior(Canvas3D canvas, BranchGroup root, Bounds bounds, JTextArea jta) {
		super(canvas, root, bounds);
		this.jta=jta;
		this.setSchedulingBounds(bounds);
		root.addChild(this);
		pickCanvas.setMode(PickTool.GEOMETRY);
		  appear=new Appearance();
	      mater=new Material();
	      mater.setDiffuseColor(new Color3f(Color.red));
	      appear.setMaterial(mater);
		
	}

	public void updateScene(int xpos, int ypos) {
		PickResult pickResult = null;
		Sphere shape = null;
		pickCanvas.setShapeLocation(xpos, ypos);

		pickResult = pickCanvas.pickClosest();
		if (pickResult != null) {
		    shape = (Sphere) pickResult.getNode(PickResult.PRIMITIVE);
		    System.out.println(shape.getName());
		    dealData(shape);
		}
		
	    }
	
	public int getZintValue(float zValue){
		if(zValue==-3.5f){
			return Integer.parseInt("00000001", 2);
		}else if(zValue==-2.5f){
			return Integer.parseInt("00000010", 2);
		}else if(zValue==-1.5f){
			return Integer.parseInt("00000100", 2);
		}else if(zValue==-0.5f){
			return Integer.parseInt("00001000", 2);
		}else if(zValue==0.5f){
			return Integer.parseInt("00010000", 2);
		}else if(zValue==1.5f){
			return Integer.parseInt("00100000", 2);
		}else if(zValue==2.5f){
			return Integer.parseInt("01000000", 2);
		}else{
			return Integer.parseInt("10000000", 2);
		}
	}
	
	
    public int getXintValue(float xValue){
    	if(xValue==-3.5f){
			return 0;
		}else if(xValue==-2.5f){
			return 1;
		}else if(xValue==-1.5f){
			return 2;
		}else if(xValue==-0.5f){
			return 3;
		}else if(xValue==0.5f){
			return 4;
		}else if(xValue==1.5f){
			return 5;
		}else if(xValue==2.5f){
			return 6;
		}else{
			return 7;
		}
    }
    
    public int getYintValue(float yValue){
    	if(yValue==-3.5f){
			return 0;
		}else if(yValue==-2.5f){
			return 1;
		}else if(yValue==-1.5f){
			return 2;
		}else if(yValue==-0.5f){
			return 3;
		}else if(yValue==0.5f){
			return 4;
		}else if(yValue==1.5f){
			return 5;
		}else if(yValue==2.5f){
			return 6;
		}else{
			return 7;
		}
    }
	
    
    public void dealData(Sphere shape){
    	String userData=shape.getName();
    	String[] str=userData.split(",");
    	int state=Integer.parseInt(str[3]);
    	float x=Float.parseFloat(str[0]);
    	float y=Float.parseFloat(str[1]);
    	float z=Float.parseFloat(str[2]);
    	if(state==0){
    		data[getXintValue(x)][getYintValue(y)]=data[getXintValue(x)][getYintValue(y)] | getZintValue(z);
    		shape.setName( str[0]+","+str[1]+","+str[2]+","+1);
    		Appearance appear=new Appearance();
    		Material mater=new Material();
  	        mater.setDiffuseColor(new Color3f(Color.red));
  	        appear.setMaterial(mater);
  	        shape.setAppearance(appear);
  	        jta.setText(getDataString());
    	}else{
    		data[getXintValue(x)][getYintValue(y)]=data[getXintValue(x)][getYintValue(y)] & (~getZintValue(z));
    		shape.setName( str[0]+","+str[1]+","+str[2]+","+0);
    		Appearance appear=new Appearance();
    		Material mater=new Material();
  	        mater.setDiffuseColor(new Color3f(Color.black));
  	        appear.setMaterial(mater);
  	        shape.setAppearance(appear);
  	        jta.setText(getDataString());
    	}
    }
    
    
    public String getDataString(){
    	String temp="";
    	temp=temp+"int data_array[8][8]=\n";
    	temp=temp+"{\n";
    	for(int x=0;x<8;x++){
    		temp=temp+"{";
    		for(int y=0;y<8;y++){
    			if(data[x][y]==0){
    				temp=temp+"0x00";
    			}else{
    				temp=temp+"0x"+Integer.toHexString(data[x][y]);
    			}
    			if(y!=7) temp=temp+",";
    		}
    		temp=temp+"}";
    		if(x!=7) temp=temp+",";
    		temp=temp+"\n";
    	}
    	temp=temp+"}";
    	return temp;
    }
	
	}


/***************************LineTest.java*****************************/

package light3D;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GraphicsConfiguration;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.LineArray;
import javax.media.j3d.LineAttributes;
import javax.media.j3d.Material;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.universe.Viewer;
import com.sun.j3d.utils.universe.ViewingPlatform;

public class LineTest extends Applet{
	
    private SimpleUniverse universe;
    GraphicsConfiguration config =SimpleUniverse.getPreferredConfiguration();
    Canvas3D can=new Canvas3D(config);
    JTextArea jta=null;
    public BranchGroup createBranchGroup()
    {
    	
    JFrame f=new JFrame("数组显示区");
    f.setUndecorated(true);
    f.setSize(300,200);
    jta=new JTextArea();
    jta.setForeground(Color.red);
    jta.setBackground(Color.green);
    f.add(jta);
    f.setLocation(java.awt.Toolkit.getDefaultToolkit().getScreenSize().width-320,java.awt.Toolkit.getDefaultToolkit().getScreenSize().height-240);
    f.setResizable(false);
    f.setAlwaysOnTop(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    
    BranchGroup branchgrouproot = new BranchGroup();
    BoundingSphere bouds=new BoundingSphere(new Point3d(0.0,0.0,0.0),100);
    Background bg=new Background(new Color3f(Color.white));
    bg.setApplicationBounds(bouds);
    branchgrouproot.addChild(bg);
    
    //光源的设置
    DirectionalLight directionLight=new DirectionalLight(new Color3f(1.0f,1.0f,1.0f),new Vector3f(0f,3.0f,-5.0f));
    directionLight.setInfluencingBounds(bouds);
    branchgrouproot.addChild(directionLight);
    
    //坐标变换的设置
    TransformGroup trans=new TransformGroup();
    trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    branchgrouproot.addChild(trans);
    
    //鼠标的设置旋转
    MouseRotate behavie1=new MouseRotate();
    behavie1.setTransformGroup(trans);
    branchgrouproot.addChild(behavie1);
    behavie1.setSchedulingBounds(bouds);
    
    //鼠标的平移
    MouseTranslate behavie2=new MouseTranslate();
    behavie2.setTransformGroup(trans);
    branchgrouproot.addChild(behavie2);
    behavie2.setSchedulingBounds(bouds);
    
    //鼠标的放大
    MouseZoom behavie3=new MouseZoom();
    behavie3.setTransformGroup(trans);
    behavie3.setSchedulingBounds(bouds);
    branchgrouproot.addChild(behavie3);
    
    MyMousePickBehavior behavie4=new MyMousePickBehavior(can,branchgrouproot,bouds,jta);
    
    
    TransformGroup lineGroup_pre = new TransformGroup();
    lineGroup_pre.setTransform(new Transform3D());
    LineShape ls=new LineShape();
    lineGroup_pre.addChild(ls);
    trans.addChild(lineGroup_pre);
    
    TransformGroup XwordGroup=new TransformGroup();
    XwordGroup.setTransform(new Transform3D());
    StringEle Xele=new StringEle("X",new Point3f(5.5f,-3.5f,-3.5f));
    XwordGroup.addChild(Xele);
    trans.addChild(XwordGroup);
    
    TransformGroup YwordGroup=new TransformGroup();
    YwordGroup.setTransform(new Transform3D());
    StringEle Yele=new StringEle("Y",new Point3f(-3.5f,5.5f,-3.5f));
    YwordGroup.addChild(Yele);
    trans.addChild(YwordGroup);
    
    TransformGroup ZwordGroup=new TransformGroup();
    ZwordGroup.setTransform(new Transform3D());
    StringEle Zele=new StringEle("Z",new Point3f(-3.5f,-3.5f,5.5f));
    ZwordGroup.addChild(Zele);
    trans.addChild(ZwordGroup);
    
    
 
       
       for(int point_num=0;point_num<1536;point_num=point_num+3){
	       TransformGroup sphereGroup =new TransformGroup();
	       sphereGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	       sphereGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
	       sphereGroup.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
	       Transform3D sphereTransform3D=new  Transform3D();
	       sphereTransform3D.setTranslation(new  Vector3d(LineShape.points[point_num],LineShape.points[point_num+1],LineShape.points[point_num+2]));
	       sphereGroup.setTransform(sphereTransform3D);
	       Sphere sphere2=new  Sphere(0.1f, -1, 80);
	       String userData=""+LineShape.points[point_num]+","+LineShape.points[point_num+1]+","+LineShape.points[point_num+2]+","+0;
	       sphere2.setName(userData);
	       sphereGroup.setName(userData);
	       Appearance appear=new Appearance();
	       Material mater=new Material();
	       mater.setDiffuseColor(new Color3f(Color.black));
	       appear.setMaterial(mater);
	       sphere2.setAppearance(appear);
	       sphereGroup.addChild(sphere2);
	       trans.addChild(sphereGroup);
       }
       
       
       float[] linevertX={-4.5f,-3.5f,-3.5f,    6.0f,-3.5f,-3.5f};
       float[] linevertY={-3.5f,-4.5f,-3.5f,    -3.5f,6.0f,-3.5f};
       float[] linevertZ={-3.5f,-3.5f,-4.5f,    -3.5f,-3.5f,6.0f};
       float[] linecolor={0.0f,0.0f,7.0f,   0.0f,7.0f,0.0f};
       
       LineArray lineX=new LineArray(2,LineArray.COORDINATES | LineArray.COLOR_3);
       lineX.setCoordinates(0, linevertX);
       lineX.setColors(0, linecolor);
       
       LineArray lineY=new LineArray(2,LineArray.COORDINATES | LineArray.COLOR_3);
       lineY.setCoordinates(0, linevertY);
       lineY.setColors(0, linecolor);
       
       LineArray lineZ=new LineArray(2,LineArray.COORDINATES | LineArray.COLOR_3);
       lineZ.setCoordinates(0, linevertZ);
       lineZ.setColors(0, linecolor);
       
       LineAttributes lineAttr=new LineAttributes();
       lineAttr.setLineWidth(3.0f);
       lineAttr.setLineAntialiasingEnable(true);
       
       Appearance lineAppr=new Appearance();
       lineAppr.setLineAttributes(lineAttr);
       
       TransformGroup lineGroup = new TransformGroup();
       lineGroup.setTransform(new Transform3D());
       Shape3D line3DX=new Shape3D();
       line3DX.setGeometry(lineX);
       line3DX.setAppearance(lineAppr);
       lineGroup.addChild(line3DX);
       
       Shape3D line3DY=new Shape3D();
       line3DY.setGeometry(lineY);
       line3DY.setAppearance(lineAppr);
       lineGroup.addChild(line3DY);
       
       Shape3D line3DZ=new Shape3D();
       line3DZ.setGeometry(lineZ);
       line3DZ.setAppearance(lineAppr);
       lineGroup.addChild(line3DZ);
       
       trans.addChild(lineGroup);
       
       
       
       
	   branchgrouproot.compile();
	   return branchgrouproot; 
    }
    
    public void init()
    {
	    this.setLayout(new BorderLayout());
	    this.add("Center",can);
	    Viewer viewer=new Viewer(can);
	    Vector3f vpoint=new Vector3f(0.0f,0.0f,25.0f);
	    Transform3D t=new Transform3D();
	    t.set(vpoint);
	    ViewingPlatform vplat=new ViewingPlatform();
	    vplat.getViewPlatformTransform().setTransform(t);
	    universe= new SimpleUniverse(vplat,viewer);
	    universe.getViewingPlatform();
	    universe.addBranchGraph(createBranchGroup());
    }
    
    
    public static void main(String[] args) {
       new MainFrame(new LineTest(),500,500 );    
    }
}

运行结果部分截图:

(---------------完----------------)

你可能感兴趣的:(java,JavaSE,3D)