Sergey has posted 7 posts at DZone. You can read more from them at their website. View Full User Profile

How Do I Use Java3D with JavaFX?

02.29.2008
| 15896 views |
  • submit to reddit

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);
}
}
]
}
}
}
}
1
Your rating: None Average: 1 (1 vote)
Published at DZone with permission of its author, Sergey Surikov.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Fran Aviles replied on Fri, 2008/02/29 - 1:30pm

Just like in pure Java

Sergey Surikov replied on Fri, 2008/02/29 - 2:19pm

yes.

 

 

Andres Almiray replied on Fri, 2008/02/29 - 2:50pm in response to: Sergey Surikov

Which begs the question: what can JavaFX offer to 2D&3D mixed together? isn't there something new under the Sun?

I hope we would see some jfx 3d extensions in the future that would make creating 3d scenes much easier, this example looks like a C&P from a regular Java3D example. 

Dmitri Trembovetski replied on Fri, 2008/02/29 - 4:04pm in response to: Andres Almiray

 

> I hope we would see some jfx 3d extensions

 

Some of this is in the works already, a 3d scenegraph: 

http://forums.java.net/jive/thread.jspa?threadID=36022&tstart=60

> As many of you are aware, Sun's emphasis on client technologies has led to the creation of JavaFX -- a platform for creating rich content applications for mobile, set-top, and desktop devices. The majority of our current effort is focused on building out the 3D support for JavaFX.

Dmitri

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.