import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.j3d.AmbientLight;
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.DirectionalLight;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.universe.ViewingPlatform;
public class CelestialApp extends Frame {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new CelestialApp();
}
private SimpleUniverse universe = null;
public CelestialApp() {
super("Celestial Physics 3D");
setSize(1024, 768);
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D defaultCanvas = new Canvas3D(config);
add("Center", defaultCanvas);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
BranchGroup scene = constructContentBranch();
scene.compile();
universe = new SimpleUniverse(defaultCanvas);
setupView();
universe.addBranchGraph(scene);
}
private void setupView() {
ViewingPlatform vp = universe.getViewingPlatform();
OrbitBehavior orbit = new OrbitBehavior();
orbit.setSchedulingBounds(new BoundingSphere(new Point3d(0.0f, 0.0f, 0.0f), 10000.0f));
vp.setNominalViewingTransform();
vp.setViewPlatformBehavior(orbit);
}
private BranchGroup constructContentBranch() {
BranchGroup scene = new BranchGroup();
CelestialBody body1 = new CelestialBody(-2.0f, 0.0f, 0.0f);
scene.addChild(body1);
CelestialBody body2 = new CelestialBody(2.0f, 0.0f, -1.1f);
scene.addChild(body2);
PhysicalBehavior physics1 = new PhysicalBehavior(body1);
scene.addChild(physics1);
PhysicalBehavior physics2 = new PhysicalBehavior(body2, new Vector3d(0.0f, 0.0f, 0.0f));
scene.addChild(physics2);
setupLights(scene);
return scene;
}
private void setupLights(BranchGroup scene) {
AmbientLight lightA = new AmbientLight();
lightA.setInfluencingBounds(new BoundingSphere());
lightA.setColor(new Color3f(0.3f, 0.3f, 0.3f));
scene.addChild(lightA);
DirectionalLight lightD1 = new DirectionalLight();
lightD1.setInfluencingBounds(new BoundingSphere());
Vector3f dir = new Vector3f(-0.3f, -1.0f, -0.5f);
dir.normalize();
lightD1.setDirection(dir);
lightD1.setColor(new Color3f(1.0f, 1.0f, 1.0f));
scene.addChild(lightD1);
Background bg = new Background(0.0f, 0.0f, 0.0f);
bg.setApplicationBounds(new BoundingSphere());
scene.addChild(bg);
}
}