package cube_demo; import com.sun.j3d.utils.behaviors.vp.OrbitBehavior; import com.sun.j3d.utils.behaviors.vp.ViewPlatformBehavior; import com.sun.j3d.utils.geometry.Sphere; import com.sun.j3d.utils.image.TextureLoader; import com.sun.j3d.utils.universe.SimpleUniverse; import com.sun.j3d.utils.universe.ViewingPlatform; import de.spacecontrol.sc.dllwrapper.ScDllWrapper; import de.spacecontrol.sc.dllwrapper.ScEx; import de.spacecontrol.sc.dllwrapper.ScStdData; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.GraphicsConfiguration; import java.awt.Image; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.lang.management.ManagementFactory; import java.util.ArrayList; import javax.media.j3d.AmbientLight; import javax.media.j3d.Appearance; import javax.media.j3d.Background; import javax.media.j3d.BoundingSphere; import javax.media.j3d.Bounds; import javax.media.j3d.BranchGroup; import javax.media.j3d.Canvas3D; import javax.media.j3d.Node; import javax.media.j3d.TextureAttributes; import javax.media.j3d.Transform3D; import javax.media.j3d.TransformGroup; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.vecmath.AxisAngle4d; import javax.vecmath.Color4f; import javax.vecmath.Matrix4d; import javax.vecmath.Point3d; import javax.vecmath.Vector3d; public class CubeApp extends JFrame { private final int mBoundsRange = 1000; private int mSceneNo; private double mHandSpan; private double mHandAngle; private ReadingThread mReadingThread = new ReadingThread(this); private int mDevIdx; private TransformGroup mCubeTfGrp = new TransformGroup(); private TransformGroup mMirrCubeTfGrp = new TransformGroup(); private Transform3D mCubeTf = new Transform3D(); private Transform3D mCubeInvTf = new Transform3D(); private Transform3D mTf = new Transform3D(); private Transform3D mRotTf = new Transform3D(); private Transform3D mMirrTf = new Transform3D(); private Transform3D mMirrXzTf = new Transform3D(); private Vector3d mV = new Vector3d(); private Matrix4d mMatrix4d = new Matrix4d(); private Vector3d mXAxis = new Vector3d(); private Vector3d mYAxis = new Vector3d(); private Vector3d mZAxis = new Vector3d(); private final Vector3d mUx = new Vector3d(1.0D, 0.0D, 0.0D); private final Vector3d mUy = new Vector3d(0.0D, 1.0D, 0.0D); private final Vector3d mUz = new Vector3d(0.0D, 0.0D, 1.0D); private AxisAngle4d mXAxisAngle = new AxisAngle4d(); private AxisAngle4d mYAxisAngle = new AxisAngle4d(); private AxisAngle4d mZAxisAngle = new AxisAngle4d(); private double mX; private double mY; private double mZ; private ViewingPlatform mVp; private JPanel mGraphicPanel; public int getDevIdx() { return this.mDevIdx; } public CubeApp() { GraphicsConfiguration graphicsConfiguration = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas3D = new Canvas3D(graphicsConfiguration); SimpleUniverse simpleUniverse = new SimpleUniverse(canvas3D); BranchGroup branchGroup = null; OrbitBehavior orbitBehavior = new OrbitBehavior(canvas3D); ArrayList arrayList = new ArrayList(); String str1 = System.getProperty("os.name"); boolean bool = str1.contains("OS X"); this.mVp = simpleUniverse.getViewingPlatform(); Image image1 = getToolkit().getImage(getClass().getResource("/pics/cube_16.png")); Image image2 = getToolkit().getImage(getClass().getResource("/pics/cube_32.png")); Image image3 = getToolkit().getImage(getClass().getResource("/pics/cube_64.png")); arrayList.add(image1); arrayList.add(image2); arrayList.add(image3); setIconImages(arrayList); try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (UnsupportedLookAndFeelException unsupportedLookAndFeelException) { unsupportedLookAndFeelException.printStackTrace(); } catch (IllegalAccessException illegalAccessException) { illegalAccessException.printStackTrace(); } catch (InstantiationException instantiationException) { instantiationException.printStackTrace(); } catch (ClassNotFoundException classNotFoundException) { classNotFoundException.printStackTrace(); } initComponents(); String str2 = bool ? getClass().getSimpleName() : getTitle(); this.mGraphicPanel.add("Center", (Component)canvas3D); simpleUniverse.getViewer().getView().setBackClipDistance(1000.0D); branchGroup = createCubeSceneGraph(); setViewPos(this.mVp); orbitBehavior.setSchedulingBounds((Bounds)new BoundingSphere(new Point3d(), 1000.0D)); orbitBehavior.setZoomFactor(5.0D); this.mVp.setViewPlatformBehavior((ViewPlatformBehavior)orbitBehavior); this.mMatrix4d.setIdentity(); this.mMatrix4d.setM11(-1.0D); this.mMirrXzTf.set(this.mMatrix4d); branchGroup.compile(); simpleUniverse.addBranchGraph(branchGroup); this.mGraphicPanel.requestFocus(); try { String str3 = ManagementFactory.getRuntimeMXBean().getName(); int i = str3.indexOf('@'); String str4 = str3.substring(0, i); ScDllWrapper.scConnect2(false, str2); this.mReadingThread.start(); } catch (ScEx scEx) { JOptionPane.showMessageDialog(this.mGraphicPanel, "No connection to SpaceControl driver!\nMove the cube with keys 1-6 and shift.", "Connection Error", 0); } } private void setViewPos(ViewingPlatform paramViewingPlatform) { TransformGroup transformGroup = paramViewingPlatform.getViewPlatformTransform(); Transform3D transform3D1 = new Transform3D(); Transform3D transform3D2 = new Transform3D(); transformGroup.getTransform(transform3D2); transform3D2.rotY(Math.toRadians(0.0D)); transform3D2.rotX(Math.toRadians(-5.0D)); transform3D1.mul(transform3D2); transform3D2.setTranslation(new Vector3d(0.0D, 5.0D, 18.0D)); transform3D1.mul(transform3D2); transformGroup.setTransform(transform3D1); } private BranchGroup createCubeSceneGraph() { BranchGroup branchGroup1 = new BranchGroup(); BranchGroup branchGroup2 = new BranchGroup(); TransformGroup transformGroup = new TransformGroup(); Floor floor = new Floor(); FloorShade floorShade1 = new FloorShade(0, 16.0D, 8.0D, this.mGraphicPanel); FloorShade floorShade2 = new FloorShade(1, 16.0D, 8.0D, this.mGraphicPanel); FloorShade floorShade3 = new FloorShade(2, 16.0D, 8.0D, this.mGraphicPanel); FloorShade floorShade4 = new FloorShade(3, 16.0D, 8.0D, this.mGraphicPanel); FloorBack floorBack = new FloorBack(16.0D, 16.0D, -8.0D); AmbientLight ambientLight = new AmbientLight(); Appearance appearance1 = new Appearance(); Lamp lamp1 = new Lamp(4.5D, 0.0D, 4.5D, 0.25D, 5.0D, false); Lamp lamp2 = new Lamp(-4.5D, 0.0D, -4.5D, 0.25D, 5.0D, false); Lamp lamp3 = new Lamp(-4.5D, 0.0D, -4.5D, 0.25D, 5.0D, true); Lamp lamp4 = new Lamp(4.5D, 0.0D, 4.5D, 0.25D, 5.0D, true); Background background = new Background(); ColoredCube coloredCube1 = new ColoredCube(0.4D, false); ColoredCube coloredCube2 = new ColoredCube(0.4D, true); Axes axes = new Axes(-8.0D, 0.2D, -8.0D); TextureLoader textureLoader = new TextureLoader("src/pics/halebop.png", this); Appearance appearance2 = new Appearance(); Sphere sphere = new Sphere(1.0F, 6, 50, appearance2); branchGroup2.addChild((Node)background); branchGroup2.addChild((Node)floor); branchGroup2.addChild((Node)floorShade1); branchGroup2.addChild((Node)floorShade2); branchGroup2.addChild((Node)floorShade3); branchGroup2.addChild((Node)floorShade4); branchGroup2.addChild((Node)floorBack); branchGroup2.addChild((Node)lamp1.getBrGrp()); branchGroup2.addChild((Node)lamp2.getBrGrp()); branchGroup2.addChild((Node)lamp3.getBrGrp()); branchGroup2.addChild((Node)lamp4.getBrGrp()); branchGroup2.addChild((Node)axes.getBrGrp()); branchGroup2.addChild((Node)this.mCubeTfGrp); branchGroup2.addChild((Node)this.mMirrCubeTfGrp); branchGroup2.addChild((Node)ambientLight); appearance2.setTexture(textureLoader.getTexture()); appearance2.setTextureAttributes(new TextureAttributes(2, new Transform3D(), new Color4f(), 1)); branchGroup1.addChild((Node)new Stars()); branchGroup1.addChild((Node)sphere); background.setApplicationBounds((Bounds)new BoundingSphere(new Point3d(), 1000.0D)); background.setGeometry(branchGroup1); this.mCubeTfGrp.addChild((Node)coloredCube1.getBrGrp()); this.mMirrCubeTfGrp.addChild((Node)coloredCube2.getBrGrp()); this.mCubeTfGrp.setCapability(18); this.mMirrCubeTfGrp.setCapability(18); ambientLight.setInfluencingBounds((Bounds)new BoundingSphere(new Point3d(0.0D, 0.0D, 0.0D), 20.0D)); return branchGroup2; } public void repaintDemo(ScStdData paramScStdData) { double d7 = 500.0D; if (!isShowing()) return; double d1 = paramScStdData.mA / d7; double d2 = paramScStdData.mB / d7; double d3 = paramScStdData.mC / d7; double d4 = paramScStdData.mX / d7; double d5 = paramScStdData.mY / d7; double d6 = paramScStdData.mZ / d7; this.mX += d4; this.mY += d5; this.mZ += d6; calcScreenRotsRelativeToCube(d1, d2, d3); this.mRotTf.setIdentity(); this.mTf.setIdentity(); this.mTf.setRotation(this.mXAxisAngle); this.mRotTf.mul(this.mTf); this.mTf.setIdentity(); this.mTf.setRotation(this.mYAxisAngle); this.mRotTf.mul(this.mTf); this.mTf.setIdentity(); this.mTf.setRotation(this.mZAxisAngle); this.mRotTf.mul(this.mTf); this.mCubeTf.mul(this.mRotTf); if (this.mX > 7.0D) this.mX = 7.0D; if (this.mX < -7.0D) this.mX = -7.0D; if (this.mZ > 7.0D) this.mZ = 7.0D; if (this.mZ < -7.0D) this.mZ = -7.0D; if (this.mY > 7.0D) this.mY = 7.0D; if (this.mY < 0.4D) this.mY = 0.4D; this.mV.set(this.mX, this.mY, this.mZ); this.mCubeTf.setTranslation(this.mV); this.mCubeTfGrp.setTransform(this.mCubeTf); this.mMirrTf.set(this.mMirrXzTf); this.mMirrTf.mul(this.mCubeTf); this.mMirrTf.mul(this.mMirrXzTf); this.mMirrCubeTfGrp.setTransform(this.mMirrTf); } private void calcScreenRotsRelativeToCube(double paramDouble1, double paramDouble2, double paramDouble3) { this.mCubeInvTf.invert(this.mCubeTf); this.mCubeInvTf.transform(this.mUx, this.mXAxis); this.mXAxisAngle.set(this.mXAxis, paramDouble1); this.mCubeInvTf.transform(this.mUy, this.mYAxis); this.mYAxisAngle.set(this.mYAxis, paramDouble2); this.mCubeInvTf.transform(this.mUz, this.mZAxis); this.mZAxisAngle.set(this.mZAxis, paramDouble3); } public void handleEvent(int paramInt1, int paramInt2) { switch (paramInt2) { case 131081: handleFrontEvent(); break; case 131082: handleRightEvent(); break; case 131083: handleTopEvent(); break; case 131094: handleBackEvent(); break; case 131095: handleLeftEvent(); break; case 131096: handleBottomEvent(); break; case 131084: handleFitEvent(); break; default: System.out.println("*** Event '" + paramInt2 + "' not supported."); break; } } private void handleFrontEvent() { ScStdData scStdData = new ScStdData(); rotateToFront(); this.mCubeTfGrp.setTransform(this.mCubeTf); repaintDemo(scStdData); } private void rotateToFront() { Transform3D transform3D = new Transform3D(); Vector3d vector3d = new Vector3d(); this.mCubeTf.get(vector3d); transform3D.invert(this.mCubeTf); this.mCubeTf.mul(transform3D); this.mCubeTf.setTranslation(vector3d); } private void handleRightEvent() { Transform3D transform3D = new Transform3D(); AxisAngle4d axisAngle4d = new AxisAngle4d(0.0D, 1.0D, 0.0D, -1.5707963267948966D); ScStdData scStdData = new ScStdData(); rotateToFront(); transform3D.setRotation(axisAngle4d); this.mCubeTf.mul(transform3D); this.mCubeTfGrp.setTransform(this.mCubeTf); repaintDemo(scStdData); } private void handleTopEvent() { Transform3D transform3D = new Transform3D(); AxisAngle4d axisAngle4d = new AxisAngle4d(1.0D, 0.0D, 0.0D, 1.5707963267948966D); ScStdData scStdData = new ScStdData(); rotateToFront(); transform3D.setRotation(axisAngle4d); this.mCubeTf.mul(transform3D); this.mCubeTfGrp.setTransform(this.mCubeTf); repaintDemo(scStdData); } private void handleBackEvent() { Transform3D transform3D = new Transform3D(); AxisAngle4d axisAngle4d = new AxisAngle4d(0.0D, 1.0D, 0.0D, Math.PI); ScStdData scStdData = new ScStdData(); rotateToFront(); transform3D.setRotation(axisAngle4d); this.mCubeTf.mul(transform3D); this.mCubeTfGrp.setTransform(this.mCubeTf); repaintDemo(scStdData); } private void handleLeftEvent() { Transform3D transform3D = new Transform3D(); AxisAngle4d axisAngle4d = new AxisAngle4d(0.0D, 1.0D, 0.0D, 1.5707963267948966D); ScStdData scStdData = new ScStdData(); rotateToFront(); transform3D.setRotation(axisAngle4d); this.mCubeTf.mul(transform3D); this.mCubeTfGrp.setTransform(this.mCubeTf); repaintDemo(scStdData); } private void handleBottomEvent() { Transform3D transform3D = new Transform3D(); AxisAngle4d axisAngle4d = new AxisAngle4d(1.0D, 0.0D, 0.0D, -1.5707963267948966D); ScStdData scStdData = new ScStdData(); rotateToFront(); transform3D.setRotation(axisAngle4d); this.mCubeTf.mul(transform3D); this.mCubeTfGrp.setTransform(this.mCubeTf); repaintDemo(scStdData); } private void handleFitEvent() { setViewPos(this.mVp); } private void initComponents() { this.mGraphicPanel = new JPanel(); setDefaultCloseOperation(3); setTitle("SC Cube Demo"); this.mGraphicPanel.setMinimumSize(new Dimension(640, 480)); this.mGraphicPanel.setPreferredSize(new Dimension(640, 480)); this.mGraphicPanel.addFocusListener(new FocusAdapter() { public void focusLost(FocusEvent param1FocusEvent) { CubeApp.this.mGraphicPanelFocusLost(param1FocusEvent); } }); this.mGraphicPanel.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent param1KeyEvent) { CubeApp.this.mGraphicPanelKeyPressed(param1KeyEvent); } }); this.mGraphicPanel.setLayout(new BorderLayout()); getContentPane().add(this.mGraphicPanel, "Center"); pack(); } private void mGraphicPanelFocusLost(FocusEvent paramFocusEvent) { String str = System.getProperty("os.name"); if (str.equals("Linux")) return; this.mGraphicPanel.requestFocus(); } private void mGraphicPanelKeyPressed(KeyEvent paramKeyEvent) { int i = paramKeyEvent.getKeyCode(); char c = paramKeyEvent.getKeyChar(); ScStdData scStdData = new ScStdData(); if (c == '1') { scStdData.mX = 1000; } else if (c == '!') { scStdData.mX = -1000; } else if (c == '2') { scStdData.mY = 1000; } else if (c == '"') { scStdData.mY = -1000; } else if (c == '3') { scStdData.mZ = 1000; } else if (c == '�') { scStdData.mZ = -1000; } else if (c == '4') { scStdData.mA = 1000; } else if (c == '$') { scStdData.mA = -1000; } else if (c == '5') { scStdData.mB = 1000; } else if (c == '%') { scStdData.mB = -1000; } else if (c == '6') { scStdData.mC = 1000; } else if (c == '&') { scStdData.mC = -1000; } repaintDemo(scStdData); } public static void main(String[] paramArrayOfString) { EventQueue.invokeLater(new Runnable() { public void run() { CubeApp cubeApp = new CubeApp(); cubeApp.setVisible(true); cubeApp.repaintDemo(new ScStdData()); } }); } } /* Location: /opt/SpaceControl/Cube_Demo.jar!/cube_demo/CubeApp.class * Java compiler version: 8 (52.0) * JD-Core Version: 1.2.1 */