Java 3D Desktop Environment旨在使用Java 3D来创建一个3D桌面环境。功能包括:分布式的应用程序
源代码在线查看: defaultgui3d.java
package org.j3de.ui.impl;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.util.List;
import java.util.Vector;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.GraphicsConfigTemplate3D;
import javax.media.j3d.InputDevice;
import javax.media.j3d.VirtualUniverse;
import javax.swing.JComponent;
import org.w3c.dom.Node;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.j3de.events.EventSources;
import org.j3de.input.ActivationManager;
import org.j3de.input.InputDevices;
import org.j3de.input.HasBehavior;
import org.j3de.interfaces.Environment;
import org.j3de.interfaces.EnvironmentManager;
import org.j3de.util.AbstractComponent;
import org.j3de.util.Configurable;
import org.j3de.util.ConfigurationException;
import org.j3de.util.ConfigHelper;
import org.j3de.util.InitializationException;
import org.j3de.ui.Gui3d;
public class DefaultGui3d extends AbstractComponent implements Gui3d, Configurable {
private Canvas3D canvas3d;
private UniverseManager universe;
private List environments;
private EnvironmentEntry currentEnvironment;
private List inputDevices;
private List eventSources;
private List assignedFocus;
// Device Numbers
private int userHead;
private int dominantHand;
private int nonDominantHand;
private int movementDevice;
private int maxFrames;
public DefaultGui3d() {
environments = new Vector();
}
public synchronized void startEnvironment(Environment environment) throws InitializationException {
if (currentEnvironment != null)
currentEnvironment.getActivationManager().setActivation(false);
EnvironmentEntry entry = new EnvironmentEntry(environment, new ActivationManager());
environments.add(entry);
entry.getEnvironment().initialize(new DefaultEnvironmentManager(entry.getActivationManager()));
entry.getActivationManager().setActivation(true);
currentEnvironment = entry;
universe.setEnvironment(entry.getEnvironment());
}
public void startModalEnvironment(Environment environment) throws InitializationException {
EnvironmentEntry oldEnvironment = currentEnvironment;
startEnvironment(environment);
synchronized(this) {
while (environments.indexOf(currentEnvironment) > environments.indexOf(oldEnvironment)) {
try {
this.wait(20 * 1000);
} catch (InterruptedException e) {}
}
}
}
public synchronized void exitEnvironment(Environment environment) { if (currentEnvironment != null) currentEnvironment.getActivationManager().setActivation(false); boolean found = false;
EnvironmentEntry entry; do { entry = (EnvironmentEntry)environments.get(environments.size()-1); if (entry.getEnvironment() == environment) found = true; environments.remove(environments.size()-1); } while ((environments.size() > 0) && (!found)); if (environments.size() > 0) { entry = (EnvironmentEntry)environments.get(environments.size()-1); entry.getActivationManager().setActivation(true); currentEnvironment = entry; universe.setEnvironment(entry.getEnvironment());
synchronized(entry) { entry.notifyAll();
} } else { // no environment left :( // TODO : clean up leftovers... System.exit(0); } }
public Component getComponent() {
return canvas3d;
}
public void configure(Node node, Document nodeFactory) throws ConfigurationException {
super.configure(node, nodeFactory);
GraphicsConfigTemplate3D tmpl = new GraphicsConfigTemplate3D();
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = env.getDefaultScreenDevice();
GraphicsConfiguration config = device.getBestConfiguration(tmpl);
canvas3d = new Canvas3D(config);
maxFrames = helper.getPropertyValue("maxFPS", 0, false);
inputDevices = helper.getComponentList("inputdevices");
universe = new UniverseManager(canvas3d, inputDevices, maxFrames);
for (int i=0; i // Initialize Devices on our own, because only VirtualInputDevices will be
// added to SceneGraph
((InputDevice)inputDevices.get(i)).initialize();
if (inputDevices.get(i) instanceof HasBehavior) {
universe.addBehavior(((HasBehavior)inputDevices.get(i)).getBehavior());
}
}
eventSources = helper.getComponentList("eventsources");
for (int i=0; i if (eventSources.get(i) instanceof HasBehavior) {
universe.addBehavior(((HasBehavior)eventSources.get(i)).getBehavior());
}
}
// asignedFocus contains the numbers of the FocusAware components, that are assigned
// to the eventSources
assignedFocus = helper.getIntList("assignedfocus");
userHead = helper.getPropertyValue("UserHead", 0, true);
dominantHand = helper.getPropertyValue("DominantHand", 1, true);
nonDominantHand = helper.getPropertyValue("NondominantHand", 2, true);
movementDevice = helper.getPropertyValue("Movement", 3, true);
}
private class DefaultEnvironmentManager implements EnvironmentManager {
private ActivationManager activationManager;
private InputDevices inputDevices; private EventSources eventSources;
public DefaultEnvironmentManager(ActivationManager activationManager) {
this.activationManager = activationManager;
this.inputDevices = new InputDevices(DefaultGui3d.this.inputDevices, activationManager, userHead, dominantHand, nonDominantHand, movementDevice); this.eventSources = new EventSources(DefaultGui3d.this.eventSources, assignedFocus, activationManager); }
public void startEnvironment(Environment environment) throws InitializationException {
DefaultGui3d.this.startEnvironment(environment);
}
public void startModalEnvironment(Environment environment) throws InitializationException {
DefaultGui3d.this.startModalEnvironment(environment);
}
public void exitEnvironment(Environment environment) {
DefaultGui3d.this.exitEnvironment(environment);
}
public InputDevices getInputDevices() {
return
inputDevices; }
public EventSources getEventSources() {
return eventSources; }
}
private class EnvironmentEntry {
private Environment environment;
private ActivationManager activationManager;
public EnvironmentEntry(Environment environment, ActivationManager activationManager) {
this.environment = environment;
this.activationManager = activationManager;
}
public Environment getEnvironment() {
return environment;
}
public ActivationManager getActivationManager() {
return activationManager;
}
public boolean equals(Object obj) { return super.equals(obj); }
}
}