前言
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);
}
}
运行效果·:文章来源:https://www.toymoban.com/news/detail-519047.html
文章来源地址https://www.toymoban.com/news/detail-519047.html
到了这里,关于Java 3d 三维图形库使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!