(一)JDK配置
安装包的下载:1.可以去官网:JDK下载,进入官网页面Java Downloads | Oracle,然后点击Download Java。2.搜博客,可能会内含安装包链接。
2.在这个界面可以选择我们要安装的版本
- 双击后面蓝色字体即可下载;这时,会弹出一个页面
- 接下来会跳转到登录页面,有账号可以直接登录,没有的话可以先注册 ,有就正常下载。
6.下载好了安装包后,双击进入安装向导
7.系统默认路径在C盘,点击这里选择更改安装路径,建议不用改,以后好找。
8.然后点击下一步,直到安装成功
9.安装 jre,可以更改 jre 安装路径(过程同上述安装目录的选择) **注意:如果提示需要将 jre 安装在一个空目录下,那自己创建一个目录即可**
10.点击下一步,直至安装成功。
11.配置环境变量(win11为例)
- idea的安装
idea 点击下载
下载community版本即可,如有激活码或其它途径可下载ultimate版本
以上三样安装安装好之后,即可打开idea进行配置
(二)下载、安装 java3D
1.Java 3D 1.5.1的下载与安装
根据自己的系统选取,例如本人用的是windows,则选java3d-1-5-1-windows-amd64.exe(代表64位)这里注意一点,JDK和Java3D的位数需相同,例如JDK用的64位,Java3D库也需64位。
下载完成后,默认安装即可。
(三)配置Ideal
1.配置dll
将java3D里bin目录下的【j3dcore-ogl.dll】复制到jdk的bin目录下,
2.项目中添加java3d的jar包
在IDEA上点击 File------Project Structure------Modules------Dependencies------点击右侧的+号------Library...------New Library------Java------索引到:C:\Program Files\Java\Java3D\1.5.1\lib\ext下,键盘按住Ctrl选择这3个jar包------点击Ok, 会提示修改名字,我这里改为 ext------勾选 在上面新添加的 java3d 依赖名 ext------点击OK------勾选添加 刚刚创建的java3D ------然后点击OK------完工
(四)运行代码
java
import java.applet.Applet;
import java.awt.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import javafx.scene.transform.Transform;
import javax.media.j3d.*;
import javax.vecmath.*;
/**
* Simple Java 3D example program to display a spinning cube.
*/
public class twoprimitivedisplay extends Applet {
public BranchGroup createBranchGraph() {
BranchGroup BranchGroupRoot=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);
BranchGroupRoot.addChild(bg);
//定义平行光、颜色、照射方向与作用
Color3f directionalColor = new Color3f(2.f, 1.f, 1.f);//2-1
Vector3f vec = new Vector3f(-1.f, -1.f, -1.0f);
DirectionalLight directionalLight = new DirectionalLight(directionalColor, vec);
directionalLight.setInfluencingBounds(bounds);
BranchGroupRoot.addChild(directionalLight);
Appearance app1 = new Appearance();
Material material1 = new Material();
material1.setDiffuseColor(new Color3f(1.0f, .0f, 0.0f));
app1.setMaterial(material1);
Appearance app2 = new Appearance();
Material material2 = new Material();
material2.setDiffuseColor(new Color3f(.0f, 1.0f, 0.0f));
app2.setMaterial(material2);
TransformGroup transformgroup = new TransformGroup();
transformgroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);//读坐标值
transformgroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
BranchGroupRoot.addChild(transformgroup);
MouseRotate mouserotate = new MouseRotate();
mouserotate.setTransformGroup(transformgroup);
BranchGroupRoot.addChild(mouserotate);
mouserotate.setSchedulingBounds(bounds);
MouseZoom mousezoom = new MouseZoom();
mousezoom.setTransformGroup(transformgroup);
BranchGroupRoot.addChild(mousezoom);
mousezoom.setSchedulingBounds(bounds);
MouseTranslate mousetranslate = new MouseTranslate();
mousetranslate.setTransformGroup(transformgroup);
BranchGroupRoot.addChild(mousetranslate);
mousetranslate.setSchedulingBounds(bounds);
TransformGroup tg1 = new TransformGroup();
tg1.addChild(new Sphere(0.4f,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.addChild(tg1);
transformgroup.addChild(tg2);
BranchGroupRoot.compile();
return BranchGroupRoot;
}
public twoprimitivedisplay() {
setLayout(new BorderLayout());
GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(gc);
add("Center", c);
BranchGroup BranchGroupScene = createBranchGraph();
SimpleUniverse u = new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(BranchGroupScene);
}
public static void main(String[] args) {
new MainFrame(new twoprimitivedisplay(), 300, 300);
}
}