1 16 package com.buchuki.ensmer; 17 18 import com.sun.j3d.utils.picking.PickCanvas; 19 import com.sun.j3d.utils.picking.PickResult; 20 import java.awt.*; 21 import java.awt.event.*; 22 import java.awt.image.BufferedImage ; 23 import javax.media.j3d.*; 24 import javax.vecmath.*; 25 import com.sun.j3d.utils.universe.SimpleUniverse; 26 27 28 37 public class InterfaceManager { 38 39 45 public InterfaceManager() { 46 Frame ensmerFrame = createWindow(); 47 ensmerFrame.add(createCanvas()); 48 ensmerFrame.setVisible(true); 49 hiddenCursor = Toolkit.getDefaultToolkit().createCustomCursor( 50 new BufferedImage (1, 1, BufferedImage.TYPE_INT_ARGB), 51 new Point(0, 0), "Invisible Cursor"); 52 } 53 54 60 public void displayArea(Area area) { 61 BranchGroup newBranchGraph = area.getBranchGraph(); 62 if (branchGraph == null) { 63 universe.getLocale().addBranchGraph(newBranchGraph); 64 } 65 else { 66 universe.getLocale().replaceBranchGraph(branchGraph, newBranchGraph); 67 } 68 branchGraph = newBranchGraph; 69 setViewPosition(area.getUserPosition()); 70 } 71 72 77 public Canvas3D getCanvas() { 78 return canvas; 79 } 80 81 87 public Matrix4f getViewPosition() { 88 Transform3D transform = new Transform3D(); 89 universe.getViewingPlatform().getViewPlatformTransform() 90 .getTransform(transform); 91 Matrix4f retval = new Matrix4f(); 92 transform.get(retval); 93 return retval; 94 } 95 96 101 public void setViewPosition(Matrix4f position) { 102 Transform3D transform = new Transform3D(position); 103 universe.getViewingPlatform().getViewPlatformTransform() 104 .setTransform(transform); 105 } 106 107 112 public void setCursorVisible(boolean visible) { 113 if (visible) { 114 canvas.setCursor(Cursor.getDefaultCursor()); 115 } 116 else { 117 canvas.setCursor(hiddenCursor); 118 } 119 } 120 121 130 public PickResult pickClosest(int xPos, int yPos) { 131 pickCanvas.setShapeLocation(xPos, yPos); 132 return pickCanvas.pickClosest(); 133 } 134 135 148 public Long pickObject(int xPos, int yPos) { 149 PickResult result = pickClosest(xPos, yPos); 150 if (result == null) { 151 return null; 152 } 153 SceneGraphPath path = result.getSceneGraphPath(); 154 for (int i = 0; i < path.nodeCount(); i++) { 155 if (path.getNode(i) instanceof BranchGroup) { 156 BranchGroup possible = (BranchGroup) path.getNode(i); 157 Object obj = possible.getUserData(); 158 if (obj instanceof Long ) { 159 return (Long ) obj; 160 } 161 } 162 } 163 return null; 164 167 } 168 169 174 private Frame createWindow() { 175 Frame frame = new Frame("Ensmer"); 176 frame.setLayout(new BorderLayout()); 177 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 178 frame.setBounds(0, 0, screenSize.width, screenSize.height); 179 frame.addWindowListener(new EnsmerCloser()); 180 return frame; 181 } 182 183 189 private Canvas3D createCanvas() { 190 canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration()); 191 universe = new SimpleUniverse(canvas); 192 universe.getViewer().getView().setBackClipDistance(100); 193 platformBounds = new BoundingLeaf(new BoundingSphere(new Point3d(), 1000)); 194 BranchGroup grp = new BranchGroup(); 195 grp.addChild(platformBounds); 196 universe.getViewingPlatform().getViewPlatformTransform().addChild(grp); 197 198 pickCanvas = new PickCanvas(canvas, universe.getLocale()); 199 pickCanvas.setTolerance(0.0f); 200 pickCanvas.setMode(pickCanvas.GEOMETRY); 201 202 BranchGroup root = new BranchGroup(); 203 204 AmbientLight lightA = new AmbientLight(); 205 lightA.setInfluencingBoundingLeaf(platformBounds); 206 root.addChild(lightA); 207 DirectionalLight lightD = new DirectionalLight(new Color3f(1f, 1f, 1f), 208 new Vector3f(0f, -1f, -1f)); 209 lightD.setInfluencingBoundingLeaf(platformBounds); 211 root.addChild(lightD); 212 universe.addBranchGraph(root); 213 return canvas; 214 } 215 216 219 private Canvas3D canvas; 220 221 224 private Cursor hiddenCursor; 225 226 229 private SimpleUniverse universe; 230 231 236 private BranchGroup branchGraph; 237 238 241 private BoundingLeaf platformBounds; 242 243 246 private PickCanvas pickCanvas; 247 248 254 private class EnsmerCloser extends WindowAdapter { 255 260 public void windowClosing(WindowEvent e) { 261 EnsmerManager.instance().closeEnsmer(); 262 } 263 } 264 } 265 266 | Popular Tags |