1 | package com.framsticks.visualization; |
---|
2 | |
---|
3 | import com.sun.j3d.utils.universe.*; |
---|
4 | import com.sun.j3d.utils.geometry.ColorCube; |
---|
5 | import org.apache.logging.log4j.Logger; |
---|
6 | import org.apache.logging.log4j.LogManager; |
---|
7 | |
---|
8 | import javax.media.j3d.*; |
---|
9 | import javax.swing.*; |
---|
10 | import javax.vecmath.*; |
---|
11 | import java.awt.*; |
---|
12 | import java.awt.event.ComponentEvent; |
---|
13 | import java.awt.event.ComponentListener; |
---|
14 | |
---|
15 | /** |
---|
16 | * Author: Piotr Śniegowski |
---|
17 | */ |
---|
18 | @SuppressWarnings("serial") |
---|
19 | public class ViewerTest extends JPanel { |
---|
20 | private static final Logger log = LogManager.getLogger(ViewerTest.class.getName()); |
---|
21 | |
---|
22 | private SimpleUniverse univ = null; |
---|
23 | private BranchGroup scene = null; |
---|
24 | |
---|
25 | public BranchGroup createSceneGraph() { |
---|
26 | // Create the root of the branch graph |
---|
27 | BranchGroup objRoot = new BranchGroup(); |
---|
28 | |
---|
29 | // Create the TransformGroup node and initialize it to the |
---|
30 | // identity. Enable the TRANSFORM_WRITE capability so that |
---|
31 | // our behavior code can modify it at run time. Add it to |
---|
32 | // the root of the subgraph. |
---|
33 | TransformGroup objTrans = new TransformGroup(); |
---|
34 | objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); |
---|
35 | objRoot.addChild(objTrans); |
---|
36 | |
---|
37 | // Create a simple Shape3D node; add it to the scene graph. |
---|
38 | objTrans.addChild(new ColorCube(0.4)); |
---|
39 | |
---|
40 | // Create a new Behavior object that will perform the |
---|
41 | // desired operation on the specified transform and add |
---|
42 | // it into the scene graph. |
---|
43 | Transform3D yAxis = new Transform3D(); |
---|
44 | Alpha rotationAlpha = new Alpha(-1, 4000); |
---|
45 | |
---|
46 | RotationInterpolator rotator = |
---|
47 | new RotationInterpolator(rotationAlpha, objTrans, yAxis, |
---|
48 | 0.0f, (float) Math.PI * 2.0f); |
---|
49 | BoundingSphere bounds = |
---|
50 | new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); |
---|
51 | rotator.setSchedulingBounds(bounds); |
---|
52 | objRoot.addChild(rotator); |
---|
53 | |
---|
54 | // Have Java 3D perform optimizations on this scene graph. |
---|
55 | objRoot.compile(); |
---|
56 | |
---|
57 | return objRoot; |
---|
58 | } |
---|
59 | |
---|
60 | private Canvas3D createUniverse() { |
---|
61 | // Get the preferred graphics configuration for the default screen |
---|
62 | GraphicsConfiguration config = |
---|
63 | SimpleUniverse.getPreferredConfiguration(); |
---|
64 | |
---|
65 | // Create a Canvas3D using the preferred configuration |
---|
66 | final Canvas3D c = new Canvas3D(config) { |
---|
67 | @Override |
---|
68 | public void paint(Graphics graphics) { |
---|
69 | super.paint(graphics); |
---|
70 | Toolkit.getDefaultToolkit().sync(); |
---|
71 | } |
---|
72 | |
---|
73 | @Override |
---|
74 | public void postRender() { |
---|
75 | super.postRender(); |
---|
76 | //log.info("postRender()"); |
---|
77 | } |
---|
78 | @Override |
---|
79 | public void postSwap() { |
---|
80 | super.postSwap(); |
---|
81 | //log.info("postSwap()"); |
---|
82 | } |
---|
83 | }; |
---|
84 | |
---|
85 | |
---|
86 | c.addComponentListener(new ComponentListener() { |
---|
87 | @Override |
---|
88 | public void componentResized(ComponentEvent componentEvent) { |
---|
89 | } |
---|
90 | |
---|
91 | @Override |
---|
92 | public void componentMoved(ComponentEvent componentEvent) { |
---|
93 | } |
---|
94 | |
---|
95 | @Override |
---|
96 | public void componentShown(ComponentEvent componentEvent) { |
---|
97 | log.info("shown"); |
---|
98 | c.startRenderer(); |
---|
99 | } |
---|
100 | |
---|
101 | @Override |
---|
102 | public void componentHidden(ComponentEvent componentEvent) { |
---|
103 | log.info("hidden"); |
---|
104 | c.stopRenderer(); |
---|
105 | } |
---|
106 | }); |
---|
107 | |
---|
108 | // Create simple universe with view branch |
---|
109 | univ = new SimpleUniverse(c); |
---|
110 | |
---|
111 | // This will move the ViewPlatform back a bit so the |
---|
112 | // objects in the scene can be viewed. |
---|
113 | univ.getViewingPlatform().setNominalViewingTransform(); |
---|
114 | |
---|
115 | // Ensure at least 5 msec per frame (i.e., < 200Hz) |
---|
116 | univ.getViewer().getView().setMinimumFrameCycleTime(16); |
---|
117 | |
---|
118 | return c; |
---|
119 | } |
---|
120 | |
---|
121 | public ViewerTest() { |
---|
122 | |
---|
123 | this.setLayout(new java.awt.BorderLayout()); |
---|
124 | |
---|
125 | this.setPreferredSize(new java.awt.Dimension(250, 250)); |
---|
126 | |
---|
127 | Canvas3D c = createUniverse(); |
---|
128 | c.setSize(100, 100); |
---|
129 | this.add(c, java.awt.BorderLayout.CENTER); |
---|
130 | |
---|
131 | // Create the content branch and add it to the universe |
---|
132 | scene = createSceneGraph(); |
---|
133 | univ.addBranchGraph(scene); |
---|
134 | } |
---|
135 | } |
---|