Skip to content

Commit 783e8b5

Browse files
committed
Java3D examples
1 parent 000a398 commit 783e8b5

22 files changed

+9423
-0
lines changed

Java3DExamples/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Different source code examples of how to use [Java 3D](http://www.oracle.com/technetwork/articles/javase/index-jsp-138252.html).
2+
To execute path to the library folder must be supplied as following:
3+
4+
```bash
5+
-Djava.library.path=/home/kalayci/Desktop/Projeler/Java3D/src/main/resources/lib/
6+
```

Java3DExamples/pom.xml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>tekrei</groupId>
7+
<artifactId>Java3DExamples</artifactId>
8+
<version>1.0</version>
9+
<properties>
10+
<java.version>1.8</java.version>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
</properties>
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>org.apache.maven.plugins</groupId>
17+
<artifactId>maven-compiler-plugin</artifactId>
18+
<version>3.6.1</version>
19+
<configuration>
20+
<source>${java.version}</source>
21+
<target>${java.version}</target>
22+
</configuration>
23+
</plugin>
24+
<plugin>
25+
<groupId>org.apache.maven.plugins</groupId>
26+
<artifactId>maven-dependency-plugin</artifactId>
27+
<executions>
28+
<execution>
29+
<id>copy-dependencies</id>
30+
<phase>prepare-package</phase>
31+
<goals>
32+
<goal>copy-dependencies</goal>
33+
</goals>
34+
<configuration>
35+
<outputDirectory>${project.build.directory}/lib
36+
</outputDirectory>
37+
</configuration>
38+
</execution>
39+
</executions>
40+
</plugin>
41+
<plugin>
42+
<groupId>org.apache.maven.plugins</groupId>
43+
<artifactId>maven-jar-plugin</artifactId>
44+
<version>2.4</version>
45+
<configuration>
46+
<archive>
47+
<manifest>
48+
<addClasspath>true</addClasspath>
49+
<classpathPrefix>lib/</classpathPrefix>
50+
<mainClass>it.inf.unibz.kaos.onprom.OnpromToolkit
51+
</mainClass>
52+
</manifest>
53+
</archive>
54+
</configuration>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
<repositories>
59+
<repository>
60+
<id>tudelft</id>
61+
<name>TU Delft Repository</name>
62+
<url>http://simulation.tudelft.nl/maven/</url>
63+
<layout>default</layout>
64+
</repository>
65+
</repositories>
66+
<dependencies>
67+
<dependency>
68+
<groupId>java3d</groupId>
69+
<artifactId>j3d-core</artifactId>
70+
<version>1.5.1</version>
71+
</dependency>
72+
<dependency>
73+
<groupId>java3d</groupId>
74+
<artifactId>j3d-core-utils</artifactId>
75+
<version>1.5.1</version>
76+
</dependency>
77+
</dependencies>
78+
79+
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package j3d;
2+
3+
import j3d.panels.*;
4+
5+
import javax.swing.*;
6+
import java.awt.*;
7+
8+
/**
9+
* Created by kalayci on 06-May-17
10+
*/
11+
public class DisplayFrame extends JFrame {
12+
13+
private DisplayFrame() {
14+
setLayout(new BorderLayout());
15+
JTabbedPane tabs = new JTabbedPane();
16+
tabs.addTab("Cube", new InteractiveCubeExample());
17+
tabs.addTab("Cone", new ConeExample());
18+
tabs.addTab("Sphere", new SphereExample());
19+
tabs.addTab("Colored Box", new ColoredBoxExample());
20+
tabs.addTab("Cylinder", new CylinderExample());
21+
tabs.addTab("Texture", new TextureExample());
22+
tabs.addTab("Texture 2", new TexturedBox());
23+
tabs.addTab("3D Text", new Text3DExample());
24+
tabs.addTab("Animated Text", new TextAnimationExample());
25+
tabs.addTab("Lightning", new LightningExample());
26+
tabs.addTab("Loading Object", new ObjectLoadingExample());
27+
28+
add(tabs, BorderLayout.CENTER);
29+
setSize(800, 600);
30+
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
31+
setVisible(true);
32+
}
33+
34+
public static void main(String[] args) {
35+
new DisplayFrame();
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package j3d.panels;
2+
3+
import com.sun.j3d.utils.geometry.Box;
4+
5+
import javax.media.j3d.*;
6+
import javax.vecmath.Color3f;
7+
import javax.vecmath.Vector3f;
8+
9+
public class ColoredBoxExample extends Java3DPanel {
10+
11+
public ColoredBoxExample() {
12+
super();
13+
}
14+
15+
@Override
16+
void buildScene(TransformGroup transformGroup) {
17+
Background background = new Background(new Color3f(.905f, .905f, 0.95f));
18+
background.setApplicationBounds(BOUNDS);
19+
transformGroup.addChild(background);
20+
21+
Appearance appearance = new Appearance();
22+
ColoringAttributes ca = new ColoringAttributes();
23+
ca.setColor(1.0f, 0.0f, 1.0f);
24+
ca.setShadeModel(ColoringAttributes.SHADE_GOURAUD);
25+
appearance.setColoringAttributes(ca);
26+
transformGroup.addChild(new Box(0.6f, 0.5f, 0.4f, appearance));
27+
28+
Transform3D transform3D = new Transform3D();
29+
transform3D.setTranslation(new Vector3f(0.3f, 0.3f, 0.3f));
30+
transform3D.setScale(0.75);
31+
Transform3D t = new Transform3D();
32+
t.rotY(Math.PI / 4.0d);
33+
transform3D.mul(t);
34+
transformGroup.setTransform(transform3D);
35+
}
36+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package j3d.panels;
2+
3+
import com.sun.j3d.utils.geometry.Cone;
4+
5+
import javax.media.j3d.TransformGroup;
6+
7+
public class ConeExample extends Java3DPanel {
8+
public ConeExample() {
9+
super();
10+
}
11+
12+
@Override
13+
void buildScene(TransformGroup transformGroup) {
14+
transformGroup.addChild(new Cone(0.5f, 0.5f, getDefaultAppearance()));
15+
}
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package j3d.panels;
2+
3+
import com.sun.j3d.utils.geometry.Cylinder;
4+
5+
import javax.media.j3d.TransformGroup;
6+
import java.awt.*;
7+
8+
public class CylinderExample extends Java3DPanel {
9+
10+
public CylinderExample() {
11+
super();
12+
}
13+
14+
@Override
15+
void buildScene(TransformGroup transformGroup) {
16+
transformGroup.addChild(new Cylinder(0.7f, 0.4f, getColorAppearance(Color.ORANGE)));
17+
}
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package j3d.panels;
2+
3+
import com.sun.j3d.utils.geometry.ColorCube;
4+
5+
import javax.media.j3d.TransformGroup;
6+
7+
public class InteractiveCubeExample extends Java3DPanel {
8+
9+
public InteractiveCubeExample() {
10+
super();
11+
}
12+
13+
@Override
14+
void buildScene(TransformGroup transformGroup) {
15+
transformGroup.addChild(new ColorCube(0.5));
16+
}
17+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package j3d.panels;
2+
3+
import com.sun.j3d.loaders.objectfile.ObjectFile;
4+
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
5+
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
6+
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
7+
import com.sun.j3d.utils.image.TextureLoader;
8+
import com.sun.j3d.utils.universe.SimpleUniverse;
9+
10+
import javax.imageio.ImageIO;
11+
import javax.media.j3d.*;
12+
import javax.swing.*;
13+
import javax.vecmath.Color3f;
14+
import javax.vecmath.Point3d;
15+
import javax.vecmath.Point3f;
16+
import javax.vecmath.Vector3f;
17+
import java.awt.*;
18+
19+
/**
20+
* Created by T. E. Kalayci on 06-May-2017
21+
*/
22+
abstract class Java3DPanel extends JPanel {
23+
24+
final static BoundingSphere BOUNDS = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
25+
26+
Java3DPanel() {
27+
super();
28+
setLayout(new BorderLayout());
29+
Canvas3D canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
30+
add(canvas3D);
31+
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
32+
simpleU.getViewingPlatform().setNominalViewingTransform();
33+
try {
34+
BranchGroup branchGroup = new BranchGroup();
35+
TransformGroup transformGroup = getInteractiveTransformGroup();
36+
branchGroup.addChild(transformGroup);
37+
buildScene(transformGroup);
38+
branchGroup.compile();
39+
simpleU.addBranchGraph(branchGroup);
40+
} catch (Exception e) {
41+
e.printStackTrace();
42+
}
43+
setVisible(true);
44+
}
45+
46+
private TransformGroup getInteractiveTransformGroup() {
47+
TransformGroup transformGroup = new TransformGroup();
48+
addInteraction(transformGroup);
49+
return transformGroup;
50+
}
51+
52+
Appearance getTextureAppearance(String path) throws Exception {
53+
Appearance appearance = new Appearance();
54+
TextureLoader pattern = new TextureLoader(ImageIO.read(getClass().getResource(path)), "RGB", this);
55+
appearance.setTexture(pattern.getTexture());
56+
return appearance;
57+
}
58+
59+
Appearance getDefaultAppearance() {
60+
return getColorAppearance(Color.BLUE);
61+
}
62+
63+
Appearance getColorAppearance(Color color) {
64+
Appearance appearance = new Appearance();
65+
Color3f color3f = new Color3f(color);
66+
appearance.setMaterial(new Material(color3f, color3f, color3f, color3f, 100f));
67+
return appearance;
68+
}
69+
70+
private void addMouseRotate(TransformGroup transformGroup) {
71+
MouseRotate mouseRotate = new MouseRotate();
72+
mouseRotate.setTransformGroup(transformGroup);
73+
transformGroup.addChild(mouseRotate);
74+
mouseRotate.setSchedulingBounds(BOUNDS);
75+
}
76+
77+
AmbientLight getAmbientLight(Color color) {
78+
AmbientLight ambientLightNode = new AmbientLight(new Color3f(color));
79+
ambientLightNode.setInfluencingBounds(BOUNDS);
80+
return ambientLightNode;
81+
}
82+
83+
DirectionalLight getDirectionalLight(Color color, Vector3f direction) {
84+
DirectionalLight directionalLight = new DirectionalLight(new Color3f(color), direction);
85+
directionalLight.setInfluencingBounds(BOUNDS);
86+
return directionalLight;
87+
}
88+
89+
PointLight getPointLight(Color color, Point3f position) {
90+
PointLight pointLight = new PointLight();
91+
pointLight.setEnable(true);
92+
pointLight.setColor(new Color3f(color));
93+
pointLight.setPosition(position);
94+
pointLight.setInfluencingBounds(BOUNDS);
95+
return pointLight;
96+
}
97+
98+
Transform3D getTransform(Vector3f translation, double scale, double rotX, double rotY) {
99+
Transform3D transform3D = new Transform3D();
100+
if (translation != null)
101+
transform3D.setTranslation(translation);
102+
if (scale >= 0)
103+
transform3D.setScale(scale);
104+
if (rotX > 0 || rotY > 0) {
105+
Transform3D t = new Transform3D();
106+
if (rotX > 0)
107+
t.rotX(rotX);
108+
if (rotY > 0)
109+
t.rotY(rotY);
110+
transform3D.mul(t);
111+
}
112+
return transform3D;
113+
}
114+
115+
private void addMouseTranslate(TransformGroup transformGroup) {
116+
MouseTranslate mouseTranslate = new MouseTranslate();
117+
mouseTranslate.setTransformGroup(transformGroup);
118+
transformGroup.addChild(mouseTranslate);
119+
mouseTranslate.setSchedulingBounds(BOUNDS);
120+
}
121+
122+
private void addMouseZoom(TransformGroup transformGroup) {
123+
MouseZoom mouseZoom = new MouseZoom();
124+
mouseZoom.setTransformGroup(transformGroup);
125+
transformGroup.addChild(mouseZoom);
126+
mouseZoom.setSchedulingBounds(BOUNDS);
127+
}
128+
129+
private void addInteraction(TransformGroup transformGroup) {
130+
transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
131+
transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
132+
addMouseRotate(transformGroup);
133+
addMouseTranslate(transformGroup);
134+
addMouseZoom(transformGroup);
135+
}
136+
137+
BranchGroup loadObject(String path) {
138+
try {
139+
return new ObjectFile(ObjectFile.RESIZE).load(getClass().getResource(path)).getSceneGroup();
140+
} catch (Exception e) {
141+
e.printStackTrace();
142+
}
143+
return null;
144+
}
145+
146+
Background getColorBackground(Color color) {
147+
Background bgNode = new Background(new Color3f(color));
148+
bgNode.setApplicationBounds(BOUNDS);
149+
return bgNode;
150+
}
151+
152+
abstract void buildScene(TransformGroup transformGroup) throws Exception;
153+
}

0 commit comments

Comments
 (0)