-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava3D.java
More file actions
90 lines (71 loc) · 2.88 KB
/
Copy pathJava3D.java
File metadata and controls
90 lines (71 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package java3d;
import javax.media.j3d.*;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseWheelZoom;
public class Java3D extends javax.swing.JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) throws Exception {
Java3D java3d = new Java3D();
java3d.setVisible(true);
}
public Java3D() {
super("Java 3D");
Canvas3D canvas3d = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
add(canvas3d);
BranchGroup bgrRoot = createRootGraph();
bgrRoot.compile();
SimpleUniverse smpU = new SimpleUniverse(canvas3d);
smpU.getViewingPlatform().setNominalViewingTransform();
smpU.addBranchGraph(bgrRoot);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(800, 600);
setResizable(false);
setLocationRelativeTo(null);
}
private BranchGroup createRootGraph() {
BranchGroup bgrRoot = new BranchGroup();
bgrRoot.addChild(createGeometry());
bgrRoot.addChild(createBackground());
return bgrRoot;
}
private TransformGroup createGeometry() {
TransformGroup tgrGeo = new TransformGroup();
int flags = Primitive.GENERATE_NORMALS + Primitive.GENERATE_TEXTURE_COORDS;
Cylinder cylinder = new Cylinder(0.2f, 0.9f, flags, createAppearence());
tgrGeo.addChild(cylinder);
tgrGeo.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
tgrGeo.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
//rotar
MouseRotate msrGeo = new MouseRotate();
msrGeo.setTransformGroup(tgrGeo);
msrGeo.setSchedulingBounds(new BoundingSphere());
tgrGeo.addChild(msrGeo);
//zoom
MouseWheelZoom mhzGeom = new MouseWheelZoom();
mhzGeom.setTransformGroup(tgrGeo);
mhzGeom.setSchedulingBounds(new BoundingSphere());
tgrGeo.addChild(mhzGeom);
return tgrGeo;
}
private TransformGroup createBackground() {
TransformGroup tgrFondo = new TransformGroup();
Background bkgFondo = new Background();
bkgFondo.setColor(0.0f, 0.4f, 0.6f);
bkgFondo.setApplicationBounds(new BoundingBox());
tgrFondo.addChild(bkgFondo);
return tgrFondo;
}
private Appearance createAppearence() {
TextureLoader loader = new TextureLoader(getClass().getResource("img/textura.jpg"),
"INTENSITY", new java.awt.Container());
Texture texture = loader.getTexture();
Appearance apariencia = new Appearance();
apariencia.setTexture(texture);
apariencia.setMaterial(new Material());
return apariencia;
}
}