Platinum Partner
java,3d,javafx

How Do I Use Java3D with JavaFX?

What is Java3D? It is an API enabling applications that call functions of the OpenGL and DirectX drivers to create and render 3D scenes on the screen. How can we use that from JavaFX?You can download the free API at the Java3D project page: https://java3d.dev.java.net.

Example

The following files will appear after successful installation of the distribution kit:

C:\Program Files\Java\Java3D\1.5.1\lib\ext\j3dcore.jar
C:\Program Files\Java\Java3D\1.5.1\lib\ext\j3dutils.jar
C:\Program Files\Java\Java3D\1.5.1\lib\ext\vecmath.jar

These files should be included in the NetBeans project.

The API enables you to render a 3D scene on a Canvas3D class panel derived from the java.awt.Panel class.

In this demo, you will create a class inherited from Panel, and its createComponent method will return a javax.swing.JPanel object with the embedded Canvas3d object. You can test this approach on the rotating cub demo code available at the following page: https://j3d-webstart.dev.java.net/test.

Add a coulple of buttons to alter rotation direction. The figure below shows the final form.

Since scene rendering is executed by the co-processor, the speed of the cub rotation does not influence rendering quality.

Source

package javafx3d;

import javafx.ui.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.lang.Math;
import java.awt.BorderLayout;
import javax.swing.JPanel;

class RenderPanel extends Panel {
public attribute rotator:RotationInterpolator;
}
operation RenderPanel.createComponent() {
var config = SimpleUniverse.getPreferredConfiguration();
var c = new Canvas3D(config);
var univ = new SimpleUniverse(c);
univ.getViewingPlatform().setNominalViewingTransform();
univ.getViewer().getView().setMinimumFrameCycleTime(5);
var objRoot = new BranchGroup();
var objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.addChild(objTrans);
objTrans.addChild(new ColorCube(0.4));
var rotationAlpha = new Alpha( -1, 999);
rotator = new RotationInterpolator(rotationAlpha, objTrans);
var t = new Transform3D();
t.rotX(2);
var axis = new Transform3D(t);
rotator.setTransformAxis(axis);
var bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
rotator.setSchedulingBounds(bounds);
objRoot.addChild(rotator);
objRoot.compile();
univ.addBranchGraph(objRoot);
var panel=new JPanel();
panel.setLayout(new BorderLayout());
panel.add(c, BorderLayout.CENTER);
return panel;
}
var renderPanel=new RenderPanel();
Frame {
visible: true
screenx: 50
screeny: 100
width: 600
height: 400
content: BorderPanel {
border: EmptyBorder {left: 16 top: 16 right: 16 bottom: 16}
center: BorderPanel {
background: new Color(1,1,1,1)
}
center: renderPanel
right: BorderPanel {
border: EmptyBorder {left: 16 top: 0 right: 0 bottom: 0}
top: GridPanel {
rows: 4
columns: 1
cells: [
Button {
text: "Horizontal"
action: operation(){
var t = new Transform3D();
t.rotX(2);
var axis = new Transform3D(t);
renderPanel.rotator.setTransformAxis(axis);
}
}
,Button {
text: "Vertical"
action: operation(){
var t = new Transform3D();
t.rotZ(2);
var axis = new Transform3D(t);
renderPanel.rotator.setTransformAxis(axis);
}
}
]
}
}
}
}
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}