java 3D 第三天 动态添加物体

上一篇 / 下一篇  2012-03-01 17:39:23 / 个人分类:Java3d开发

   在java 3D中交互式中,我们如果想添加,一个场景中没有的物体,怎么弄呢?

   在场景中,要添加的子孩子既可以在场景中预先定义好,也可以添加时进行定义,但是必须将添加的物体对象定义为全局变量。

  前提必须添加:setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);运行添加子孩子的权限。

  代码我们还是使用第一天的代码:

package com.first.scene;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.GraphicsConfiguration;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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.Material;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.geometry.Cone;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class first_secen extends Applet implements ActionListener{
 SimpleUniverse universe;
 Button button;
 BranchGroup scene;
 BranchGroup exten;
 public BranchGroup createSceneGraph(){
  BranchGroup bjRoot = new BranchGroup();
  objRoot.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
  BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0);
  
  Color3f bgcColor = new Color3f(1.0f,1.0f,1.0f);
  Background bg = new Background(bgcColor);
  bg.setApplicationBounds(bounds);
  objRoot.addChild(bg);
  
  Color3f DirectionalColor = new Color3f(1.f,1.f,1.f);
  Vector3f vec = new Vector3f(0.f,0.f,-1.0f);
  DirectionalLight diretcton = new DirectionalLight(DirectionalColor,vec);
  diretcton.setInfluencingBounds(bounds);
  objRoot.addChild(diretcton);
  
  Appearance app = new Appearance();
  Material material = new Material();
  material.setEmissiveColor(new Color3f(1.0f,0.0f,0.0f));
  app.setMaterial(material);
  ColorCube colorCube = new ColorCube(0.2);
  colorCube.setAppearance(app);
  objRoot.addChild(colorCube);
  
  objRoot.compile();
  return objRoot;
 }
 
 public first_secen(){
  setLayout(new BorderLayout());
  GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
  Canvas3D canvas3d = new Canvas3D(config);
  add(canvas3d,BorderLayout.CENTER);
  button = new Button("change");
  add(button,BorderLayout.WEST);
  button.addActionListener(this);
  scene = createSceneGraph();
  universe = new SimpleUniverse(canvas3d);
  universe.getViewingPlatform().setNominalViewingTransform();
  universe.addBranchGraph(scene);
 }
 
 public static void main(String[] args){
  new MainFrame(new first_secen(), 800,600);
 }
 @Override
 public void actionPerformed(ActionEvent arg0) {
  if (arg0.getSource() == button) {
   Transform3D t1 = new Transform3D();
   t1.set(0.2,new Vector3d(-.5,0.0,0.0));
   TransformGroup objTrans1 = new TransformGroup(t1);
   exten = new BranchGroup();
   objTrans1.addChild(new ColorCube(0.5));
   exten.addChild(objTrans1);
   scene.addChild(exten);
  }
  
 }
}

 

效果图如下:

 


TAG:

 

评分:0

我来说两句

Open Toolbar