java3D学习(1)

不善言谈直接上码

import java.applet.Applet;//可以插入html
import java.awt.BorderLayout;//窗口采用BorderLayout方式布局
import com.sun.j3d.utils.applet.MainFrame;//application
import com.sun.j3d.utils.geometry.Sphere;//调用生成Box的Utility
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.universe.*;//观测位置的设置
import javax.media.j3d.*;//核心类
import javax.vecmath.*;//矢量计算
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.behaviors.mouse.*;


public class Hello extends Applet {
  public BranchGroup createSceneGraph() {


   // 创建一个用来包含对象的数据结构
   BranchGroup objRoot = 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);
        objRoot.addChild(bg);

TransformGroup trans = new TransformGroup();
     trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objRoot.addChild(trans);
   
    MouseRotate behavior = new MouseRotate();
    behavior.setTransformGroup(trans);
    objRoot.addChild(behavior);
    behavior.setSchedulingBounds(bounds);


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


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

// 创建一束红色光
Color3f light1Color = new Color3f(1.8f, 0.1f, 0.1f);
// 设置光线的颜色
 
Vector3f light1Direction  = new Vector3f(4.0f, -7.0f, -12.0f);
// 设置光线的方向
   DirectionalLight light1
     = new DirectionalLight(light1Color, light1Direction);
// 指定颜色和方向,产生单向光源
light1.setInfluencingBounds(bounds);
// 把光线的作用范围加入光源中
objRoot.addChild(light1);
// 将光源加入objRoot组
// 创建一个球并把它加入到objRoot中
Sphere sphere = new Sphere(0.5f);
// 小球的半径为0.5米
  trans.addChild(sphere);


         return objRoot;
  }
  
  public Hello() {
    setLayout(new BorderLayout());
    // 设置一个BorderLayout
   GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration(); 
    Canvas3D c=new Canvas3D(config);
    // 构造一个图形环境
    add("Center",c);
    // 将图形环境放入BorderLayout的中心位置
    BranchGroup scene=createSceneGraph();
    // 生成一个场景图分支,在其中定义分支的形体、材质、灯光等信息
    SimpleUniverse u=new SimpleUniverse(c);
    // 生成虚拟空间
    u.getViewingPlatform().setNominalViewingTransform();
    // 设置观察者的位置为缺省位置
    u.addBranchGraph(scene);
    // 把场景放入虚拟空间中
  }
  
  public static void main(String[] args) {
    new MainFrame(new Hello(),300,300);
  // 生成一个MainFrame对象,MainFrame是Java 3D提供的一个实用工具
  // 如此处理就使程序既可以按Application的方式在控制台上用“Java Hello”运行
  // 也能作为Applet在被浏览的网页中运行
  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值