Java3D, is relatively old technology, Java 8 Oracle JavaFX support Java3D. For information, refer to the Oracle official website <JDK8 has not officially released Early Access version of http://jdk8.java.net>
Environment to build a Java3D development environment, JDK download see the Oracle official website. After installation, find the Java3D driver package in the lib file under the installation directory, add your own project
Java3D Implementation of simple geometric space graphics description:
Instance application: Create a 3D line
Copy codeThe Code is as follows: package com. java3d. dennist. study;
Import javax. media. j3d. Appearance;
Import javax. media. j3d. LineArray;
Import javax. media. j3d. LineAttributes;
Import javax. media. j3d. Shape3D;
/**
*
*
* @ Version: 1.1
*
* @ Author: su Ruian <a href = "mailto: DennisIT@163.com"> send mail </a>
*
* @ Since: 1.0 Creation Time: 02:57:47
*
* TODO:
*
*/
Public class LineShape extends Shape3D {
// Fixed point coordinates of a straight line
Private float vert [] = {
. 5f, 0.5f, 0.0f,-0.5f, 0.5f, 0.0f,
. 3f, 0.0f, 0.0f,-0.3f, 0.0f, 0.0f,
-0.5f,-0.5f, 0.0f, 0.5f,-0.5f, 0.0f };
// The color of each specified point
Private float color [] = {
. 0f, 0.5f, 1.0f, 0.0f, 0.5f, 1.0f,
. 0f, 0.8f, 2.0f, 1.0f, 0.0f, 0.3f,
. 0f, 1.0f, 0.3f, 0.3f, 0.8f, 0.0f };
Public LineShape (){
// Create a line array object
LineArray line = new LineArray (6, LineArray. COORDINATES | LineArray. COLOR_3 );
// Set the coordinate array of the line Object
Line. setCoordinates (0, vert );
// Sets the color array of the line Object
Line. setColors (0, color );
// Create a line property object
LineAttributes linea = new LineAttributes ();
// Set the line width
Linea. setLineWidth (10.0f );
// Set the rendering effect of a straight line
Linea. setLineAntialiasingEnable (true );
Appearance app = new Appearance ();
App. setLineAttributes (linea );
This. setGeometry (line );
This. setAppearance (app );
}
}
Place 3d straight lines in the sceneCopy codeThe Code is as follows: package com. java3d. dennist. study;
Import javax. media. j3d. BoundingSphere;
Import javax. media. j3d. BranchGroup;
Import javax. media. j3d. DirectionalLight;
Import javax. media. j3d. Shape3D;
Import javax. vecmath. Color3f;
Import javax. vecmath. Point3d;
Import javax. vecmath. Vector3f;
Import com. sun. j3d. utils. universe. SimpleUniverse;
/**
*
*
* @ Version: 1.1
*
* @ Author: su Ruian <a href = "mailto: DennisIT@163.com"> send mail </a>
*
* @ Since: 1.0 Creation Time: 02:50:08
*
* TODO:
*
*/
Public class Line3DShape {
Public Line3DShape (){
// Construct space and objects
// Create a virtual space
SimpleUniverse universe = new SimpleUniverse ();
// Create a data structure that contains objects
BranchGroup group = new BranchGroup ();
// Create a linear shape object and add it to the group
Shape3D shape = new LineShape ();
Group. addChild (shape );
// Lighting Construction
Color3f light1Color = new Color3f (1.8f, 0.1f, 0.1f );
// Set the light color
BoundingSphere bounds = new BoundingSphere (new Point3d (0.0, 0.0, 0.0), 100.0 );
// Set the scope of light
Vector3f light1Direction = new Vector3f (4.0f,-7.0f,-12.0f );
// Set the direction of light
DirectionalLight light1 = new DirectionalLight (light1Color, light1Direction );
// Specify the color and direction to generate a unidirectional Light Source
Light1.setInfluencingBounds (bounds );
// Add the scope of light to the light source
Group. addChild (light1 );
// Add the light source to the group
// Place the observation point
Universe. getViewingPlatform (). setNominalViewingTransform ();
// Add the group to the virtual space
Universe. addBranchGraph (group );
}
Public static void main (String [] args ){
New Line3DShape ();
}
}
Running effect:
Other shapes
Cone: cone Cone = new Cone (. 5f, 1.0f, 1, app) // app is the appearance Parameter
Sphere: sphere Sphere = new Sphere (. 5f, app); // app is the appearance Parameter
Cylinder cylinder = new Cylinder (. 5f, 1.0f );
Example application: Java3D implements a spatial vertebral body
Copy codeThe Code is as follows: package com. java3d. dennist. study;
Import javax. media. j3d. BoundingSphere;
Import javax. media. j3d. BranchGroup;
Import javax. media. j3d. DirectionalLight;
Import javax. vecmath. Color3f;
Import javax. vecmath. Point3d;
Import javax. vecmath. Vector3f;
Import com. sun. j3d. utils. geometry. Cone;
Import com. sun. j3d. utils. universe. SimpleUniverse;
/**
*
*
* @ Version: 1.1
*
* @ Author: su Ruian <a href = "mailto: DennisIT@163.com"> send mail </a>
*
* @ Since: 1.0 Creation Time: 02:50:08
*
* TODO: display various 3D shapes in Java3D
*
*/
Public class Java3DShape {
Public Java3DShape (){
// Construct space and objects
// Create a virtual space
SimpleUniverse universe = new SimpleUniverse ();
// Create a data structure that contains objects
BranchGroup group = new BranchGroup ();
// Create a vertebral body and add it to the group
Cone cone = new Cone (. 5f, 1.0f, 1, null); // Cone
Group. addChild (cone );
// Lighting Construction
Color3f light1Color = new Color3f (1.8f, 0.1f, 0.1f );
// Set the light color
BoundingSphere bounds = new BoundingSphere (new Point3d (0.0, 0.0, 0.0), 100.0 );
// Set the scope of light
Vector3f light1Direction = new Vector3f (4.0f,-7.0f,-12.0f );
// Set the direction of light
DirectionalLight light1 = new DirectionalLight (light1Color, light1Direction );
// Specify the color and direction to generate a unidirectional Light Source
Light1.setInfluencingBounds (bounds );
// Add the scope of light to the light source
Group. addChild (light1 );
// Add the light source to the group
// Place the observation point
Universe. getViewingPlatform (). setNominalViewingTransform ();
// Add the group to the virtual space
Universe. addBranchGraph (group );
}
Public static void main (String [] args ){
New Java3DShape ();
}
}
:
Example application: Java3D implements a space sphere
Copy codeThe Code is as follows: package com. java3d. dennist. study;
Import javax. media. j3d. BoundingSphere;
Import javax. media. j3d. BranchGroup;
Import javax. media. j3d. DirectionalLight;
Import javax. vecmath. Color3f;
Import javax. vecmath. Point3d;
Import javax. vecmath. Vector3f;
Import com. sun. j3d. utils. geometry. Sphere;
Import com. sun. j3d. utils. universe. SimpleUniverse;
/**
*
*
* @ Version: 1.1
*
* @ Author: su Ruian <a href = "mailto: DennisIT@163.com"> send mail </a>
*
* @ Since: 1.0 Creation Time: 02:50:08
*
* TODO: display various 3D shapes in Java3D
*
*/
Public class Java3DShape {
Public Java3DShape (){
// Construct space and objects
// Create a virtual space
SimpleUniverse universe = new SimpleUniverse ();
// Create a data structure that contains objects
BranchGroup group = new BranchGroup ();
// Create a ball and add it to the group
Sphere sphere = new Sphere (0.5f); // The ball radius is 0.5 meters.
Group. addChild (sphere );
// Lighting Construction
Color3f light1Color = new Color3f (1.8f, 0.1f, 0.1f );
// Set the light color
BoundingSphere bounds = new BoundingSphere (new Point3d (0.0, 0.0, 0.0), 100.0 );
// Set the scope of light
Vector3f light1Direction = new Vector3f (4.0f,-7.0f,-12.0f );
// Set the direction of light
DirectionalLight light1 = new DirectionalLight (light1Color, light1Direction );
// Specify the color and direction to generate a unidirectional Light Source
Light1.setInfluencingBounds (bounds );
// Add the scope of light to the light source
Group. addChild (light1 );
// Add the light source to the group
// Place the observation point
Universe. getViewingPlatform (). setNominalViewingTransform ();
// Add the group to the virtual space
Universe. addBranchGraph (group );
}
Public static void main (String [] args ){
New Java3DShape ();
}
}
:
Example application: Java3D implement space Cylinder
Copy codeThe Code is as follows: package com. java3d. dennist. study;
Import javax. media. j3d. BoundingSphere;
Import javax. media. j3d. BranchGroup;
Import javax. media. j3d. DirectionalLight;
Import javax. vecmath. Color3f;
Import javax. vecmath. Point3d;
Import javax. vecmath. Vector3f;
Import com. sun. j3d. utils. geometry. Cylinder;
Import com. sun. j3d. utils. universe. SimpleUniverse;
/**
*
*
* @ Version: 1.1
*
* @ Author: su Ruian <a href = "mailto: DennisIT@163.com"> send mail </a>
*
* @ Since: 1.0 Creation Time: 02:50:08
*
* TODO: display various 3D shapes in Java3D
*
*/
Public class Java3DShape {
Public Java3DShape (){
// Construct space and objects
// Create a virtual space
SimpleUniverse universe = new SimpleUniverse ();
// Create a data structure that contains objects
BranchGroup group = new BranchGroup ();
// Create a cylindrical shape and add it to the group
Cylinder cylinder = new Cylinder (. 5f, 1.0f); // cylindrical
Group. addChild (cylinder );
// Lighting Construction
Color3f light1Color = new Color3f (1.8f, 0.1f, 0.1f );
// Set the light color
BoundingSphere bounds = new BoundingSphere (new Point3d (0.0, 0.0, 0.0), 100.0 );
// Set the scope of light
Vector3f light1Direction = new Vector3f (4.0f,-7.0f,-12.0f );
// Set the direction of light
DirectionalLight light1 = new DirectionalLight (light1Color, light1Direction );
// Specify the color and direction to generate a unidirectional Light Source
Light1.setInfluencingBounds (bounds );
// Add the scope of light to the light source
Group. addChild (light1 );
// Add the light source to the group
// Place the observation point
Universe. getViewingPlatform (). setNominalViewingTransform ();
// Add the group to the virtual space
Universe. addBranchGraph (group );
}
Public static void main (String [] args ){
New Java3DShape ();
}
}
: