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

📄 molecule1.java

📁 包括了JAVA3D(全世界都能看到的网络三维动画)的源代码部分! 很多基础但是却很精彩的例子!有什么不明白的也可以和我交流MSN:guorui0728@hotmail.com
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			pointTransformGroup[l][i].addChild(new Link(shared));
			group.addChild(pointTransformGroup[l][i]);
		}
	}
	return group;
  }


  private void moleculeActivate()  
  {
    int l=prevLink;
    int i=prevVert;
    
   
	if (i==links[l].numVert-1) 
	{
		pointTransform3D[l][i] = transForm(
			pointPosition[l][i-1].x - pointPosition[l][i].x,
			pointPosition[l][i-1].y - pointPosition[l][i].y,
			pointPosition[l][i-1].z - pointPosition[l][i].z
			);
	} else
	{
		pointTransform3D[l][i] = transForm(
			pointPosition[l][i+1].x - pointPosition[l][i].x,
			pointPosition[l][i+1].y - pointPosition[l][i].y,
			pointPosition[l][i+1].z - pointPosition[l][i].z
			);
	}
	pointTransform3D[l][i].setTranslation(new Vector3d(pointPosition[l][i].x, 
		pointPosition[l][i].y, pointPosition[l][i].z));
	pointTransformGroup[l][i].setTransform( pointTransform3D[l][i] );
	
	changeAVert();		// 先显示,再改变坐标,可以不用记住改变之前的位置
  }
  
  
  public BranchGroup createSceneGraph(Canvas3D c) {

    BranchGroup objRoot = new BranchGroup();
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
    
    // 顶板和底板颜色及材质
    Appearance ap0 = new Appearance();
    Material mat0 = new Material();
    mat0.setDiffuseColor(new Color3f(1f,.0f,0f));
    ap0.setMaterial(mat0);
    
    // 上下垂直坐标轴的颜色及材质
    Appearance ap2 = new Appearance();
    Material mat2 = new Material();
    mat2.setDiffuseColor(new Color3f(0/255f,0/255f,180/255f));
    ap2.setMaterial(mat2);

    // 左右水平标轴的颜色及材质
    Appearance ap3 = new Appearance();
    Material mat3 = new Material();
    mat3.setDiffuseColor(new Color3f(0/255f,0/255f,125/255f));
    ap3.setMaterial(mat3);

    // 垂直屏幕的坐标轴的颜色及材质
    Appearance ap4 = new Appearance();
    Material mat4 = new Material();
    mat4.setDiffuseColor(new Color3f(0/255f,0/255f,125/255f));
    ap4.setMaterial(mat4);
    
    // 背景
    Color3f bgColor = new Color3f(0/255f, 0/255f, 0/255f);
    Background bg = new Background(bgColor);
      bg.setApplicationBounds(bounds);
    objRoot.addChild(bg);
    
    // 灯光
	// Set up the global lights
	Color3f lColor1 = new Color3f(1f, 1f, 1f);
	Vector3f lDir1  = new Vector3f(-1.0f, -1.0f, -1.0f);


	DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
	lgt1.setInfluencingBounds(bounds);
	objRoot.addChild(lgt1);

	Color3f DirectionalColor = new Color3f(1.f, 1.f, 1.f);
    Vector3f vec = new Vector3f( 0.f, 1.f, -1.0f );
	DirectionalLight DirectionalLight= new DirectionalLight(DirectionalColor,vec);
    DirectionalLight.setInfluencingBounds(bounds);
    objRoot.addChild(DirectionalLight);

    globalTransform3D = new Transform3D();
      globalTransform3D.rotX(0.3);    
    globalTransform3DBackup=new Transform3D(globalTransform3D);
    globalTransformGroup = new TransformGroup(globalTransform3D);
      globalTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
      globalTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objRoot.addChild(globalTransformGroup);

	// 顶板
    Transform3D t = new Transform3D();
    t.setTranslation(new Vector3f(0f,1.5f,0f));
    TransformGroup gb1 = new TransformGroup(t);
    Box box = new Box(1.5f,0.02f,1.2f,ap0);
    gb1.addChild(box);
    globalTransformGroup.addChild(gb1);   
    
    // 底板
    t = new Transform3D();
    t.setTranslation(new Vector3f(0f,-1.5f,0f));
    TransformGroup gb2 = new TransformGroup(t);
    box = new Box(1.5f,0.02f,1.2f,ap0);
    gb2.addChild(box);    
	globalTransformGroup.addChild(gb2);
    
	// 中心垂直轴
	globalTransformGroup.addChild(new Cylinder(0.03f, 3f, ap2));
	
	// 从右到左坐标轴
	t = new Transform3D();
	t.rotZ(Math.PI/2);
	TransformGroup gb3 = new TransformGroup(t);
	gb3.addChild(new Cylinder(0.03f, 3f, ap3));
	globalTransformGroup.addChild(gb3);

	t = new Transform3D();
	t.rotZ(Math.PI/2);
	t.setTranslation(new Vector3f(-1.5f,0f,0f));
	TransformGroup gb4 = new TransformGroup(t);
	gb4.addChild(new Cone(0.05f, 0.12f, Primitive.GENERATE_NORMALS, ap3));
	globalTransformGroup.addChild(gb4);
	
	// 从内到外坐标轴
	t = new Transform3D();
	t.rotX(Math.PI/2);
	TransformGroup gb5 = new TransformGroup(t);
	gb5.addChild(new Cylinder(0.03f, 3f, ap4));
	globalTransformGroup.addChild(gb5);

	t = new Transform3D();
	t.rotX(Math.PI/2);
	t.setTranslation(new Vector3f(0f,0f,1.5f));
	TransformGroup gb6 = new TransformGroup(t);
	gb6.addChild(new Cone(0.05f, 0.12f, Primitive.GENERATE_NORMALS, ap4));
	globalTransformGroup.addChild(gb6);
	
	globalTransformGroup.addChild(moleculeLinks());

    MouseRotate behavior = new MouseRotate();
    behavior.setTransformGroup(globalTransformGroup);
    behavior.setSchedulingBounds(bounds);
    objRoot.addChild(behavior);

    MouseZoom behavior2 = new MouseZoom();
    behavior2.setTransformGroup(globalTransformGroup);
    behavior2.setSchedulingBounds(bounds);
    objRoot.addChild(behavior2);

    MouseTranslate behavior3 = new MouseTranslate();
    behavior3.setTransformGroup(globalTransformGroup);
    behavior3.setSchedulingBounds(bounds);
    objRoot.addChild(behavior3);

    KeyNavigatorBehavior key=new KeyNavigatorBehavior(globalTransformGroup);
    	key.setSchedulingBounds(bounds);
    objRoot.addChild(key);    

    objRoot.compile();
    return objRoot;
  }
    
  Group aMolecule(){

    // 分子小球颜色及材质
    Appearance ap11 = new Appearance();
    Material mat11 = new Material();
    mat11.setDiffuseColor(new Color3f(1f,0.65f,1f));
    
    ap11.setMaterial(mat11);

	// 分子杆
    Appearance ap12 = new Appearance();
    Material mat12 = new Material();
    mat12.setDiffuseColor(new Color3f(1f,0.65f,1f));
    ap12.setMaterial(mat12);

    Group group = new Group();
  	
    Transform3D t1 = new Transform3D();
    TransformGroup g1 = new TransformGroup(t1);
    g1.addChild(new Sphere(0.08f,ap11));
    
    t1=new Transform3D();
    t1.setTranslation(new Vector3f(0f,0.14f,0f));
    TransformGroup g2 = new TransformGroup(t1);
    g2.addChild(new Cylinder(0.03f,0.12f,ap11));
    
    group.addChild(g1);
    group.addChild(g2);

    return group;
  }

  public void actionPerformed(ActionEvent e) {
	if (e.getSource() == activateB){
		if (activateState)
		{
			stop();		
			activateState=false;
		} else
		{
			start();
			activateState=true;
		}
	}
	if (e.getSource() == normalB){
	    globalTransform3D.set(globalTransform3DBackup);
	    globalTransformGroup.setTransform(globalTransform3D);
	}
	if (e.getSource() == rotateBX){
	    angle += Math.toRadians(-15.0);
	    globalTransform3D.rotX(angle);
	    globalTransformGroup.setTransform(globalTransform3D);
	}
	if (e.getSource() == rotateBY){
	    angle += Math.toRadians(30.0);
	    globalTransform3D.rotY(angle);
	    globalTransformGroup.setTransform(globalTransform3D);
	}
	if (e.getSource() == rotateBZ){
	    angle += Math.toRadians(30.0);
	    globalTransform3D.rotZ(angle);
	    globalTransformGroup.setTransform(globalTransform3D);
	}
  }

  public void start() {
  	timerFlag=true;
	timer = new Thread(this);
    timer.start();
  }

  public void stop() {
  	timerFlag=false;
  	timer.interrupt();
    timer = null;
  }

  public void destroy() {        
  }

  public void run() {    
    while (timer != null && timerFlag) {
    	try{
	        if (timerFlag) moleculeActivate();	        
	        Thread.sleep(180);
	    } catch (InterruptedException e) {return;}
    }
  }

    
  public molecule1() {
    setLayout(new BorderLayout());
    Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
    add("Center", c);

	Panel p = new Panel();
	
	p.add(activateB);
	p.add(normalB);
	p.add(rotateBX);
	p.add(rotateBY);
	p.add(rotateBZ);
	add("South", p);

	activateB.addActionListener(this);
	normalB.addActionListener(this);
	rotateBX.addActionListener(this);
	rotateBY.addActionListener(this);
	rotateBZ.addActionListener(this);

    Viewer viewer = new Viewer(c);
    Vector3d viewpoint = new Vector3d(0.0, 0.0, 7.0);  //初始观察点位置
    Transform3D t = new Transform3D();
      t.set(viewpoint);
    ViewingPlatform v = new ViewingPlatform( );
    v.getViewPlatformTransform().setTransform(t);

    BranchGroup scene = createSceneGraph(c);
    SimpleUniverse u = new SimpleUniverse(v, viewer);
    u.getViewingPlatform();
    u.addBranchGraph(scene); 
  }

  public static void main(String[] args) {
    new MainFrame(new molecule1(), 800, 540);
  }
}

⌨️ 快捷键说明

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