We start from a relative simple program:
You can compile and run it. Let's add some more code to it.import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { } public void actionPerformed(ActionEvent e) { } public Example(boolean isApplication, float initOffScreenScale) { } public Example() { } }
Our focus will now be theimport java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { } public void actionPerformed(ActionEvent e) { } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; public void init() { System.out.println("Hello, everybody!"); } }
init()
method.
But even simple additions propagate so we need to stop and take note.import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { } public void actionPerformed(ActionEvent e) { } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); } }
And yet we need to keep going.import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { } public void actionPerformed(ActionEvent e) { } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); } }
Now at least the output finally changed. More changes under way.import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { } public void actionPerformed(ActionEvent e) { } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); add("Center", canvas); u = new SimpleUniverse(canvas); } }
At this point compiling and running are done with the help ofimport java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { } public void actionPerformed(ActionEvent e) { } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; OffScreenCanvas3D offScreenCanvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); add("Center", canvas); u = new SimpleUniverse(canvas); if (isApplication) { offScreenCanvas = new OffScreenCanvas3D(config, true); } } }
J3DJumpStart.jar
.
Now we add code until a new function is needed:javac -classpath .;J3DJumpStart.jar Example.java java -classpath .;J3DJumpStart.jar Example
So we now focus on finishing up the two methods (new and old):import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { } public void actionPerformed(ActionEvent e) { } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; OffScreenCanvas3D offScreenCanvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); add("Center", canvas); u = new SimpleUniverse(canvas); if (isApplication) { offScreenCanvas = new OffScreenCanvas3D(config, true); Screen3D sOn = canvas.getScreen3D(); Screen3D sOff = offScreenCanvas.getScreen3D(); Dimension dim = sOn.getSize(); dim.width *= offScreenScale; dim.height *= offScreenScale; sOff.setSize(dim); sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * offScreenScale); sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * offScreenScale); u.getViewer().getView().addCanvas3D(offScreenCanvas); } BranchGroup scene = createSceneGraph(); } BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); return objRoot; } }
And pretty soon the interface starts to show up:import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; import javax.swing.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { } public void actionPerformed(ActionEvent e) { } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; OffScreenCanvas3D offScreenCanvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); add("Center", canvas); u = new SimpleUniverse(canvas); if (isApplication) { offScreenCanvas = new OffScreenCanvas3D(config, true); Screen3D sOn = canvas.getScreen3D(); Screen3D sOff = offScreenCanvas.getScreen3D(); Dimension dim = sOn.getSize(); dim.width *= offScreenScale; dim.height *= offScreenScale; sOff.setSize(dim); sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * offScreenScale); sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * offScreenScale); u.getViewer().getView().addCanvas3D(offScreenCanvas); } BranchGroup scene = createSceneGraph(); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); view = u.getViewer().getView(); add("East", guiPanel()); } View view; JPanel guiPanel() { JPanel panel = new JPanel(); return panel; } BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); return objRoot; } }
One part of the interface is easy to finish:import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; import javax.swing.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { } public void actionPerformed(ActionEvent e) { } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; OffScreenCanvas3D offScreenCanvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); add("Center", canvas); u = new SimpleUniverse(canvas); if (isApplication) { offScreenCanvas = new OffScreenCanvas3D(config, true); Screen3D sOn = canvas.getScreen3D(); Screen3D sOff = offScreenCanvas.getScreen3D(); Dimension dim = sOn.getSize(); dim.width *= offScreenScale; dim.height *= offScreenScale; sOff.setSize(dim); sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * offScreenScale); sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * offScreenScale); u.getViewer().getView().addCanvas3D(offScreenCanvas); } BranchGroup scene = createSceneGraph(); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); view = u.getViewer().getView(); add("East", guiPanel()); } View view; JPanel guiPanel() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(0, 1)); panel.add(new JLabel("Right Shoulder rotation")); rShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rShoulderRot); rShoulderSliderLabel = new JLabel(Integer.toString(rShoulderRot)); panel.add(rShoulderSlider); return panel; } int rShoulderRot = 0; JSlider rShoulderSlider; JLabel rShoulderSliderLabel; BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); return objRoot; } }
Small changes need to wrap up this direction of development:import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; import javax.swing.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { } public void actionPerformed(ActionEvent e) { } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; OffScreenCanvas3D offScreenCanvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); add("Center", canvas); u = new SimpleUniverse(canvas); if (isApplication) { offScreenCanvas = new OffScreenCanvas3D(config, true); Screen3D sOn = canvas.getScreen3D(); Screen3D sOff = offScreenCanvas.getScreen3D(); Dimension dim = sOn.getSize(); dim.width *= offScreenScale; dim.height *= offScreenScale; sOff.setSize(dim); sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * offScreenScale); sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * offScreenScale); u.getViewer().getView().addCanvas3D(offScreenCanvas); } BranchGroup scene = createSceneGraph(); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); view = u.getViewer().getView(); add("East", guiPanel()); } View view; JPanel guiPanel() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(0, 1)); panel.add(new JLabel("Right Shoulder rotation")); rShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rShoulderRot); rShoulderSliderLabel = new JLabel(Integer.toString(rShoulderRot)); panel.add(rShoulderSlider); // this was for the right shoulder rotation // Human_r_elbow rotation panel.add(new JLabel("Right Elbow rotation")); rElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rElbowRot); rElbowSlider.addChangeListener(this); rElbowSliderLabel = new JLabel(Integer.toString(rElbowRot)); panel.add(rElbowSlider); panel.add(rElbowSliderLabel); // Human_l_shoulder rotation panel.add(new JLabel("Left Shoulder rotation")); lShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lShoulderRot); lShoulderSlider.addChangeListener(this); lShoulderSliderLabel = new JLabel(Integer.toString(lShoulderRot)); panel.add(lShoulderSlider); panel.add(lShoulderSliderLabel); // Human_l_elbow rotation panel.add(new JLabel("Left Elbow rotation")); lElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lElbowRot); lElbowSlider.addChangeListener(this); lElbowSliderLabel = new JLabel(Integer.toString(lElbowRot)); panel.add(lElbowSlider); panel.add(rElbowSliderLabel); return panel; } int rShoulderRot = 0; JSlider rShoulderSlider; JLabel rShoulderSliderLabel; int lShoulderRot = 0; JSlider lShoulderSlider; JLabel lShoulderSliderLabel; int rElbowRot = 0; JSlider rElbowSlider; JLabel rElbowSliderLabel; int lElbowRot = 0; JSlider lElbowSlider; JLabel lElbowSliderLabel; BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); return objRoot; } }
Now it would be nice if we could summarize what we have.import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; import javax.swing.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { } public void actionPerformed(ActionEvent e) { } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; OffScreenCanvas3D offScreenCanvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); add("Center", canvas); u = new SimpleUniverse(canvas); if (isApplication) { offScreenCanvas = new OffScreenCanvas3D(config, true); Screen3D sOn = canvas.getScreen3D(); Screen3D sOff = offScreenCanvas.getScreen3D(); Dimension dim = sOn.getSize(); dim.width *= offScreenScale; dim.height *= offScreenScale; sOff.setSize(dim); sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * offScreenScale); sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * offScreenScale); u.getViewer().getView().addCanvas3D(offScreenCanvas); } BranchGroup scene = createSceneGraph(); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); view = u.getViewer().getView(); add("East", guiPanel()); } View view; JPanel guiPanel() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(0, 1)); panel.add(new JLabel("Right Shoulder rotation")); rShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rShoulderRot); rShoulderSliderLabel = new JLabel(Integer.toString(rShoulderRot)); panel.add(rShoulderSlider); // this was for the right shoulder rotation // Human_r_elbow rotation panel.add(new JLabel("Right Elbow rotation")); rElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rElbowRot); rElbowSlider.addChangeListener(this); rElbowSliderLabel = new JLabel(Integer.toString(rElbowRot)); panel.add(rElbowSlider); panel.add(rElbowSliderLabel); // Human_l_shoulder rotation panel.add(new JLabel("Left Shoulder rotation")); lShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lShoulderRot); lShoulderSlider.addChangeListener(this); lShoulderSliderLabel = new JLabel(Integer.toString(lShoulderRot)); panel.add(lShoulderSlider); panel.add(lShoulderSliderLabel); // Human_l_elbow rotation panel.add(new JLabel("Left Elbow rotation")); lElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lElbowRot); lElbowSlider.addChangeListener(this); lElbowSliderLabel = new JLabel(Integer.toString(lElbowRot)); panel.add(lElbowSlider); panel.add(rElbowSliderLabel); if (isApplication) { JButton snapButton = new JButton(snapImageString); snapButton.setActionCommand(snapImageString); snapButton.addActionListener(this); panel.add(snapButton); } return panel; } String snapImageString = "Snap Image"; int rShoulderRot = 0; JSlider rShoulderSlider; JLabel rShoulderSliderLabel; int lShoulderRot = 0; JSlider lShoulderSlider; JLabel lShoulderSliderLabel; int rElbowRot = 0; JSlider rElbowSlider; JLabel rElbowSliderLabel; int lElbowRot = 0; JSlider lElbowSlider; JLabel lElbowSliderLabel; BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); return objRoot; } public void destroy() { u.removeAllLocales(); } }
Then the next stage takes us to:
It is important to understand the potential embedded in the new code.import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; import javax.swing.*; import javax.vecmath.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { System.out.println("state changed has been called..."); JSlider source = (JSlider)e.getSource(); int value = source.getValue(); if (source == rShoulderSlider) { setRShoulderRot(value); } } public void setRShoulderRot(int rotation) { rShoulderRot = rotation; rShoulderAA.angle = (float) Math.toRadians(rShoulderRot); } AxisAngle4f rShoulderAA = new AxisAngle4f(0.0f, 0.0f, -1.0f, 0.0f); public void actionPerformed(ActionEvent e) { System.out.println("action performed has been called..."); } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; OffScreenCanvas3D offScreenCanvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); add("Center", canvas); u = new SimpleUniverse(canvas); if (isApplication) { offScreenCanvas = new OffScreenCanvas3D(config, true); Screen3D sOn = canvas.getScreen3D(); Screen3D sOff = offScreenCanvas.getScreen3D(); Dimension dim = sOn.getSize(); dim.width *= offScreenScale; dim.height *= offScreenScale; sOff.setSize(dim); sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * offScreenScale); sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * offScreenScale); u.getViewer().getView().addCanvas3D(offScreenCanvas); } BranchGroup scene = createSceneGraph(); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); view = u.getViewer().getView(); add("East", guiPanel()); } View view; JPanel guiPanel() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(0, 1)); panel.add(new JLabel("Right Shoulder rotation")); rShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rShoulderRot); rShoulderSliderLabel = new JLabel(Integer.toString(rShoulderRot)); panel.add(rShoulderSlider); // this was for the right shoulder rotation // Human_r_elbow rotation panel.add(new JLabel("Right Elbow rotation")); rElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rElbowRot); rElbowSlider.addChangeListener(this); rElbowSliderLabel = new JLabel(Integer.toString(rElbowRot)); panel.add(rElbowSlider); panel.add(rElbowSliderLabel); // Human_l_shoulder rotation panel.add(new JLabel("Left Shoulder rotation")); lShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lShoulderRot); lShoulderSlider.addChangeListener(this); lShoulderSliderLabel = new JLabel(Integer.toString(lShoulderRot)); panel.add(lShoulderSlider); panel.add(lShoulderSliderLabel); // Human_l_elbow rotation panel.add(new JLabel("Left Elbow rotation")); lElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lElbowRot); lElbowSlider.addChangeListener(this); lElbowSliderLabel = new JLabel(Integer.toString(lElbowRot)); panel.add(lElbowSlider); panel.add(rElbowSliderLabel); if (isApplication) { JButton snapButton = new JButton(snapImageString); snapButton.setActionCommand(snapImageString); snapButton.addActionListener(this); panel.add(snapButton); } return panel; } String snapImageString = "Snap Image"; int rShoulderRot = 0; JSlider rShoulderSlider; JLabel rShoulderSliderLabel; int lShoulderRot = 0; JSlider lShoulderSlider; JLabel lShoulderSliderLabel; int rElbowRot = 0; JSlider rElbowSlider; JLabel rElbowSliderLabel; int lElbowRot = 0; JSlider lElbowSlider; JLabel lElbowSliderLabel; BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); return objRoot; } public void destroy() { u.removeAllLocales(); } }
Note that we're getting real close to creating the model.import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; import javax.swing.*; import javax.vecmath.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { System.out.println("state changed has been called..."); JSlider source = (JSlider)e.getSource(); int value = source.getValue(); if (source == rShoulderSlider) { setRShoulderRot(value); } } public void setRShoulderRot(int rotation) { rShoulderRot = rotation; rShoulderAA.angle = (float) Math.toRadians(rShoulderRot); } AxisAngle4f rShoulderAA = new AxisAngle4f(0.0f, 0.0f, -1.0f, 0.0f); public void actionPerformed(ActionEvent e) { System.out.println("action performed has been called..."); } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; OffScreenCanvas3D offScreenCanvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); add("Center", canvas); u = new SimpleUniverse(canvas); if (isApplication) { offScreenCanvas = new OffScreenCanvas3D(config, true); Screen3D sOn = canvas.getScreen3D(); Screen3D sOff = offScreenCanvas.getScreen3D(); Dimension dim = sOn.getSize(); dim.width *= offScreenScale; dim.height *= offScreenScale; sOff.setSize(dim); sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * offScreenScale); sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * offScreenScale); u.getViewer().getView().addCanvas3D(offScreenCanvas); } BranchGroup scene = createSceneGraph(); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); view = u.getViewer().getView(); add("East", guiPanel()); } View view; JPanel guiPanel() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(0, 1)); panel.add(new JLabel("Right Shoulder rotation")); rShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rShoulderRot); rShoulderSliderLabel = new JLabel(Integer.toString(rShoulderRot)); panel.add(rShoulderSlider); // this was for the right shoulder rotation // Human_r_elbow rotation panel.add(new JLabel("Right Elbow rotation")); rElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rElbowRot); rElbowSlider.addChangeListener(this); rElbowSliderLabel = new JLabel(Integer.toString(rElbowRot)); panel.add(rElbowSlider); panel.add(rElbowSliderLabel); // Human_l_shoulder rotation panel.add(new JLabel("Left Shoulder rotation")); lShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lShoulderRot); lShoulderSlider.addChangeListener(this); lShoulderSliderLabel = new JLabel(Integer.toString(lShoulderRot)); panel.add(lShoulderSlider); panel.add(lShoulderSliderLabel); // Human_l_elbow rotation panel.add(new JLabel("Left Elbow rotation")); lElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lElbowRot); lElbowSlider.addChangeListener(this); lElbowSliderLabel = new JLabel(Integer.toString(lElbowRot)); panel.add(lElbowSlider); panel.add(rElbowSliderLabel); if (isApplication) { JButton snapButton = new JButton(snapImageString); snapButton.setActionCommand(snapImageString); snapButton.addActionListener(this); panel.add(snapButton); } return panel; } String snapImageString = "Snap Image"; int rShoulderRot = 0; JSlider rShoulderSlider; JLabel rShoulderSliderLabel; int lShoulderRot = 0; JSlider lShoulderSlider; JLabel lShoulderSliderLabel; int rElbowRot = 0; JSlider rElbowSlider; JLabel rElbowSliderLabel; int lElbowRot = 0; JSlider lElbowSlider; JLabel lElbowSliderLabel; BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); TransformGroup objScale = new TransformGroup(); Transform3D scaleTrans = new Transform3D(); scaleTrans.set(1 / 3.5f); objScale.setTransform(scaleTrans); objRoot.addChild(objScale); TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objScale.addChild(objTrans); createHuman(); return objRoot; } void createHuman() { } public void destroy() { u.removeAllLocales(); } }
Now the background has changed, and is white.import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; import javax.swing.*; import javax.vecmath.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { System.out.println("state changed has been called..."); JSlider source = (JSlider)e.getSource(); int value = source.getValue(); if (source == rShoulderSlider) { setRShoulderRot(value); } } public void setRShoulderRot(int rotation) { rShoulderRot = rotation; rShoulderAA.angle = (float) Math.toRadians(rShoulderRot); } AxisAngle4f rShoulderAA = new AxisAngle4f(0.0f, 0.0f, -1.0f, 0.0f); public void actionPerformed(ActionEvent e) { System.out.println("action performed has been called..."); } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; OffScreenCanvas3D offScreenCanvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); add("Center", canvas); u = new SimpleUniverse(canvas); if (isApplication) { offScreenCanvas = new OffScreenCanvas3D(config, true); Screen3D sOn = canvas.getScreen3D(); Screen3D sOff = offScreenCanvas.getScreen3D(); Dimension dim = sOn.getSize(); dim.width *= offScreenScale; dim.height *= offScreenScale; sOff.setSize(dim); sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * offScreenScale); sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * offScreenScale); u.getViewer().getView().addCanvas3D(offScreenCanvas); } BranchGroup scene = createSceneGraph(); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); view = u.getViewer().getView(); add("East", guiPanel()); } View view; JPanel guiPanel() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(0, 1)); panel.add(new JLabel("Right Shoulder rotation")); rShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rShoulderRot); rShoulderSliderLabel = new JLabel(Integer.toString(rShoulderRot)); panel.add(rShoulderSlider); // this was for the right shoulder rotation // Human_r_elbow rotation panel.add(new JLabel("Right Elbow rotation")); rElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rElbowRot); rElbowSlider.addChangeListener(this); rElbowSliderLabel = new JLabel(Integer.toString(rElbowRot)); panel.add(rElbowSlider); panel.add(rElbowSliderLabel); // Human_l_shoulder rotation panel.add(new JLabel("Left Shoulder rotation")); lShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lShoulderRot); lShoulderSlider.addChangeListener(this); lShoulderSliderLabel = new JLabel(Integer.toString(lShoulderRot)); panel.add(lShoulderSlider); panel.add(lShoulderSliderLabel); // Human_l_elbow rotation panel.add(new JLabel("Left Elbow rotation")); lElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lElbowRot); lElbowSlider.addChangeListener(this); lElbowSliderLabel = new JLabel(Integer.toString(lElbowRot)); panel.add(lElbowSlider); panel.add(rElbowSliderLabel); if (isApplication) { JButton snapButton = new JButton(snapImageString); snapButton.setActionCommand(snapImageString); snapButton.addActionListener(this); panel.add(snapButton); } return panel; } String snapImageString = "Snap Image"; int rShoulderRot = 0; JSlider rShoulderSlider; JLabel rShoulderSliderLabel; int lShoulderRot = 0; JSlider lShoulderSlider; JLabel lShoulderSliderLabel; int rElbowRot = 0; JSlider rElbowSlider; JLabel rElbowSliderLabel; int lElbowRot = 0; JSlider lElbowSlider; JLabel lElbowSliderLabel; BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); TransformGroup objScale = new TransformGroup(); Transform3D scaleTrans = new Transform3D(); scaleTrans.set(1 / 3.5f); objScale.setTransform(scaleTrans); objRoot.addChild(objScale); TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objScale.addChild(objTrans); createHuman(); objTrans.addChild(Human_body); BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0); Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f)); bg.setApplicationBounds(bounds); objTrans.addChild(bg); return objRoot; } TransformGroup Human_body; Vector3f tmpVector = new Vector3f(); Transform3D tmpTrans = new Transform3D(); void createHuman() { Human_body = new TransformGroup(); tmpVector.set(0.0f, -1.5f, 0.0f); tmpTrans.set(tmpVector); Human_body.setTransform(tmpTrans); } public void destroy() { u.removeAllLocales(); } }
Still more has to be added:
If you would look at an empty illuminated universe would you see anything?import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; import javax.swing.*; import javax.vecmath.*; import com.sun.j3d.utils.behaviors.mouse.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { System.out.println("state changed has been called..."); JSlider source = (JSlider)e.getSource(); int value = source.getValue(); if (source == rShoulderSlider) { setRShoulderRot(value); } } public void setRShoulderRot(int rotation) { rShoulderRot = rotation; rShoulderAA.angle = (float) Math.toRadians(rShoulderRot); } AxisAngle4f rShoulderAA = new AxisAngle4f(0.0f, 0.0f, -1.0f, 0.0f); public void actionPerformed(ActionEvent e) { System.out.println("action performed has been called..."); } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; OffScreenCanvas3D offScreenCanvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); add("Center", canvas); u = new SimpleUniverse(canvas); if (isApplication) { offScreenCanvas = new OffScreenCanvas3D(config, true); Screen3D sOn = canvas.getScreen3D(); Screen3D sOff = offScreenCanvas.getScreen3D(); Dimension dim = sOn.getSize(); dim.width *= offScreenScale; dim.height *= offScreenScale; sOff.setSize(dim); sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * offScreenScale); sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * offScreenScale); u.getViewer().getView().addCanvas3D(offScreenCanvas); } BranchGroup scene = createSceneGraph(); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); view = u.getViewer().getView(); add("East", guiPanel()); } View view; JPanel guiPanel() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(0, 1)); panel.add(new JLabel("Right Shoulder rotation")); rShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rShoulderRot); rShoulderSliderLabel = new JLabel(Integer.toString(rShoulderRot)); panel.add(rShoulderSlider); // this was for the right shoulder rotation // Human_r_elbow rotation panel.add(new JLabel("Right Elbow rotation")); rElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rElbowRot); rElbowSlider.addChangeListener(this); rElbowSliderLabel = new JLabel(Integer.toString(rElbowRot)); panel.add(rElbowSlider); panel.add(rElbowSliderLabel); // Human_l_shoulder rotation panel.add(new JLabel("Left Shoulder rotation")); lShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lShoulderRot); lShoulderSlider.addChangeListener(this); lShoulderSliderLabel = new JLabel(Integer.toString(lShoulderRot)); panel.add(lShoulderSlider); panel.add(lShoulderSliderLabel); // Human_l_elbow rotation panel.add(new JLabel("Left Elbow rotation")); lElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lElbowRot); lElbowSlider.addChangeListener(this); lElbowSliderLabel = new JLabel(Integer.toString(lElbowRot)); panel.add(lElbowSlider); panel.add(rElbowSliderLabel); if (isApplication) { JButton snapButton = new JButton(snapImageString); snapButton.setActionCommand(snapImageString); snapButton.addActionListener(this); panel.add(snapButton); } return panel; } String snapImageString = "Snap Image"; int rShoulderRot = 0; JSlider rShoulderSlider; JLabel rShoulderSliderLabel; int lShoulderRot = 0; JSlider lShoulderSlider; JLabel lShoulderSliderLabel; int rElbowRot = 0; JSlider rElbowSlider; JLabel rElbowSliderLabel; int lElbowRot = 0; JSlider lElbowSlider; JLabel lElbowSliderLabel; BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); TransformGroup objScale = new TransformGroup(); Transform3D scaleTrans = new Transform3D(); scaleTrans.set(1 / 3.5f); objScale.setTransform(scaleTrans); objRoot.addChild(objScale); TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objScale.addChild(objTrans); createHuman(); objTrans.addChild(Human_body); BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0); Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f)); bg.setApplicationBounds(bounds); objTrans.addChild(bg); MouseRotate mr = new MouseRotate(); mr.setTransformGroup(objTrans); mr.setSchedulingBounds(bounds); mr.setFactor(0.007); objTrans.addChild(mr); Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f); AmbientLight ambientLightNode = new AmbientLight(ambientColor); ambientLightNode.setInfluencingBounds(bounds); objRoot.addChild(ambientLightNode); Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f); Vector3f light1Direction = new Vector3f(0.0f, -0.2f, -1.0f); DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); objRoot.addChild(light1); return objRoot; } TransformGroup Human_body; Vector3f tmpVector = new Vector3f(); Transform3D tmpTrans = new Transform3D(); void createHuman() { Human_body = new TransformGroup(); tmpVector.set(0.0f, -1.5f, 0.0f); tmpTrans.set(tmpVector); Human_body.setTransform(tmpTrans); } public void destroy() { u.removeAllLocales(); } }
So finally things start moving.import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; import javax.swing.*; import javax.vecmath.*; import com.sun.j3d.utils.behaviors.mouse.*; import com.sun.j3d.utils.geometry.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { System.out.println("state changed has been called..."); JSlider source = (JSlider)e.getSource(); int value = source.getValue(); if (source == rShoulderSlider) { setRShoulderRot(value); } } public void setRShoulderRot(int rotation) { rShoulderRot = rotation; rShoulderAA.angle = (float) Math.toRadians(rShoulderRot); } AxisAngle4f rShoulderAA = new AxisAngle4f(0.0f, 0.0f, -1.0f, 0.0f); public void actionPerformed(ActionEvent e) { System.out.println("action performed has been called..."); } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; OffScreenCanvas3D offScreenCanvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); add("Center", canvas); u = new SimpleUniverse(canvas); if (isApplication) { offScreenCanvas = new OffScreenCanvas3D(config, true); Screen3D sOn = canvas.getScreen3D(); Screen3D sOff = offScreenCanvas.getScreen3D(); Dimension dim = sOn.getSize(); dim.width *= offScreenScale; dim.height *= offScreenScale; sOff.setSize(dim); sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * offScreenScale); sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * offScreenScale); u.getViewer().getView().addCanvas3D(offScreenCanvas); } BranchGroup scene = createSceneGraph(); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); view = u.getViewer().getView(); add("East", guiPanel()); } View view; JPanel guiPanel() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(0, 1)); panel.add(new JLabel("Right Shoulder rotation")); rShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rShoulderRot); rShoulderSliderLabel = new JLabel(Integer.toString(rShoulderRot)); panel.add(rShoulderSlider); // this was for the right shoulder rotation // Human_r_elbow rotation panel.add(new JLabel("Right Elbow rotation")); rElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rElbowRot); rElbowSlider.addChangeListener(this); rElbowSliderLabel = new JLabel(Integer.toString(rElbowRot)); panel.add(rElbowSlider); panel.add(rElbowSliderLabel); // Human_l_shoulder rotation panel.add(new JLabel("Left Shoulder rotation")); lShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lShoulderRot); lShoulderSlider.addChangeListener(this); lShoulderSliderLabel = new JLabel(Integer.toString(lShoulderRot)); panel.add(lShoulderSlider); panel.add(lShoulderSliderLabel); // Human_l_elbow rotation panel.add(new JLabel("Left Elbow rotation")); lElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lElbowRot); lElbowSlider.addChangeListener(this); lElbowSliderLabel = new JLabel(Integer.toString(lElbowRot)); panel.add(lElbowSlider); panel.add(rElbowSliderLabel); if (isApplication) { JButton snapButton = new JButton(snapImageString); snapButton.setActionCommand(snapImageString); snapButton.addActionListener(this); panel.add(snapButton); } return panel; } String snapImageString = "Snap Image"; int rShoulderRot = 0; JSlider rShoulderSlider; JLabel rShoulderSliderLabel; int lShoulderRot = 0; JSlider lShoulderSlider; JLabel lShoulderSliderLabel; int rElbowRot = 0; JSlider rElbowSlider; JLabel rElbowSliderLabel; int lElbowRot = 0; JSlider lElbowSlider; JLabel lElbowSliderLabel; BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); TransformGroup objScale = new TransformGroup(); Transform3D scaleTrans = new Transform3D(); scaleTrans.set(1 / 3.5f); objScale.setTransform(scaleTrans); objRoot.addChild(objScale); TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objScale.addChild(objTrans); createHuman(); objTrans.addChild(Human_body); BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0); Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f)); bg.setApplicationBounds(bounds); objTrans.addChild(bg); MouseRotate mr = new MouseRotate(); mr.setTransformGroup(objTrans); mr.setSchedulingBounds(bounds); mr.setFactor(0.007); objTrans.addChild(mr); Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f); AmbientLight ambientLightNode = new AmbientLight(ambientColor); ambientLightNode.setInfluencingBounds(bounds); objRoot.addChild(ambientLightNode); Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f); Vector3f light1Direction = new Vector3f(0.0f, -0.2f, -1.0f); DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); objRoot.addChild(light1); return objRoot; } TransformGroup Human_body; Vector3f tmpVector = new Vector3f(); Transform3D tmpTrans = new Transform3D(); Color3f red = new Color3f(1.0f, 0.0f, 0.0f); Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Color3f white = new Color3f(1.0f, 1.0f, 1.0f); TransformGroup tmpTG; Cylinder tmpCyl; void createHuman() { Human_body = new TransformGroup(); tmpVector.set(0.0f, -1.5f, 0.0f); tmpTrans.set(tmpVector); Human_body.setTransform(tmpTrans); Material material = new Material(red, black, red, white, 64); Appearance appearance = new Appearance(); appearance.setMaterial(material); tmpTG = new TransformGroup(); tmpVector.set(0.0f, 1.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.75f, 3.0f, appearance); tmpTG.addChild(tmpCyl); Human_body.addChild(tmpTG); } public void destroy() { u.removeAllLocales(); } }
(Does this remind you of Pinocchio?)
Now the right shoulder is there.import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; import javax.swing.*; import javax.vecmath.*; import com.sun.j3d.utils.behaviors.mouse.*; import com.sun.j3d.utils.geometry.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { System.out.println("state changed has been called..."); JSlider source = (JSlider)e.getSource(); int value = source.getValue(); if (source == rShoulderSlider) { setRShoulderRot(value); } } public void setRShoulderRot(int rotation) { rShoulderRot = rotation; rShoulderAA.angle = (float) Math.toRadians(rShoulderRot); } AxisAngle4f rShoulderAA = new AxisAngle4f(0.0f, 0.0f, -1.0f, 0.0f); public void actionPerformed(ActionEvent e) { System.out.println("action performed has been called..."); } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; OffScreenCanvas3D offScreenCanvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); add("Center", canvas); u = new SimpleUniverse(canvas); if (isApplication) { offScreenCanvas = new OffScreenCanvas3D(config, true); Screen3D sOn = canvas.getScreen3D(); Screen3D sOff = offScreenCanvas.getScreen3D(); Dimension dim = sOn.getSize(); dim.width *= offScreenScale; dim.height *= offScreenScale; sOff.setSize(dim); sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * offScreenScale); sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * offScreenScale); u.getViewer().getView().addCanvas3D(offScreenCanvas); } BranchGroup scene = createSceneGraph(); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); view = u.getViewer().getView(); add("East", guiPanel()); } View view; JPanel guiPanel() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(0, 1)); panel.add(new JLabel("Right Shoulder rotation")); rShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rShoulderRot); rShoulderSliderLabel = new JLabel(Integer.toString(rShoulderRot)); panel.add(rShoulderSlider); // this was for the right shoulder rotation // Human_r_elbow rotation panel.add(new JLabel("Right Elbow rotation")); rElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rElbowRot); rElbowSlider.addChangeListener(this); rElbowSliderLabel = new JLabel(Integer.toString(rElbowRot)); panel.add(rElbowSlider); panel.add(rElbowSliderLabel); // Human_l_shoulder rotation panel.add(new JLabel("Left Shoulder rotation")); lShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lShoulderRot); lShoulderSlider.addChangeListener(this); lShoulderSliderLabel = new JLabel(Integer.toString(lShoulderRot)); panel.add(lShoulderSlider); panel.add(lShoulderSliderLabel); // Human_l_elbow rotation panel.add(new JLabel("Left Elbow rotation")); lElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lElbowRot); lElbowSlider.addChangeListener(this); lElbowSliderLabel = new JLabel(Integer.toString(lElbowRot)); panel.add(lElbowSlider); panel.add(rElbowSliderLabel); if (isApplication) { JButton snapButton = new JButton(snapImageString); snapButton.setActionCommand(snapImageString); snapButton.addActionListener(this); panel.add(snapButton); } return panel; } String snapImageString = "Snap Image"; int rShoulderRot = 0; JSlider rShoulderSlider; JLabel rShoulderSliderLabel; int lShoulderRot = 0; JSlider lShoulderSlider; JLabel lShoulderSliderLabel; int rElbowRot = 0; JSlider rElbowSlider; JLabel rElbowSliderLabel; int lElbowRot = 0; JSlider lElbowSlider; JLabel lElbowSliderLabel; BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); TransformGroup objScale = new TransformGroup(); Transform3D scaleTrans = new Transform3D(); scaleTrans.set(1 / 3.5f); objScale.setTransform(scaleTrans); objRoot.addChild(objScale); TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objScale.addChild(objTrans); createHuman(); objTrans.addChild(Human_body); BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0); Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f)); bg.setApplicationBounds(bounds); objTrans.addChild(bg); MouseRotate mr = new MouseRotate(); mr.setTransformGroup(objTrans); mr.setSchedulingBounds(bounds); mr.setFactor(0.007); objTrans.addChild(mr); Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f); AmbientLight ambientLightNode = new AmbientLight(ambientColor); ambientLightNode.setInfluencingBounds(bounds); objRoot.addChild(ambientLightNode); Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f); Vector3f light1Direction = new Vector3f(0.0f, -0.2f, -1.0f); DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); objRoot.addChild(light1); return objRoot; } TransformGroup Human_body; Vector3f tmpVector = new Vector3f(); Transform3D tmpTrans = new Transform3D(); Color3f red = new Color3f(1.0f, 0.0f, 0.0f); Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Color3f white = new Color3f(1.0f, 1.0f, 1.0f); TransformGroup tmpTG; TransformGroup Human_r_shoulder; Cylinder tmpCyl; Sphere tmpSphere; void createHuman() { Human_body = new TransformGroup(); tmpVector.set(0.0f, -1.5f, 0.0f); tmpTrans.set(tmpVector); Human_body.setTransform(tmpTrans); Material material = new Material(red, black, red, white, 64); Appearance appearance = new Appearance(); appearance.setMaterial(material); tmpTG = new TransformGroup(); tmpVector.set(0.0f, 1.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.75f, 3.0f, appearance); tmpTG.addChild(tmpCyl); Human_body.addChild(tmpTG); Human_r_shoulder = new TransformGroup(); Human_r_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Human_r_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tmpVector.set(-0.95f, 2.9f, -0.2f); tmpTrans.set(tmpVector); Human_r_shoulder.setTransform(tmpTrans); tmpSphere = new Sphere(0.22f, appearance); Human_r_shoulder.addChild(tmpSphere); tmpTG = new TransformGroup(); tmpVector.set(0.0f, -0.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.2f, 1.0f, appearance); tmpTG.addChild(tmpCyl); Human_r_shoulder.addChild(tmpTG); Human_body.addChild(Human_r_shoulder); } public void destroy() { u.removeAllLocales(); } }
And it moves, thanks to one newly added (rather late, though) line.import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; import javax.swing.*; import javax.vecmath.*; import com.sun.j3d.utils.behaviors.mouse.*; import com.sun.j3d.utils.geometry.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { JSlider source = (JSlider)e.getSource(); int value = source.getValue(); System.out.print("..."); if (source == rShoulderSlider) { setRShoulderRot(value); rShoulderSliderLabel.setText(Integer.toString(value)); } else { System.out.println("who's this?"); } } public void setRShoulderRot(int rotation) { System.out.println("I am changing the r shoulder slider..."); rShoulderRot = rotation; rShoulderAA.angle = (float) Math.toRadians(rShoulderRot); Human_r_shoulder.getTransform(tmpTrans); tmpTrans.setRotation(rShoulderAA); Human_r_shoulder.setTransform(tmpTrans); } AxisAngle4f rShoulderAA = new AxisAngle4f(0.0f, 0.0f, -1.0f, 0.0f); public void actionPerformed(ActionEvent e) { System.out.println("action performed has been called..."); } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; OffScreenCanvas3D offScreenCanvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); add("Center", canvas); u = new SimpleUniverse(canvas); if (isApplication) { offScreenCanvas = new OffScreenCanvas3D(config, true); Screen3D sOn = canvas.getScreen3D(); Screen3D sOff = offScreenCanvas.getScreen3D(); Dimension dim = sOn.getSize(); dim.width *= offScreenScale; dim.height *= offScreenScale; sOff.setSize(dim); sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * offScreenScale); sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * offScreenScale); u.getViewer().getView().addCanvas3D(offScreenCanvas); } BranchGroup scene = createSceneGraph(); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); view = u.getViewer().getView(); add("East", guiPanel()); } View view; JPanel guiPanel() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(0, 1)); panel.add(new JLabel("Right Shoulder rotation")); rShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rShoulderRot); rShoulderSlider.addChangeListener(this); // we somehow skipped this the first time around...?! rShoulderSliderLabel = new JLabel(Integer.toString(rShoulderRot)); panel.add(rShoulderSlider); // this was for the right shoulder rotation // Human_r_elbow rotation panel.add(new JLabel("Right Elbow rotation")); rElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rElbowRot); rElbowSlider.addChangeListener(this); rElbowSliderLabel = new JLabel(Integer.toString(rElbowRot)); panel.add(rElbowSlider); panel.add(rElbowSliderLabel); // Human_l_shoulder rotation panel.add(new JLabel("Left Shoulder rotation")); lShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lShoulderRot); lShoulderSlider.addChangeListener(this); lShoulderSliderLabel = new JLabel(Integer.toString(lShoulderRot)); panel.add(lShoulderSlider); panel.add(lShoulderSliderLabel); // Human_l_elbow rotation panel.add(new JLabel("Left Elbow rotation")); lElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lElbowRot); lElbowSlider.addChangeListener(this); lElbowSliderLabel = new JLabel(Integer.toString(lElbowRot)); panel.add(lElbowSlider); panel.add(rElbowSliderLabel); if (isApplication) { JButton snapButton = new JButton(snapImageString); snapButton.setActionCommand(snapImageString); snapButton.addActionListener(this); panel.add(snapButton); } return panel; } String snapImageString = "Snap Image"; int rShoulderRot = 0; JSlider rShoulderSlider; JLabel rShoulderSliderLabel; int lShoulderRot = 0; JSlider lShoulderSlider; JLabel lShoulderSliderLabel; int rElbowRot = 0; JSlider rElbowSlider; JLabel rElbowSliderLabel; int lElbowRot = 0; JSlider lElbowSlider; JLabel lElbowSliderLabel; BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); TransformGroup objScale = new TransformGroup(); Transform3D scaleTrans = new Transform3D(); scaleTrans.set(1 / 3.5f); objScale.setTransform(scaleTrans); objRoot.addChild(objScale); TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objScale.addChild(objTrans); createHuman(); objTrans.addChild(Human_body); BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0); Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f)); bg.setApplicationBounds(bounds); objTrans.addChild(bg); MouseRotate mr = new MouseRotate(); mr.setTransformGroup(objTrans); mr.setSchedulingBounds(bounds); mr.setFactor(0.007); objTrans.addChild(mr); Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f); AmbientLight ambientLightNode = new AmbientLight(ambientColor); ambientLightNode.setInfluencingBounds(bounds); objRoot.addChild(ambientLightNode); Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f); Vector3f light1Direction = new Vector3f(0.0f, -0.2f, -1.0f); DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); objRoot.addChild(light1); return objRoot; } TransformGroup Human_body; Vector3f tmpVector = new Vector3f(); Transform3D tmpTrans = new Transform3D(); Color3f red = new Color3f(1.0f, 0.0f, 0.0f); Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Color3f white = new Color3f(1.0f, 1.0f, 1.0f); TransformGroup tmpTG; TransformGroup Human_r_shoulder; Cylinder tmpCyl; Sphere tmpSphere; void createHuman() { Human_body = new TransformGroup(); tmpVector.set(0.0f, -1.5f, 0.0f); tmpTrans.set(tmpVector); Human_body.setTransform(tmpTrans); Material material = new Material(red, black, red, white, 64); Appearance appearance = new Appearance(); appearance.setMaterial(material); tmpTG = new TransformGroup(); tmpVector.set(0.0f, 1.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.75f, 3.0f, appearance); tmpTG.addChild(tmpCyl); Human_body.addChild(tmpTG); Human_r_shoulder = new TransformGroup(); Human_r_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Human_r_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tmpVector.set(-0.95f, 2.9f, -0.2f); tmpTrans.set(tmpVector); Human_r_shoulder.setTransform(tmpTrans); tmpSphere = new Sphere(0.22f, appearance); Human_r_shoulder.addChild(tmpSphere); tmpTG = new TransformGroup(); tmpVector.set(0.0f, -0.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.2f, 1.0f, appearance); tmpTG.addChild(tmpCyl); Human_r_shoulder.addChild(tmpTG); Human_body.addChild(Human_r_shoulder); } public void destroy() { u.removeAllLocales(); } }
We need to do something for the left elbow.import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; import javax.swing.*; import javax.vecmath.*; import com.sun.j3d.utils.behaviors.mouse.*; import com.sun.j3d.utils.geometry.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { JSlider source = (JSlider)e.getSource(); int value = source.getValue(); System.out.print("..."); if (source == rShoulderSlider) { setRShoulderRot(value); rShoulderSliderLabel.setText(Integer.toString(value)); } else { System.out.println("who's this?"); } } public void setRShoulderRot(int rotation) { System.out.println("I am changing the r shoulder slider..."); rShoulderRot = rotation; rShoulderAA.angle = (float) Math.toRadians(rShoulderRot); Human_r_shoulder.getTransform(tmpTrans); tmpTrans.setRotation(rShoulderAA); Human_r_shoulder.setTransform(tmpTrans); } AxisAngle4f rShoulderAA = new AxisAngle4f(0.0f, 0.0f, -1.0f, 0.0f); public void actionPerformed(ActionEvent e) { System.out.println("action performed has been called..."); } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; OffScreenCanvas3D offScreenCanvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); add("Center", canvas); u = new SimpleUniverse(canvas); if (isApplication) { offScreenCanvas = new OffScreenCanvas3D(config, true); Screen3D sOn = canvas.getScreen3D(); Screen3D sOff = offScreenCanvas.getScreen3D(); Dimension dim = sOn.getSize(); dim.width *= offScreenScale; dim.height *= offScreenScale; sOff.setSize(dim); sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * offScreenScale); sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * offScreenScale); u.getViewer().getView().addCanvas3D(offScreenCanvas); } BranchGroup scene = createSceneGraph(); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); view = u.getViewer().getView(); add("East", guiPanel()); } View view; JPanel guiPanel() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(0, 1)); panel.add(new JLabel("Right Shoulder rotation")); rShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rShoulderRot); rShoulderSlider.addChangeListener(this); // we somehow skipped this the first time around...?! rShoulderSliderLabel = new JLabel(Integer.toString(rShoulderRot)); panel.add(rShoulderSlider); // this was for the right shoulder rotation // Human_r_elbow rotation panel.add(new JLabel("Right Elbow rotation")); rElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rElbowRot); rElbowSlider.addChangeListener(this); rElbowSliderLabel = new JLabel(Integer.toString(rElbowRot)); panel.add(rElbowSlider); panel.add(rElbowSliderLabel); // Human_l_shoulder rotation panel.add(new JLabel("Left Shoulder rotation")); lShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lShoulderRot); lShoulderSlider.addChangeListener(this); lShoulderSliderLabel = new JLabel(Integer.toString(lShoulderRot)); panel.add(lShoulderSlider); panel.add(lShoulderSliderLabel); // Human_l_elbow rotation panel.add(new JLabel("Left Elbow rotation")); lElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lElbowRot); lElbowSlider.addChangeListener(this); lElbowSliderLabel = new JLabel(Integer.toString(lElbowRot)); panel.add(lElbowSlider); panel.add(rElbowSliderLabel); if (isApplication) { JButton snapButton = new JButton(snapImageString); snapButton.setActionCommand(snapImageString); snapButton.addActionListener(this); panel.add(snapButton); } return panel; } String snapImageString = "Snap Image"; int rShoulderRot = 0; JSlider rShoulderSlider; JLabel rShoulderSliderLabel; int lShoulderRot = 0; JSlider lShoulderSlider; JLabel lShoulderSliderLabel; int rElbowRot = 0; JSlider rElbowSlider; JLabel rElbowSliderLabel; int lElbowRot = 0; JSlider lElbowSlider; JLabel lElbowSliderLabel; BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); TransformGroup objScale = new TransformGroup(); Transform3D scaleTrans = new Transform3D(); scaleTrans.set(1 / 3.5f); objScale.setTransform(scaleTrans); objRoot.addChild(objScale); TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objScale.addChild(objTrans); createHuman(); objTrans.addChild(Human_body); BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0); Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f)); bg.setApplicationBounds(bounds); objTrans.addChild(bg); MouseRotate mr = new MouseRotate(); mr.setTransformGroup(objTrans); mr.setSchedulingBounds(bounds); mr.setFactor(0.007); objTrans.addChild(mr); Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f); AmbientLight ambientLightNode = new AmbientLight(ambientColor); ambientLightNode.setInfluencingBounds(bounds); objRoot.addChild(ambientLightNode); Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f); Vector3f light1Direction = new Vector3f(0.0f, -0.2f, -1.0f); DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); objRoot.addChild(light1); return objRoot; } TransformGroup Human_body; Vector3f tmpVector = new Vector3f(); Transform3D tmpTrans = new Transform3D(); Color3f red = new Color3f(1.0f, 0.0f, 0.0f); Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Color3f white = new Color3f(1.0f, 1.0f, 1.0f); TransformGroup tmpTG; TransformGroup Human_r_shoulder; Cylinder tmpCyl; Sphere tmpSphere; void createHuman() { Human_body = new TransformGroup(); tmpVector.set(0.0f, -1.5f, 0.0f); tmpTrans.set(tmpVector); Human_body.setTransform(tmpTrans); Material material = new Material(red, black, red, white, 64); Appearance appearance = new Appearance(); appearance.setMaterial(material); tmpTG = new TransformGroup(); tmpVector.set(0.0f, 1.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.75f, 3.0f, appearance); tmpTG.addChild(tmpCyl); Human_body.addChild(tmpTG); Human_r_shoulder = new TransformGroup(); Human_r_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Human_r_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tmpVector.set(-0.95f, 2.9f, -0.2f); tmpTrans.set(tmpVector); Human_r_shoulder.setTransform(tmpTrans); tmpSphere = new Sphere(0.22f, appearance); Human_r_shoulder.addChild(tmpSphere); tmpTG = new TransformGroup(); tmpVector.set(0.0f, -0.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.2f, 1.0f, appearance); tmpTG.addChild(tmpCyl); Human_r_shoulder.addChild(tmpTG); Human_body.addChild(Human_r_shoulder); // create the r_elbow TransformGroup Human_r_elbow = new TransformGroup(); Human_r_elbow.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Human_r_elbow.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tmpVector.set(0.0f, -1.054f, 0.0f); tmpTrans.set(tmpVector); Human_r_elbow.setTransform(tmpTrans); // place the sphere for the r_elbow tmpSphere = new Sphere(0.22f, appearance); Human_r_elbow.addChild(tmpSphere); // offset and place the cylinder for the r_shoulder tmpTG = new TransformGroup(); // offset the shape tmpVector.set(0.0f,-0.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.2f, 1.0f, appearance); tmpTG.addChild(tmpCyl); // add the shape to the r_shoulder Human_r_elbow.addChild(tmpTG); // add the elbow to the shoulder group Human_r_shoulder.addChild(Human_r_elbow); } TransformGroup Human_r_elbow; public void destroy() { u.removeAllLocales(); } }
Some of the changes are visible, the others are just building a potential.import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; import javax.swing.*; import javax.vecmath.*; import com.sun.j3d.utils.behaviors.mouse.*; import com.sun.j3d.utils.geometry.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { JSlider source = (JSlider)e.getSource(); int value = source.getValue(); System.out.print("..."); if (source == rShoulderSlider) { setRShoulderRot(value); rShoulderSliderLabel.setText(Integer.toString(value)); } else if (source == rElbowSlider) { setRElbowRot(value); rElbowSliderLabel.setText(Integer.toString(value)); } else if (source == lShoulderSlider) { setLShoulderRot(value); lShoulderSliderLabel.setText(Integer.toString(value)); } else if (source == lElbowSlider) { setLElbowRot(value); lElbowSliderLabel.setText(Integer.toString(value)); } } TransformGroup Human_l_shoulder; TransformGroup Human_l_elbow; TransformGroup Human_skullbase; AxisAngle4f rElbowAA = new AxisAngle4f(0.0f, 0.0f,-1.0f, 0.0f); AxisAngle4f lShoulderAA = new AxisAngle4f(0.0f, 0.0f,1.0f, 0.0f); AxisAngle4f lElbowAA = new AxisAngle4f(0.0f, 0.0f,1.0f, 0.0f); public void setRElbowRot(int rotation) { float angle = (float) Math.toRadians(rotation); rElbowRot = rotation; rElbowAA.angle = (float) Math.toRadians(rElbowRot); Human_r_elbow.getTransform(tmpTrans); tmpTrans.setRotation(rElbowAA); Human_r_elbow.setTransform(tmpTrans); } public void setLShoulderRot(int rotation) { lShoulderRot = rotation; lShoulderAA.angle = (float) Math.toRadians(lShoulderRot); Human_l_shoulder.getTransform(tmpTrans); tmpTrans.setRotation(lShoulderAA); Human_l_shoulder.setTransform(tmpTrans); } public void setLElbowRot(int rotation) { float angle = (float) Math.toRadians(rotation); lElbowRot = rotation; lElbowAA.angle = (float) Math.toRadians(lElbowRot); Human_l_elbow.getTransform(tmpTrans); tmpTrans.setRotation(lElbowAA); Human_l_elbow.setTransform(tmpTrans); } public void setRShoulderRot(int rotation) { System.out.println("I am changing the r shoulder slider..."); rShoulderRot = rotation; rShoulderAA.angle = (float) Math.toRadians(rShoulderRot); Human_r_shoulder.getTransform(tmpTrans); tmpTrans.setRotation(rShoulderAA); Human_r_shoulder.setTransform(tmpTrans); } AxisAngle4f rShoulderAA = new AxisAngle4f(0.0f, 0.0f, -1.0f, 0.0f); public void actionPerformed(ActionEvent e) { System.out.println("action performed has been called..."); } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; OffScreenCanvas3D offScreenCanvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); add("Center", canvas); u = new SimpleUniverse(canvas); if (isApplication) { offScreenCanvas = new OffScreenCanvas3D(config, true); Screen3D sOn = canvas.getScreen3D(); Screen3D sOff = offScreenCanvas.getScreen3D(); Dimension dim = sOn.getSize(); dim.width *= offScreenScale; dim.height *= offScreenScale; sOff.setSize(dim); sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * offScreenScale); sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * offScreenScale); u.getViewer().getView().addCanvas3D(offScreenCanvas); } BranchGroup scene = createSceneGraph(); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); view = u.getViewer().getView(); add("East", guiPanel()); } View view; JPanel guiPanel() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(0, 1)); panel.add(new JLabel("Right Shoulder rotation")); rShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rShoulderRot); rShoulderSlider.addChangeListener(this); // we somehow skipped this the first time around...?! rShoulderSliderLabel = new JLabel(Integer.toString(rShoulderRot)); panel.add(rShoulderSlider); // this was for the right shoulder rotation // Human_r_elbow rotation panel.add(new JLabel("Right Elbow rotation")); rElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rElbowRot); rElbowSlider.addChangeListener(this); rElbowSliderLabel = new JLabel(Integer.toString(rElbowRot)); panel.add(rElbowSlider); panel.add(rElbowSliderLabel); // Human_l_shoulder rotation panel.add(new JLabel("Left Shoulder rotation")); lShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lShoulderRot); lShoulderSlider.addChangeListener(this); lShoulderSliderLabel = new JLabel(Integer.toString(lShoulderRot)); panel.add(lShoulderSlider); panel.add(lShoulderSliderLabel); // Human_l_elbow rotation panel.add(new JLabel("Left Elbow rotation")); lElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lElbowRot); lElbowSlider.addChangeListener(this); lElbowSliderLabel = new JLabel(Integer.toString(lElbowRot)); panel.add(lElbowSlider); panel.add(rElbowSliderLabel); if (isApplication) { JButton snapButton = new JButton(snapImageString); snapButton.setActionCommand(snapImageString); snapButton.addActionListener(this); panel.add(snapButton); } return panel; } String snapImageString = "Snap Image"; int rShoulderRot = 0; JSlider rShoulderSlider; JLabel rShoulderSliderLabel; int lShoulderRot = 0; JSlider lShoulderSlider; JLabel lShoulderSliderLabel; int rElbowRot = 0; JSlider rElbowSlider; JLabel rElbowSliderLabel; int lElbowRot = 0; JSlider lElbowSlider; JLabel lElbowSliderLabel; BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); TransformGroup objScale = new TransformGroup(); Transform3D scaleTrans = new Transform3D(); scaleTrans.set(1 / 3.5f); objScale.setTransform(scaleTrans); objRoot.addChild(objScale); TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objScale.addChild(objTrans); createHuman(); objTrans.addChild(Human_body); BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0); Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f)); bg.setApplicationBounds(bounds); objTrans.addChild(bg); MouseRotate mr = new MouseRotate(); mr.setTransformGroup(objTrans); mr.setSchedulingBounds(bounds); mr.setFactor(0.007); objTrans.addChild(mr); Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f); AmbientLight ambientLightNode = new AmbientLight(ambientColor); ambientLightNode.setInfluencingBounds(bounds); objRoot.addChild(ambientLightNode); Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f); Vector3f light1Direction = new Vector3f(0.0f, -0.2f, -1.0f); DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); objRoot.addChild(light1); return objRoot; } TransformGroup Human_body; Vector3f tmpVector = new Vector3f(); Transform3D tmpTrans = new Transform3D(); Color3f red = new Color3f(1.0f, 0.0f, 0.0f); Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Color3f white = new Color3f(1.0f, 1.0f, 1.0f); TransformGroup tmpTG; TransformGroup Human_r_shoulder; Cylinder tmpCyl; Sphere tmpSphere; void createHuman() { Human_body = new TransformGroup(); tmpVector.set(0.0f, -1.5f, 0.0f); tmpTrans.set(tmpVector); Human_body.setTransform(tmpTrans); Material material = new Material(red, black, red, white, 64); Appearance appearance = new Appearance(); appearance.setMaterial(material); tmpTG = new TransformGroup(); tmpVector.set(0.0f, 1.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.75f, 3.0f, appearance); tmpTG.addChild(tmpCyl); Human_body.addChild(tmpTG); Human_r_shoulder = new TransformGroup(); Human_r_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Human_r_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tmpVector.set(-0.95f, 2.9f, -0.2f); tmpTrans.set(tmpVector); Human_r_shoulder.setTransform(tmpTrans); tmpSphere = new Sphere(0.22f, appearance); Human_r_shoulder.addChild(tmpSphere); tmpTG = new TransformGroup(); tmpVector.set(0.0f, -0.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.2f, 1.0f, appearance); tmpTG.addChild(tmpCyl); Human_r_shoulder.addChild(tmpTG); Human_body.addChild(Human_r_shoulder); // create the r_elbow TransformGroup Human_r_elbow = new TransformGroup(); Human_r_elbow.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Human_r_elbow.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tmpVector.set(0.0f, -1.054f, 0.0f); tmpTrans.set(tmpVector); Human_r_elbow.setTransform(tmpTrans); // place the sphere for the r_elbow tmpSphere = new Sphere(0.22f, appearance); Human_r_elbow.addChild(tmpSphere); // offset and place the cylinder for the r_shoulder tmpTG = new TransformGroup(); // offset the shape tmpVector.set(0.0f,-0.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.2f, 1.0f, appearance); tmpTG.addChild(tmpCyl); // add the shape to the r_shoulder Human_r_elbow.addChild(tmpTG); // add the elbow to the shoulder group Human_r_shoulder.addChild(Human_r_elbow); } TransformGroup Human_r_elbow; public void destroy() { u.removeAllLocales(); } }
So we're almost done:import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; import javax.swing.*; import javax.vecmath.*; import com.sun.j3d.utils.behaviors.mouse.*; import com.sun.j3d.utils.geometry.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { JSlider source = (JSlider)e.getSource(); int value = source.getValue(); if (source == rShoulderSlider) { setRShoulderRot(value); rShoulderSliderLabel.setText(Integer.toString(value)); } else if (source == rElbowSlider) { setRElbowRot(value); rElbowSliderLabel.setText(Integer.toString(value)); } else if (source == lShoulderSlider) { setLShoulderRot(value); lShoulderSliderLabel.setText(Integer.toString(value)); } else if (source == lElbowSlider) { setLElbowRot(value); lElbowSliderLabel.setText(Integer.toString(value)); } } TransformGroup Human_l_shoulder; TransformGroup Human_l_elbow; TransformGroup Human_skullbase; AxisAngle4f rElbowAA = new AxisAngle4f(0.0f, 0.0f,-1.0f, 0.0f); AxisAngle4f lShoulderAA = new AxisAngle4f(0.0f, 0.0f,1.0f, 0.0f); AxisAngle4f lElbowAA = new AxisAngle4f(0.0f, 0.0f,1.0f, 0.0f); public void setRElbowRot(int rotation) { float angle = (float) Math.toRadians(rotation); rElbowRot = rotation; rElbowAA.angle = (float) Math.toRadians(rElbowRot); Human_r_elbow.getTransform(tmpTrans); tmpTrans.setRotation(rElbowAA); Human_r_elbow.setTransform(tmpTrans); } public void setLShoulderRot(int rotation) { lShoulderRot = rotation; lShoulderAA.angle = (float) Math.toRadians(lShoulderRot); Human_l_shoulder.getTransform(tmpTrans); tmpTrans.setRotation(lShoulderAA); Human_l_shoulder.setTransform(tmpTrans); } public void setLElbowRot(int rotation) { float angle = (float) Math.toRadians(rotation); lElbowRot = rotation; lElbowAA.angle = (float) Math.toRadians(lElbowRot); Human_l_elbow.getTransform(tmpTrans); tmpTrans.setRotation(lElbowAA); Human_l_elbow.setTransform(tmpTrans); } public void setRShoulderRot(int rotation) { System.out.println("I am changing the r shoulder slider..."); rShoulderRot = rotation; rShoulderAA.angle = (float) Math.toRadians(rShoulderRot); Human_r_shoulder.getTransform(tmpTrans); tmpTrans.setRotation(rShoulderAA); Human_r_shoulder.setTransform(tmpTrans); } AxisAngle4f rShoulderAA = new AxisAngle4f(0.0f, 0.0f, -1.0f, 0.0f); public void actionPerformed(ActionEvent e) { System.out.println("action performed has been called..."); } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; OffScreenCanvas3D offScreenCanvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); add("Center", canvas); u = new SimpleUniverse(canvas); if (isApplication) { offScreenCanvas = new OffScreenCanvas3D(config, true); Screen3D sOn = canvas.getScreen3D(); Screen3D sOff = offScreenCanvas.getScreen3D(); Dimension dim = sOn.getSize(); dim.width *= offScreenScale; dim.height *= offScreenScale; sOff.setSize(dim); sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * offScreenScale); sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * offScreenScale); u.getViewer().getView().addCanvas3D(offScreenCanvas); } BranchGroup scene = createSceneGraph(); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); view = u.getViewer().getView(); add("East", guiPanel()); } View view; JPanel guiPanel() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(0, 1)); panel.add(new JLabel("Right Shoulder rotation")); rShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rShoulderRot); rShoulderSlider.addChangeListener(this); // we somehow skipped this the first time around...?! rShoulderSliderLabel = new JLabel(Integer.toString(rShoulderRot)); panel.add(rShoulderSlider); // this was for the right shoulder rotation // Human_r_elbow rotation panel.add(new JLabel("Right Elbow rotation")); rElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rElbowRot); rElbowSlider.addChangeListener(this); rElbowSliderLabel = new JLabel(Integer.toString(rElbowRot)); panel.add(rElbowSlider); panel.add(rElbowSliderLabel); // Human_l_shoulder rotation panel.add(new JLabel("Left Shoulder rotation")); lShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lShoulderRot); lShoulderSlider.addChangeListener(this); lShoulderSliderLabel = new JLabel(Integer.toString(lShoulderRot)); panel.add(lShoulderSlider); panel.add(lShoulderSliderLabel); // Human_l_elbow rotation panel.add(new JLabel("Left Elbow rotation")); lElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lElbowRot); lElbowSlider.addChangeListener(this); lElbowSliderLabel = new JLabel(Integer.toString(lElbowRot)); panel.add(lElbowSlider); panel.add(rElbowSliderLabel); if (isApplication) { JButton snapButton = new JButton(snapImageString); snapButton.setActionCommand(snapImageString); snapButton.addActionListener(this); panel.add(snapButton); } return panel; } String snapImageString = "Snap Image"; int rShoulderRot = 0; JSlider rShoulderSlider; JLabel rShoulderSliderLabel; int lShoulderRot = 0; JSlider lShoulderSlider; JLabel lShoulderSliderLabel; int rElbowRot = 0; JSlider rElbowSlider; JLabel rElbowSliderLabel; int lElbowRot = 0; JSlider lElbowSlider; JLabel lElbowSliderLabel; BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); TransformGroup objScale = new TransformGroup(); Transform3D scaleTrans = new Transform3D(); scaleTrans.set(1 / 3.5f); objScale.setTransform(scaleTrans); objRoot.addChild(objScale); TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objScale.addChild(objTrans); createHuman(); objTrans.addChild(Human_body); BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0); Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f)); bg.setApplicationBounds(bounds); objTrans.addChild(bg); MouseRotate mr = new MouseRotate(); mr.setTransformGroup(objTrans); mr.setSchedulingBounds(bounds); mr.setFactor(0.007); objTrans.addChild(mr); Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f); AmbientLight ambientLightNode = new AmbientLight(ambientColor); ambientLightNode.setInfluencingBounds(bounds); objRoot.addChild(ambientLightNode); Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f); Vector3f light1Direction = new Vector3f(0.0f, -0.2f, -1.0f); DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); objRoot.addChild(light1); return objRoot; } TransformGroup Human_body; Vector3f tmpVector = new Vector3f(); Transform3D tmpTrans = new Transform3D(); Color3f red = new Color3f(1.0f, 0.0f, 0.0f); Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Color3f white = new Color3f(1.0f, 1.0f, 1.0f); TransformGroup tmpTG; TransformGroup Human_r_shoulder; Cylinder tmpCyl; Sphere tmpSphere; void createHuman() { Human_body = new TransformGroup(); tmpVector.set(0.0f, -1.5f, 0.0f); tmpTrans.set(tmpVector); Human_body.setTransform(tmpTrans); Material material = new Material(red, black, red, white, 64); Appearance appearance = new Appearance(); appearance.setMaterial(material); tmpTG = new TransformGroup(); tmpVector.set(0.0f, 1.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.75f, 3.0f, appearance); tmpTG.addChild(tmpCyl); Human_body.addChild(tmpTG); Human_r_shoulder = new TransformGroup(); Human_r_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Human_r_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tmpVector.set(-0.95f, 2.9f, -0.2f); tmpTrans.set(tmpVector); Human_r_shoulder.setTransform(tmpTrans); tmpSphere = new Sphere(0.22f, appearance); Human_r_shoulder.addChild(tmpSphere); tmpTG = new TransformGroup(); tmpVector.set(0.0f, -0.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.2f, 1.0f, appearance); tmpTG.addChild(tmpCyl); Human_r_shoulder.addChild(tmpTG); Human_body.addChild(Human_r_shoulder); // create the r_elbow TransformGroup Human_r_elbow = new TransformGroup(); Human_r_elbow.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Human_r_elbow.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tmpVector.set(0.0f, -1.054f, 0.0f); tmpTrans.set(tmpVector); Human_r_elbow.setTransform(tmpTrans); // place the sphere for the r_elbow tmpSphere = new Sphere(0.22f, appearance); Human_r_elbow.addChild(tmpSphere); // offset and place the cylinder for the r_shoulder tmpTG = new TransformGroup(); // offset the shape tmpVector.set(0.0f,-0.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.2f, 1.0f, appearance); tmpTG.addChild(tmpCyl); // add the shape to the r_shoulder Human_r_elbow.addChild(tmpTG); // add the elbow to the shoulder group Human_r_shoulder.addChild(Human_r_elbow); // create the l_shoulder TransformGroup Human_l_shoulder = new TransformGroup(); Human_l_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Human_l_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tmpVector.set( 0.95f, 2.9f,-0.2f); tmpTrans.set(tmpVector); Human_l_shoulder.setTransform(tmpTrans); // place the sphere for the l_shoulder tmpSphere = new Sphere(0.22f, appearance); Human_l_shoulder.addChild(tmpSphere); // offset and place the cylinder for the l_shoulder tmpTG = new TransformGroup(); // offset the shape tmpVector.set(0.0f,-0.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.2f, 1.0f, appearance); tmpTG.addChild(tmpCyl); // add the shape to the l_shoulder Human_l_shoulder.addChild(tmpTG); // add the shoulder to the body group Human_body.addChild(Human_l_shoulder); // create the r_elbow TransformGroup Human_l_elbow = new TransformGroup(); Human_l_elbow.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Human_l_elbow.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tmpVector.set(0.0f, -1.054f, 0.0f); tmpTrans.set(tmpVector); Human_l_elbow.setTransform(tmpTrans); // place the sphere for the l_elbow tmpSphere = new Sphere(0.22f, appearance); Human_l_elbow.addChild(tmpSphere); // offset and place the cylinder for the l_elbow tmpTG = new TransformGroup(); // offset the shape tmpVector.set(0.0f,-0.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.2f, 1.0f, appearance); tmpTG.addChild(tmpCyl); // add the shape to the l_elbow Human_l_elbow.addChild(tmpTG); // add the shoulder to the body group Human_l_shoulder.addChild(Human_l_elbow); // create the skullbase TransformGroup Human_skullbase = new TransformGroup(); tmpVector.set( 0.0f, 3.632f, 0.0f); tmpTrans.set(tmpVector); Human_skullbase.setTransform(tmpTrans); // offset and place the sphere for the skull tmpSphere = new Sphere(0.5f, appearance); // add the shape to the l_shoulder Human_skullbase.addChild(tmpSphere); // add the shoulder to the body group Human_body.addChild(Human_skullbase); } TransformGroup Human_r_elbow; public void destroy() { u.removeAllLocales(); } }
Minor fixes to the interface (can you find them?) and we're finished:import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; import javax.swing.*; import javax.vecmath.*; import com.sun.j3d.utils.behaviors.mouse.*; import com.sun.j3d.utils.geometry.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { JSlider source = (JSlider)e.getSource(); int value = source.getValue(); if (source == rShoulderSlider) { setRShoulderRot(value); rShoulderSliderLabel.setText(Integer.toString(value)); } else if (source == rElbowSlider) { setRElbowRot(value); rElbowSliderLabel.setText(Integer.toString(value)); } else if (source == lShoulderSlider) { setLShoulderRot(value); lShoulderSliderLabel.setText(Integer.toString(value)); } else if (source == lElbowSlider) { setLElbowRot(value); lElbowSliderLabel.setText(Integer.toString(value)); } } TransformGroup Human_l_shoulder; TransformGroup Human_l_elbow; TransformGroup Human_skullbase; AxisAngle4f rElbowAA = new AxisAngle4f(0.0f, 0.0f,-1.0f, 0.0f); AxisAngle4f lShoulderAA = new AxisAngle4f(0.0f, 0.0f,1.0f, 0.0f); AxisAngle4f lElbowAA = new AxisAngle4f(0.0f, 0.0f,1.0f, 0.0f); public void setRElbowRot(int rotation) { float angle = (float) Math.toRadians(rotation); rElbowRot = rotation; rElbowAA.angle = (float) Math.toRadians(rElbowRot); Human_r_elbow.getTransform(tmpTrans); tmpTrans.setRotation(rElbowAA); Human_r_elbow.setTransform(tmpTrans); } public void setLShoulderRot(int rotation) { lShoulderRot = rotation; lShoulderAA.angle = (float) Math.toRadians(lShoulderRot); Human_l_shoulder.getTransform(tmpTrans); tmpTrans.setRotation(lShoulderAA); Human_l_shoulder.setTransform(tmpTrans); } public void setLElbowRot(int rotation) { float angle = (float) Math.toRadians(rotation); lElbowRot = rotation; lElbowAA.angle = (float) Math.toRadians(lElbowRot); Human_l_elbow.getTransform(tmpTrans); tmpTrans.setRotation(lElbowAA); Human_l_elbow.setTransform(tmpTrans); } public void setRShoulderRot(int rotation) { System.out.println("I am changing the r shoulder slider..."); rShoulderRot = rotation; rShoulderAA.angle = (float) Math.toRadians(rShoulderRot); Human_r_shoulder.getTransform(tmpTrans); tmpTrans.setRotation(rShoulderAA); Human_r_shoulder.setTransform(tmpTrans); } AxisAngle4f rShoulderAA = new AxisAngle4f(0.0f, 0.0f, -1.0f, 0.0f); public void actionPerformed(ActionEvent e) { System.out.println("action performed has been called..."); } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; OffScreenCanvas3D offScreenCanvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); add("Center", canvas); u = new SimpleUniverse(canvas); if (isApplication) { offScreenCanvas = new OffScreenCanvas3D(config, true); Screen3D sOn = canvas.getScreen3D(); Screen3D sOff = offScreenCanvas.getScreen3D(); Dimension dim = sOn.getSize(); dim.width *= offScreenScale; dim.height *= offScreenScale; sOff.setSize(dim); sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * offScreenScale); sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * offScreenScale); u.getViewer().getView().addCanvas3D(offScreenCanvas); } BranchGroup scene = createSceneGraph(); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); view = u.getViewer().getView(); add("East", guiPanel()); } View view; JPanel guiPanel() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(0, 1)); panel.add(new JLabel("Right Shoulder rotation")); rShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rShoulderRot); rShoulderSlider.addChangeListener(this); // we somehow skipped this the first time around...?! rShoulderSliderLabel = new JLabel(Integer.toString(rShoulderRot)); panel.add(rShoulderSlider); // this was for the right shoulder rotation // Human_r_elbow rotation panel.add(new JLabel("Right Elbow rotation")); rElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rElbowRot); rElbowSlider.addChangeListener(this); rElbowSliderLabel = new JLabel(Integer.toString(rElbowRot)); panel.add(rElbowSlider); panel.add(rElbowSliderLabel); // Human_l_shoulder rotation panel.add(new JLabel("Left Shoulder rotation")); lShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lShoulderRot); lShoulderSlider.addChangeListener(this); lShoulderSliderLabel = new JLabel(Integer.toString(lShoulderRot)); panel.add(lShoulderSlider); panel.add(lShoulderSliderLabel); // Human_l_elbow rotation panel.add(new JLabel("Left Elbow rotation")); lElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lElbowRot); lElbowSlider.addChangeListener(this); lElbowSliderLabel = new JLabel(Integer.toString(lElbowRot)); panel.add(lElbowSlider); panel.add(rElbowSliderLabel); if (isApplication) { JButton snapButton = new JButton(snapImageString); snapButton.setActionCommand(snapImageString); snapButton.addActionListener(this); panel.add(snapButton); } return panel; } String snapImageString = "Snap Image"; int rShoulderRot = 0; JSlider rShoulderSlider; JLabel rShoulderSliderLabel; int lShoulderRot = 0; JSlider lShoulderSlider; JLabel lShoulderSliderLabel; int rElbowRot = 0; JSlider rElbowSlider; JLabel rElbowSliderLabel; int lElbowRot = 0; JSlider lElbowSlider; JLabel lElbowSliderLabel; BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); TransformGroup objScale = new TransformGroup(); Transform3D scaleTrans = new Transform3D(); scaleTrans.set(1 / 3.5f); objScale.setTransform(scaleTrans); objRoot.addChild(objScale); TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objScale.addChild(objTrans); createHuman(); objTrans.addChild(Human_body); BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0); Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f)); bg.setApplicationBounds(bounds); objTrans.addChild(bg); MouseRotate mr = new MouseRotate(); mr.setTransformGroup(objTrans); mr.setSchedulingBounds(bounds); mr.setFactor(0.007); objTrans.addChild(mr); Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f); AmbientLight ambientLightNode = new AmbientLight(ambientColor); ambientLightNode.setInfluencingBounds(bounds); objRoot.addChild(ambientLightNode); Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f); Vector3f light1Direction = new Vector3f(0.0f, -0.2f, -1.0f); DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); objRoot.addChild(light1); return objRoot; } TransformGroup Human_body; Vector3f tmpVector = new Vector3f(); Transform3D tmpTrans = new Transform3D(); Color3f red = new Color3f(1.0f, 0.0f, 0.0f); Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Color3f white = new Color3f(1.0f, 1.0f, 1.0f); TransformGroup tmpTG; TransformGroup Human_r_shoulder; Cylinder tmpCyl; Sphere tmpSphere; void createHuman() { Human_body = new TransformGroup(); tmpVector.set(0.0f, -1.5f, 0.0f); tmpTrans.set(tmpVector); Human_body.setTransform(tmpTrans); Material material = new Material(red, black, red, white, 64); Appearance appearance = new Appearance(); appearance.setMaterial(material); tmpTG = new TransformGroup(); tmpVector.set(0.0f, 1.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.75f, 3.0f, appearance); tmpTG.addChild(tmpCyl); Human_body.addChild(tmpTG); Human_r_shoulder = new TransformGroup(); Human_r_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Human_r_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tmpVector.set(-0.95f, 2.9f, -0.2f); tmpTrans.set(tmpVector); Human_r_shoulder.setTransform(tmpTrans); tmpSphere = new Sphere(0.22f, appearance); Human_r_shoulder.addChild(tmpSphere); tmpTG = new TransformGroup(); tmpVector.set(0.0f, -0.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.2f, 1.0f, appearance); tmpTG.addChild(tmpCyl); Human_r_shoulder.addChild(tmpTG); Human_body.addChild(Human_r_shoulder); // create the r_elbow TransformGroup Human_r_elbow = new TransformGroup(); Human_r_elbow.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Human_r_elbow.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tmpVector.set(0.0f, -1.054f, 0.0f); tmpTrans.set(tmpVector); Human_r_elbow.setTransform(tmpTrans); // place the sphere for the r_elbow tmpSphere = new Sphere(0.22f, appearance); Human_r_elbow.addChild(tmpSphere); // offset and place the cylinder for the r_shoulder tmpTG = new TransformGroup(); // offset the shape tmpVector.set(0.0f,-0.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.2f, 1.0f, appearance); tmpTG.addChild(tmpCyl); // add the shape to the r_shoulder Human_r_elbow.addChild(tmpTG); // add the elbow to the shoulder group Human_r_shoulder.addChild(Human_r_elbow); // create the l_shoulder TransformGroup Human_l_shoulder = new TransformGroup(); Human_l_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Human_l_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tmpVector.set( 0.95f, 2.9f,-0.2f); tmpTrans.set(tmpVector); Human_l_shoulder.setTransform(tmpTrans); // place the sphere for the l_shoulder tmpSphere = new Sphere(0.22f, appearance); Human_l_shoulder.addChild(tmpSphere); // offset and place the cylinder for the l_shoulder tmpTG = new TransformGroup(); // offset the shape tmpVector.set(0.0f,-0.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.2f, 1.0f, appearance); tmpTG.addChild(tmpCyl); // add the shape to the l_shoulder Human_l_shoulder.addChild(tmpTG); // add the shoulder to the body group Human_body.addChild(Human_l_shoulder); // create the r_elbow TransformGroup Human_l_elbow = new TransformGroup(); Human_l_elbow.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Human_l_elbow.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tmpVector.set(0.0f, -1.054f, 0.0f); tmpTrans.set(tmpVector); Human_l_elbow.setTransform(tmpTrans); // place the sphere for the l_elbow tmpSphere = new Sphere(0.22f, appearance); Human_l_elbow.addChild(tmpSphere); // offset and place the cylinder for the l_elbow tmpTG = new TransformGroup(); // offset the shape tmpVector.set(0.0f,-0.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.2f, 1.0f, appearance); tmpTG.addChild(tmpCyl); // add the shape to the l_elbow Human_l_elbow.addChild(tmpTG); // add the shoulder to the body group Human_l_shoulder.addChild(Human_l_elbow); // create the skullbase TransformGroup Human_skullbase = new TransformGroup(); tmpVector.set( 0.0f, 3.632f, 0.0f); tmpTrans.set(tmpVector); Human_skullbase.setTransform(tmpTrans); // offset and place the sphere for the skull tmpSphere = new Sphere(0.5f, appearance); // add the shape to the l_shoulder Human_skullbase.addChild(tmpSphere); // add the shoulder to the body group Human_body.addChild(Human_skullbase); } TransformGroup Human_r_elbow; public void destroy() { u.removeAllLocales(); } }
import java.applet.Applet; import com.sun.j3d.utils.applet.MainFrame; import javax.swing.event.*; import java.awt.event.*; import com.sun.j3d.utils.universe.*; import java.text.*; import java.awt.*; import javax.media.j3d.*; import javax.swing.*; import javax.vecmath.*; import com.sun.j3d.utils.behaviors.mouse.*; import com.sun.j3d.utils.geometry.*; public class Example extends Applet implements ChangeListener, ActionListener { public static void main(String[] args) { float initOffScreenScale = 2.5f; for (int i = 0; i < args.length; i++) { if (args[i].equals("-s")) { if (args.length >= (i+1)) { initOffScreenScale = Float.parseFloat(args[i+1]); i++; } } } new MainFrame(new Example(true, initOffScreenScale), 950, 600); } public void stateChanged(ChangeEvent e) { JSlider source = (JSlider)e.getSource(); int value = source.getValue(); if (source == rShoulderSlider) { setRShoulderRot(value); rShoulderSliderLabel.setText(Integer.toString(value)); } else if (source == rElbowSlider) { setRElbowRot(value); rElbowSliderLabel.setText(Integer.toString(value)); } else if (source == lShoulderSlider) { setLShoulderRot(value); lShoulderSliderLabel.setText(Integer.toString(value)); } else if (source == lElbowSlider) { setLElbowRot(value); lElbowSliderLabel.setText(Integer.toString(value)); } } TransformGroup Human_l_shoulder; TransformGroup Human_l_elbow; TransformGroup Human_skullbase; AxisAngle4f rElbowAA = new AxisAngle4f(0.0f, 0.0f,-1.0f, 0.0f); AxisAngle4f lShoulderAA = new AxisAngle4f(0.0f, 0.0f,1.0f, 0.0f); AxisAngle4f lElbowAA = new AxisAngle4f(0.0f, 0.0f,1.0f, 0.0f); public void setRElbowRot(int rotation) { float angle = (float) Math.toRadians(rotation); rElbowRot = rotation; rElbowAA.angle = (float) Math.toRadians(rElbowRot); Human_r_elbow.getTransform(tmpTrans); tmpTrans.setRotation(rElbowAA); Human_r_elbow.setTransform(tmpTrans); } public void setLShoulderRot(int rotation) { lShoulderRot = rotation; lShoulderAA.angle = (float) Math.toRadians(lShoulderRot); Human_l_shoulder.getTransform(tmpTrans); tmpTrans.setRotation(lShoulderAA); Human_l_shoulder.setTransform(tmpTrans); } public void setLElbowRot(int rotation) { float angle = (float) Math.toRadians(rotation); lElbowRot = rotation; lElbowAA.angle = (float) Math.toRadians(lElbowRot); Human_l_elbow.getTransform(tmpTrans); tmpTrans.setRotation(lElbowAA); Human_l_elbow.setTransform(tmpTrans); } public void setRShoulderRot(int rotation) { rShoulderRot = rotation; rShoulderAA.angle = (float) Math.toRadians(rShoulderRot); Human_r_shoulder.getTransform(tmpTrans); tmpTrans.setRotation(rShoulderAA); Human_r_shoulder.setTransform(tmpTrans); } AxisAngle4f rShoulderAA = new AxisAngle4f(0.0f, 0.0f, -1.0f, 0.0f); public void actionPerformed(ActionEvent e) { System.out.println("action performed has been called..."); } boolean isApplication; float offScreenScale = 1.5f; public Example(boolean isApplication, float initOffScreenScale) { this.isApplication = isApplication; this.offScreenScale = offScreenScale; } public Example() { this(false, 1.0f); } SimpleUniverse u; NumberFormat nf; Canvas3D canvas; OffScreenCanvas3D offScreenCanvas; public void init() { nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); canvas = new Canvas3D(config); add("Center", canvas); u = new SimpleUniverse(canvas); if (isApplication) { offScreenCanvas = new OffScreenCanvas3D(config, true); Screen3D sOn = canvas.getScreen3D(); Screen3D sOff = offScreenCanvas.getScreen3D(); Dimension dim = sOn.getSize(); dim.width *= offScreenScale; dim.height *= offScreenScale; sOff.setSize(dim); sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * offScreenScale); sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * offScreenScale); u.getViewer().getView().addCanvas3D(offScreenCanvas); } BranchGroup scene = createSceneGraph(); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); view = u.getViewer().getView(); add("East", guiPanel()); } View view; JPanel guiPanel() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(0, 1)); panel.add(new JLabel("Right Shoulder rotation")); rShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rShoulderRot); rShoulderSlider.addChangeListener(this); // we somehow skipped this the first time around...?! rShoulderSliderLabel = new JLabel(Integer.toString(rShoulderRot)); panel.add(rShoulderSlider); // this was for the right shoulder rotation panel.add(rShoulderSliderLabel); // this was for the right shoulder rotation // Human_r_elbow rotation panel.add(new JLabel("Right Elbow rotation")); rElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, rElbowRot); rElbowSlider.addChangeListener(this); rElbowSliderLabel = new JLabel(Integer.toString(rElbowRot)); panel.add(rElbowSlider); panel.add(rElbowSliderLabel); // Human_l_shoulder rotation panel.add(new JLabel("Left Shoulder rotation")); lShoulderSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lShoulderRot); lShoulderSlider.addChangeListener(this); lShoulderSliderLabel = new JLabel(Integer.toString(lShoulderRot)); panel.add(lShoulderSlider); panel.add(lShoulderSliderLabel); // Human_l_elbow rotation panel.add(new JLabel("Left Elbow rotation")); lElbowSlider = new JSlider(JSlider.HORIZONTAL, 0, 180, lElbowRot); lElbowSlider.addChangeListener(this); lElbowSliderLabel = new JLabel(Integer.toString(lElbowRot)); panel.add(lElbowSlider); panel.add(lElbowSliderLabel); if (isApplication) { JButton snapButton = new JButton(snapImageString); snapButton.setActionCommand(snapImageString); snapButton.addActionListener(this); panel.add(snapButton); } return panel; } String snapImageString = "Snap Image"; int rShoulderRot = 0; JSlider rShoulderSlider; JLabel rShoulderSliderLabel; int lShoulderRot = 0; JSlider lShoulderSlider; JLabel lShoulderSliderLabel; int rElbowRot = 0; JSlider rElbowSlider; JLabel rElbowSliderLabel; int lElbowRot = 0; JSlider lElbowSlider; JLabel lElbowSliderLabel; BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); TransformGroup objScale = new TransformGroup(); Transform3D scaleTrans = new Transform3D(); scaleTrans.set(1 / 3.5f); objScale.setTransform(scaleTrans); objRoot.addChild(objScale); TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objScale.addChild(objTrans); createHuman(); objTrans.addChild(Human_body); BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0); Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f)); bg.setApplicationBounds(bounds); objTrans.addChild(bg); MouseRotate mr = new MouseRotate(); mr.setTransformGroup(objTrans); mr.setSchedulingBounds(bounds); mr.setFactor(0.007); objTrans.addChild(mr); Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f); AmbientLight ambientLightNode = new AmbientLight(ambientColor); ambientLightNode.setInfluencingBounds(bounds); objRoot.addChild(ambientLightNode); Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f); Vector3f light1Direction = new Vector3f(0.0f, -0.2f, -1.0f); DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); objRoot.addChild(light1); return objRoot; } TransformGroup Human_body; Vector3f tmpVector = new Vector3f(); Transform3D tmpTrans = new Transform3D(); Color3f red = new Color3f(1.0f, 0.0f, 0.0f); Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Color3f white = new Color3f(1.0f, 1.0f, 1.0f); TransformGroup tmpTG; TransformGroup Human_r_shoulder; Cylinder tmpCyl; Sphere tmpSphere; void createHuman() { Human_body = new TransformGroup(); tmpVector.set(0.0f, -1.5f, 0.0f); tmpTrans.set(tmpVector); Human_body.setTransform(tmpTrans); Material material = new Material(red, black, red, white, 64); Appearance appearance = new Appearance(); appearance.setMaterial(material); tmpTG = new TransformGroup(); tmpVector.set(0.0f, 1.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.75f, 3.0f, appearance); tmpTG.addChild(tmpCyl); Human_body.addChild(tmpTG); Human_r_shoulder = new TransformGroup(); Human_r_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Human_r_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tmpVector.set(-0.95f, 2.9f, -0.2f); tmpTrans.set(tmpVector); Human_r_shoulder.setTransform(tmpTrans); tmpSphere = new Sphere(0.22f, appearance); Human_r_shoulder.addChild(tmpSphere); tmpTG = new TransformGroup(); tmpVector.set(0.0f, -0.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.2f, 1.0f, appearance); tmpTG.addChild(tmpCyl); Human_r_shoulder.addChild(tmpTG); Human_body.addChild(Human_r_shoulder); // create the r_elbow TransformGroup Human_r_elbow = new TransformGroup(); Human_r_elbow.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Human_r_elbow.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tmpVector.set(0.0f, -1.054f, 0.0f); tmpTrans.set(tmpVector); Human_r_elbow.setTransform(tmpTrans); // place the sphere for the r_elbow tmpSphere = new Sphere(0.22f, appearance); Human_r_elbow.addChild(tmpSphere); // offset and place the cylinder for the r_shoulder tmpTG = new TransformGroup(); // offset the shape tmpVector.set(0.0f,-0.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.2f, 1.0f, appearance); tmpTG.addChild(tmpCyl); // add the shape to the r_shoulder Human_r_elbow.addChild(tmpTG); // add the elbow to the shoulder group Human_r_shoulder.addChild(Human_r_elbow); // create the l_shoulder TransformGroup Human_l_shoulder = new TransformGroup(); Human_l_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Human_l_shoulder.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tmpVector.set( 0.95f, 2.9f,-0.2f); tmpTrans.set(tmpVector); Human_l_shoulder.setTransform(tmpTrans); // place the sphere for the l_shoulder tmpSphere = new Sphere(0.22f, appearance); Human_l_shoulder.addChild(tmpSphere); // offset and place the cylinder for the l_shoulder tmpTG = new TransformGroup(); // offset the shape tmpVector.set(0.0f,-0.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.2f, 1.0f, appearance); tmpTG.addChild(tmpCyl); // add the shape to the l_shoulder Human_l_shoulder.addChild(tmpTG); // add the shoulder to the body group Human_body.addChild(Human_l_shoulder); // create the r_elbow TransformGroup Human_l_elbow = new TransformGroup(); Human_l_elbow.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Human_l_elbow.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tmpVector.set(0.0f, -1.054f, 0.0f); tmpTrans.set(tmpVector); Human_l_elbow.setTransform(tmpTrans); // place the sphere for the l_elbow tmpSphere = new Sphere(0.22f, appearance); Human_l_elbow.addChild(tmpSphere); // offset and place the cylinder for the l_elbow tmpTG = new TransformGroup(); // offset the shape tmpVector.set(0.0f,-0.5f, 0.0f); tmpTrans.set(tmpVector); tmpTG.setTransform(tmpTrans); tmpCyl = new Cylinder(0.2f, 1.0f, appearance); tmpTG.addChild(tmpCyl); // add the shape to the l_elbow Human_l_elbow.addChild(tmpTG); // add the shoulder to the body group Human_l_shoulder.addChild(Human_l_elbow); // create the skullbase TransformGroup Human_skullbase = new TransformGroup(); tmpVector.set( 0.0f, 3.632f, 0.0f); tmpTrans.set(tmpVector); Human_skullbase.setTransform(tmpTrans); // offset and place the sphere for the skull tmpSphere = new Sphere(0.5f, appearance); // add the shape to the l_shoulder Human_skullbase.addChild(tmpSphere); // add the shoulder to the body group Human_body.addChild(Human_skullbase); } TransformGroup Human_r_elbow; public void destroy() { u.removeAllLocales(); } }