Java3d三维图形库使用-创新互联

前言

Java 3D是Java语言在三维图形领域的扩展,是一组应用编程接口(API)。利用Java 3D提供的API,可以编写出基于网页的三维动画、各种计算机辅助教学软件和三维游戏。如果对java语言比较熟悉使用Java 3D开发三维图形是很适合的。

站在用户的角度思考问题,与客户深入沟通,找到刚察网站设计与刚察网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:做网站、成都做网站、企业官网、英文网站、手机端网站、网站推广、国际域名空间、虚拟空间、企业邮箱。业务覆盖刚察地区。java3d下载安装

下载:

oracle官网:Java 3D 1.5.1

csdn下载:java 3d(window-64位)1.5.1下载

jDK依赖导入:

复制java 3d dll文件到java.exe的bin目录:

需复制如下dll文件:

复制到jdk的bin目录:

idea导入java 3d依赖:

maven项目在在依赖下右击,选择以下选项:

然后点击+添加java 3d的如下3个依赖:

java3d简单使用

代码示例:

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.Box;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.applet.Applet;
import java.awt.BorderLayout;
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.Material;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

public class Lantern extends Applet {
  public BranchGroup createBranchGroup()
  {
    //定义BranchGroup
    BranchGroup BranchGroupRoot = new BranchGroup ();

    //创建球心在坐标系原点球形范围
    BoundingSphere bounds = new BoundingSphere(new Point3d( 0.0,0.0,0.0),100.0);

    //定义背景颜色
    Color3f bgColor=new Color3f(.0f,.0f,.0f);
    Background bg=new Background(bgColor);
    bg.setApplicationBounds(bounds);
    BranchGroupRoot.addChild(bg);

    //定义平行光、颜色、照射方向与作用范围
    Color3f directionColor=new Color3f(1.f,1.f,1.f);
    Vector3f vec=new Vector3f(1.0f,-1.0f,-1.0f);
    DirectionalLight directionalLight= new DirectionalLight(directionColor,vec);
    directionalLight.setInfluencingBounds(bounds);
    BranchGroupRoot.addChild(directionalLight);

    //定义总的TransformGroup:transformgroup
    TransformGroup transformgroup=new TransformGroup();

    //设置对该TransformGroup的读写能力
    transformgroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    transformgroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

    //将该TransformGroup加入到BranchGroupRoot中
    BranchGroupRoot.addChild(transformgroup);

    //定义鼠标对场景的旋转、平移与放大功能
    MouseRotate mouserotate=new MouseRotate();
    mouserotate.setTransformGroup(transformgroup);
    BranchGroupRoot.addChild(mouserotate);
    mouserotate.setSchedulingBounds(bounds);

    MouseZoom mousezoom=new MouseZoom();
    mousezoom.setSchedulingBounds(bounds);
    BranchGroupRoot.addChild(mousezoom);
    mousezoom.setSchedulingBounds(bounds);

    MouseTranslate mousetranslate=new MouseTranslate();
    mousetranslate.setTransformGroup(transformgroup);
    BranchGroupRoot.addChild(mousetranslate);
    mousetranslate.setSchedulingBounds(bounds);

    //定义两个三维型体的外观
    Appearance app1=new Appearance();
    Material material1=new Material();
    material1.setDiffuseColor(new Color3f(0.7f,0.3f,0.0f));
    app1.setMaterial(material1);

    Appearance app2=new Appearance();
    Material material2=new Material();
    material2.setDiffuseColor(new Color3f(0.0f,1.0f,0.0f));
    app2.setMaterial(material2);

    //定义一个球体于一个长方体的大小、外观属性与坐标变换,并定义相应的TransformGroup:ta1、ta2
    TransformGroup tg1=new TransformGroup();
    tg1.addChild(new Sphere(0.3f,app1));
    Transform3D t=new Transform3D();
    t.setTranslation(new Vector3f(0.f,-0.425f,0.f));
    TransformGroup tg2=new TransformGroup(t);
    tg2.addChild(new Box(0.5f,0.05f,0.5f,app2));

    //将定义好的两个TransformGroup(tg1、tg2)加入到总的transformgroup
    transformgroup.addChild(tg1);
    transformgroup.addChild(tg2);

    //对BranchGroupRoot预编译
    BranchGroupRoot.compile();

    //通过方法名返回BranchGroupRoot
    return BranchGroupRoot;
  }
  public Lantern()
  {
    //设置显示界面的相关参数
    setLayout(new BorderLayout());
    //创建投影平面Canvas3D
    GraphicsConfiguration gc= SimpleUniverse.getPreferredConfiguration();
    Canvas3D c=new Canvas3D(gc);
    //将投影平面上的图像显示在显示平面的中间
    add("Center",c);
    //设置SimpleUniverse,由系统选择视点在z轴的正向,观察方向沿z轴反向
    BranchGroup BranchGroupScene=createBranchGroup();
    SimpleUniverse u=new SimpleUniverse(c);
    u.getViewingPlatform().setNominalViewingTransform();
    //将BranchGroup:BranchGroupScene加入到SimpleUniverse:u中
    u.addBranchGraph(BranchGroupScene);
  }

  public static void main(String[] args)
  {//通过MainFrame显示图像

    new MainFrame(new Lantern(),300,300);

  }


}

运行效果·:

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


文章名称:Java3d三维图形库使用-创新互联
分享地址:http://myzitong.com/article/dpgpss.html