Advertisement
Guest User

Untitled

a guest
Oct 17th, 2011
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1. import java.awt.Frame;
  2. import java.awt.GraphicsConfiguration;
  3. import java.awt.event.WindowAdapter;
  4. import java.awt.event.WindowEvent;
  5.  
  6. import javax.media.j3d.AmbientLight;
  7. import javax.media.j3d.Background;
  8. import javax.media.j3d.BoundingSphere;
  9. import javax.media.j3d.Bounds;
  10. import javax.media.j3d.BranchGroup;
  11. import javax.media.j3d.Canvas3D;
  12. import javax.media.j3d.DirectionalLight;
  13. import javax.vecmath.Color3f;
  14. import javax.vecmath.Point3d;
  15. import javax.vecmath.Vector3d;
  16. import javax.vecmath.Vector3f;
  17.  
  18. import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
  19. import com.sun.j3d.utils.universe.SimpleUniverse;
  20. import com.sun.j3d.utils.universe.ViewingPlatform;
  21.  
  22.  
  23. public class CelestialApp extends Frame {
  24.  
  25.     /**
  26.      *
  27.      */
  28.     private static final long serialVersionUID = 1L;
  29.  
  30.     /**
  31.      * @param args
  32.      */
  33.     public static void main(String[] args) {
  34.         // TODO Auto-generated method stub
  35.         new CelestialApp();
  36.     }
  37.    
  38.     private SimpleUniverse universe = null;
  39.    
  40.     public CelestialApp() {
  41.         super("Celestial Physics 3D");
  42.         setSize(1024, 768);
  43.        
  44.         GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
  45.         Canvas3D defaultCanvas = new Canvas3D(config);
  46.         add("Center", defaultCanvas);
  47.         setVisible(true);
  48.        
  49.         addWindowListener(new WindowAdapter() {
  50.             public void windowClosing(WindowEvent e) {
  51.                 dispose();
  52.                 System.exit(0);
  53.             }
  54.         });
  55.        
  56.        
  57.         BranchGroup scene = constructContentBranch();
  58.         scene.compile();
  59.        
  60.         universe = new SimpleUniverse(defaultCanvas);
  61.         setupView();
  62.         universe.addBranchGraph(scene);
  63.     }
  64.    
  65.     private void setupView() {
  66.         ViewingPlatform vp =  universe.getViewingPlatform();
  67.         OrbitBehavior orbit = new OrbitBehavior();
  68.         orbit.setSchedulingBounds(new BoundingSphere(new Point3d(0.0f, 0.0f, 0.0f), 10000.0f));
  69.         vp.setNominalViewingTransform();
  70.         vp.setViewPlatformBehavior(orbit);
  71.     }
  72.  
  73.     private BranchGroup constructContentBranch() {
  74.         BranchGroup scene = new BranchGroup();
  75.        
  76.         CelestialBody body1 = new CelestialBody(-2.0f, 0.0f, 0.0f);
  77.         scene.addChild(body1);
  78.  
  79.         CelestialBody body2 = new CelestialBody(2.0f, 0.0f, -1.1f);
  80.         scene.addChild(body2);
  81.        
  82.         PhysicalBehavior physics1 = new PhysicalBehavior(body1);
  83.         scene.addChild(physics1);
  84.  
  85.         PhysicalBehavior physics2 = new PhysicalBehavior(body2, new Vector3d(0.0f, 0.0f, 0.0f));
  86.         scene.addChild(physics2);
  87.  
  88.         setupLights(scene);
  89.        
  90.         return scene;
  91.     }
  92.    
  93.     private void setupLights(BranchGroup scene) {
  94.         AmbientLight lightA = new AmbientLight();
  95.         lightA.setInfluencingBounds(new BoundingSphere());
  96.         lightA.setColor(new Color3f(0.3f, 0.3f, 0.3f));
  97.         scene.addChild(lightA);
  98.        
  99.         DirectionalLight lightD1 = new DirectionalLight();
  100.         lightD1.setInfluencingBounds(new BoundingSphere());
  101.         Vector3f dir = new Vector3f(-0.3f, -1.0f, -0.5f);
  102.         dir.normalize();
  103.         lightD1.setDirection(dir);
  104.         lightD1.setColor(new Color3f(1.0f, 1.0f, 1.0f));
  105.         scene.addChild(lightD1);
  106.        
  107.         Background bg = new Background(0.0f, 0.0f, 0.0f);
  108.         bg.setApplicationBounds(new BoundingSphere());
  109.         scene.addChild(bg);
  110.     }
  111. }
  112.  
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement