Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.82 KB | None | 0 0
  1. package project6;
  2.  
  3. /**
  4.  * @author Andrew Hamilton
  5.  */
  6.  
  7. import static javax.media.j3d.PolygonAttributes.*;
  8.  
  9. import java.awt.*;
  10. import java.awt.event.KeyEvent;
  11. import java.awt.event.KeyListener;
  12.  
  13. import javax.swing.*;
  14. import javax.vecmath.*;
  15. import javax.media.j3d.*;
  16. import com.sun.j3d.utils.universe.SimpleUniverse;
  17.  
  18. public class Project6 {
  19.    
  20.     public static float ROTATION;
  21.     public static boolean LEFT_DOWN;
  22.     public static boolean RIGHT_DOWN;
  23.    
  24.     /**
  25.      * Entry point of application.
  26.      * @param args
  27.      */
  28.     public static void main(String[] args) {
  29.         // Create the JFrame
  30.         JFrame frame = new JFrame();
  31.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32.        
  33.         // Add 3d canvas
  34.         GameCanvas c = new GameCanvas();
  35.         c.setPreferredSize(new Dimension(640,480));
  36.         frame.add(c);
  37.        
  38.         // Display application
  39.         frame.pack();
  40.         frame.setVisible(true);
  41.        
  42.         // Key handler
  43.         c.canvas.addKeyListener(new KeyListener() {
  44.  
  45.             @Override
  46.             public void keyTyped(KeyEvent e) {
  47.                 //
  48.             }
  49.  
  50.             @Override
  51.             public void keyPressed(KeyEvent e) {
  52.                 System.out.println(e.getKeyCode());
  53.                
  54.                 if ( e.getKeyCode() == KeyEvent.VK_A ) {
  55.                     LEFT_DOWN = true;
  56.                 }else if ( e.getKeyCode() == KeyEvent.VK_D ) {
  57.                     RIGHT_DOWN = true;
  58.                 }
  59.             }
  60.  
  61.             @Override
  62.             public void keyReleased(KeyEvent e) {
  63.                 if ( e.getKeyCode() == KeyEvent.VK_A ) {
  64.                     LEFT_DOWN = false;
  65.                 }else if ( e.getKeyCode() == KeyEvent.VK_D ) {
  66.                     RIGHT_DOWN = false;
  67.                 }
  68.             }
  69.         });
  70.     }
  71.    
  72.     /**
  73.      * Octahedron Shape. Extends Shape3D. Uses OctahedronData class for its triangle data.
  74.      * @author Andrew
  75.      *
  76.      */
  77.     static class OctahedronShape extends Shape3D {
  78.         public OctahedronShape() {
  79.             super(new OctahedronData());
  80.            
  81.             // Create a material for the shape
  82.             Material material = new Material();
  83.             material.setLightingEnable(true);
  84.             Appearance appearance = new Appearance();
  85.             appearance.setPolygonAttributes(new PolygonAttributes(POLYGON_LINE, CULL_NONE, 0));
  86.             appearance.setMaterial(material);
  87.             setAppearance(appearance);
  88.         }
  89.     }
  90.    
  91.     /**
  92.      * Creates a new triangle array containing the predefined data for a normalized Octahedron.
  93.      * @author Andrew
  94.      *
  95.      */
  96.     static class OctahedronData extends IndexedTriangleArray {
  97.  
  98.         final static int VERTICES = 6;
  99.         final static int VERTEX_FORMAT = GeometryArray.COORDINATES;
  100.         final static int INDICES = 24;
  101.        
  102.         public OctahedronData() {
  103.             super(VERTICES, VERTEX_FORMAT, INDICES);
  104.            
  105.             // Define the final list of vertices
  106.             final Point3f[] verts = new Point3f[] {
  107.                     new Point3f(0, 0, 1),
  108.                     new Point3f(-1, 0, 0),
  109.                     new Point3f(0, -1, 0),
  110.                     new Point3f(1, 0, 0),
  111.                     new Point3f(0, 1, 0),
  112.                     new Point3f(0, 0, -1),
  113.             };
  114.            
  115.             // Define the final list of indices
  116.             final int[] indices = new int[] {
  117.                     2, 0, 1,
  118.                     1, 0, 4,
  119.                     4, 0, 3,
  120.                     3, 0, 2,
  121.                    
  122.                     5, 2, 1,
  123.                     5, 1, 4,
  124.                     5, 4, 3,
  125.                     5, 3, 2
  126.             };
  127.            
  128.             // Set the vertices/indices to the triangle array
  129.             this.setCoordinates(0, verts);
  130.             this.setCoordinateIndices(0, indices);
  131.         }
  132.        
  133.     }
  134.    
  135.     /**
  136.      * Main canvas to render the 3d scene.
  137.      * @author Andrew
  138.      *
  139.      */
  140.     static class GameCanvas extends JPanel {
  141.         Canvas3D canvas;
  142.        
  143.         public GameCanvas() {
  144.             setLayout(new BorderLayout());
  145.            
  146.             // Create 3d canvas
  147.             canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
  148.             this.add(canvas, BorderLayout.CENTER);
  149.            
  150.             // Universe
  151.             SimpleUniverse universe = new SimpleUniverse(canvas);
  152.  
  153.             // Create a structure to contain objects
  154.             BranchGroup group = new BranchGroup();
  155.            
  156.             // Create a octahedron
  157.             Node shape = new OctahedronShape();
  158.            
  159.             // Create a transformation for the shape
  160.             TransformGroup tg = new TransformGroup();
  161.             tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  162.             group.addChild(tg);
  163.             tg.addChild(shape);
  164.  
  165.             // Reset view & add to universe
  166.             universe.getViewingPlatform().setNominalViewingTransform();
  167.             universe.addBranchGraph(group);
  168.            
  169.             // Create game-loop to rotate the shape
  170.             new Thread(new Runnable() {
  171.                 @Override
  172.                 public void run() {
  173.                     long start = System.nanoTime();
  174.                    
  175.                     while(true) {
  176.                         // Sleep thread (so CPU doesn't overwork)
  177.                         try {
  178.                             Thread.sleep(10);
  179.                         } catch (InterruptedException e) {
  180.                             e.printStackTrace();
  181.                         }
  182.  
  183.                         // Calculate the delta time
  184.                         long time = System.nanoTime();
  185.                         double delta = (time-start)*1.0e-9;
  186.                         start = time;
  187.                        
  188.                         // compute direction
  189.                         float direction = (LEFT_DOWN?1:0)-(RIGHT_DOWN?1:0);
  190.  
  191.                         // Increment rotation value
  192.                         ROTATION += delta*direction;
  193.                        
  194.                         // Apply rotation to shape
  195.                         Transform3D tr = new Transform3D();
  196.                         tr.rotY(ROTATION);
  197.                         tr.setScale(0.33);
  198.                         tg.setTransform(tr);
  199.                     }
  200.                 }
  201.             }).start();
  202.         }
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement