?? desktopenvironment.java
字號:
package org.j3de.environment.desktop;
import java.awt.Font;import java.net.URL;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;
import java.util.HashMap;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.Group;
import javax.media.j3d.InputDevice;
import javax.media.j3d.PhysicalBody;
import javax.media.j3d.PhysicalEnvironment;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.Transform3D;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Material;
import javax.media.j3d.Appearance;
import javax.media.j3d.Text3D;
import javax.media.j3d.Font3D;
import javax.media.j3d.FontExtrusion;
import javax.media.j3d.ViewPlatform;import javax.vecmath.Point3f;
import javax.vecmath.Point3d;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3f;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.j3de.behavior.MovementBehavior;
import org.j3de.behavior.DominantHandBehavior;
import org.j3de.events.EventSources;
import org.j3de.events.EventDispatcher;
import org.j3de.exception.ExceptionHandler;
import org.j3de.input.InputDevices;
import org.j3de.interfaces.Environment;
import org.j3de.interfaces.EnvironmentManager;
import org.j3de.util.AbstractComponent;
import org.j3de.util.ApplicationHelper;
import org.j3de.util.InitializationException;
import org.j3de.ui.BackgroundUIElement;
import org.j3de.ui.EnvironmentLightsUIElement;
import org.j3de.ui.UIFactory;
import org.j3de.ui.UIRootContainer;
import org.j3de.ui.UICreationException;
import org.j3de.ui.UIDominantHand;
import org.j3de.ui.impl.UniverseManager;
public class DesktopEnvironment implements Environment {
private UIFactory uiFactory;
private List startupURLs;
private MovementBehavior movementBehavior;
private DominantHandBehavior dominantHandBehavior;
private Vector3f position;
private transient EnvironmentManager environmentManager;
private transient BranchGroup worldObjects;
private transient ViewPlatform platform;
private transient PhysicalBody body;
private transient List applications;
private transient TransformGroup viewTransformGroup;
private transient TransformGroup dominantHandTransformGroup;
private transient EventDispatcher eventDispatcher;
private transient UIRootContainer rootContainer;
public DesktopEnvironment(UIFactory uiFactory,
List startupURLs,
MovementBehavior movementBehavior,
DominantHandBehavior dominantHandBehavior,
Vector3f position) {
this.uiFactory = uiFactory;
this.startupURLs = startupURLs;
this.movementBehavior = movementBehavior;
this.dominantHandBehavior = dominantHandBehavior;
this.position = position;
}
public void initialize(EnvironmentManager environmentManager) throws InitializationException {
// Initialize local Variables with values from environmentManager
this.environmentManager = environmentManager;
InputDevices devices = environmentManager.getInputDevices();
EventSources eventSources = environmentManager.getEventSources();
// This is our world
worldObjects = new BranchGroup();
worldObjects.setCapability(Group.ALLOW_CHILDREN_READ);
worldObjects.setCapability(Group.ALLOW_CHILDREN_WRITE);
worldObjects.setCapability(Group.ALLOW_CHILDREN_EXTEND);
// Set up event management
eventDispatcher = new EventDispatcher(worldObjects);
eventDispatcher.addFocusAware(dominantHandBehavior);
for (int i=0; i<eventSources.getSourceCount(); i++) {
eventDispatcher.addEventSource(eventSources.getEventSource(i), eventSources.getAssignedFocusAware(i));
}
// Root Container contains all UIElements, created by the environment
try {
rootContainer = (UIRootContainer)uiFactory.createUIElement(org.j3de.ui.UIRootContainer.class);
worldObjects.addChild(rootContainer.getUILocalElement().getNode());
} catch (RemoteException e) {
// Should not happen, but handle it anyway
ExceptionHandler.handleException(e);
} catch (UICreationException e) {
// Not a nice thing to happen, probably makes no sense to continue ...
ExceptionHandler.handleException(e);
throw new InitializationException(e.getMessage());
}
// Set up View and Input-Devices/Behaviors
platform = new ViewPlatform();
body = new PhysicalBody();
viewTransformGroup = new TransformGroup();
viewTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
viewTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
Transform3D startPosition = new Transform3D();
startPosition.setTranslation(position);
viewTransformGroup.setTransform(startPosition);
movementBehavior.setSensor(devices.getSensor(devices.getMovementSensor()));
movementBehavior.setTransformGroup(viewTransformGroup);
dominantHandBehavior.setSensor(devices.getSensor(devices.getDominantHand()));
worldObjects.addChild(viewTransformGroup);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
// Set up lights
EnvironmentLightsUIElement lights;
try {
lights =
(EnvironmentLightsUIElement)uiFactory.createUIElement(org.j3de.ui.EnvironmentLightsUIElement.class);
rootContainer.add(lights);
} catch (RemoteException e) {
// Should not happen, but handle it anyway
ExceptionHandler.handleException(e);
throw new InitializationException(e.getMessage());
} catch (UICreationException e) {
// Not a nice thing to happen, probably makes no sense to continue ...
ExceptionHandler.handleException(e);
throw new InitializationException(e.getMessage());
}
// Set up the background
BackgroundUIElement background;
try {
background =
(BackgroundUIElement)uiFactory.createUIElement(org.j3de.ui.BackgroundUIElement.class);
rootContainer.add(background);
} catch (RemoteException e) {
// Should not happen, but handle it anyway
ExceptionHandler.handleException(e);
throw new InitializationException(e.getMessage());
} catch (UICreationException e) {
// Not a nice thing to happen, probably makes no sense to continue ...
ExceptionHandler.handleException(e);
throw new InitializationException(e.getMessage());
}
// Set up the dominant hand
dominantHandTransformGroup = new TransformGroup();
dominantHandTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
dominantHandTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
try {
// Root Container the for the dominant "hand"
UIRootContainer handContainer =
(UIRootContainer)uiFactory.createUIElement(org.j3de.ui.UIRootContainer.class);
UIDominantHand hand = (UIDominantHand)uiFactory.createUIElement(org.j3de.ui.UIDominantHand.class);
handContainer.add(hand);
dominantHandTransformGroup.addChild(handContainer.getUILocalElement().getNode());
dominantHandBehavior.setSensor(devices.getSensor(devices.getDominantHand()));
dominantHandBehavior.setTransformGroup(dominantHandTransformGroup);
} catch (RemoteException e) {
// Should not happen, but handle it anyway
ExceptionHandler.handleException(e);
} catch (UICreationException e) {
// Not a nice thing to happen, probably makes no sense to continue ...
ExceptionHandler.handleException(e);
throw new InitializationException(e.getMessage());
}
viewTransformGroup.addChild(platform);
viewTransformGroup.addChild(movementBehavior);
viewTransformGroup.addChild(dominantHandBehavior);
viewTransformGroup.addChild(dominantHandTransformGroup);
// Start the Applications
startApplications();
}
private void startApplications() throws InitializationException {
try {
UnicastRemoteObject.exportObject(uiFactory);
final UIRootContainer applicationContainer =
(UIRootContainer)uiFactory.createUIElement(org.j3de.ui.UIRootContainer.class);
BranchGroup bg = new BranchGroup();
bg.addChild(applicationContainer.getUILocalElement().getNode());
bg.compile();
synchronized (worldObjects) {
worldObjects.addChild(bg);
}
for (int i=0; i<startupURLs.size(); i++) {
final int copyI = i;
new Thread(new Runnable() {
public void run() {
try {
DesktopApplicationEnvironment applicationEnvironment =
new DesktopApplicationEnvironment(uiFactory, applicationContainer, DesktopEnvironment.this, environmentManager);
UnicastRemoteObject.exportObject(applicationEnvironment);
ApplicationHelper.getApplication((String)startupURLs.get(copyI), applicationEnvironment);
} catch (RemoteException e) {
ExceptionHandler.handleException(e);
} catch (Exception e) {
ExceptionHandler.handleException(e);
}
}
}).start();
}
} catch (RemoteException e) {
//hmm, if this happens, something has seriously gone wrong ;)
ExceptionHandler.handleException(e);
throw new InitializationException(e.getMessage());
} catch (UICreationException e) {
// Without gui a desktop environment ain't no fun :(
ExceptionHandler.handleException(e);
throw new InitializationException(e.getMessage());
}
}
public BranchGroup getWorldObject() {
return worldObjects;
}
public ViewPlatform getViewPlatform() {
return platform;
}
public PhysicalBody getPhysicalBody() { return body; }
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -