Over a million developers have joined DZone.

How Do I Use Java3D with JavaFX?

DZone's Guide to

How Do I Use Java3D with JavaFX?

· Java Zone ·
Free Resource

Delivering modern software? Atomist automates your software delivery experience.

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.


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.


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);
var objRoot = new BranchGroup();
var objTrans = new TransformGroup();
objTrans.addChild(new ColorCube(0.4));
var rotationAlpha = new Alpha( -1, 999);
rotator = new RotationInterpolator(rotationAlpha, objTrans);
var t = new Transform3D();
var axis = new Transform3D(t);
var bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
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();
var axis = new Transform3D(t);
,Button {
text: "Vertical"
action: operation(){
var t = new Transform3D();
var axis = new Transform3D(t);

Start automating your delivery right there on your own laptop, today! Get the open source Atomist Software Delivery Machine.


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}