Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package project6;
- /**
- * @author Andrew Hamilton
- */
- import static javax.media.j3d.PolygonAttributes.*;
- import java.awt.*;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import javax.swing.*;
- import javax.vecmath.*;
- import javax.media.j3d.*;
- import com.sun.j3d.utils.universe.SimpleUniverse;
- public class Project6 {
- public static float ROTATION;
- public static boolean LEFT_DOWN;
- public static boolean RIGHT_DOWN;
- /**
- * Entry point of application.
- * @param args
- */
- public static void main(String[] args) {
- // Create the JFrame
- JFrame frame = new JFrame();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- // Add 3d canvas
- GameCanvas c = new GameCanvas();
- c.setPreferredSize(new Dimension(640,480));
- frame.add(c);
- // Display application
- frame.pack();
- frame.setVisible(true);
- // Key handler
- c.canvas.addKeyListener(new KeyListener() {
- @Override
- public void keyTyped(KeyEvent e) {
- //
- }
- @Override
- public void keyPressed(KeyEvent e) {
- System.out.println(e.getKeyCode());
- if ( e.getKeyCode() == KeyEvent.VK_A ) {
- LEFT_DOWN = true;
- }else if ( e.getKeyCode() == KeyEvent.VK_D ) {
- RIGHT_DOWN = true;
- }
- }
- @Override
- public void keyReleased(KeyEvent e) {
- if ( e.getKeyCode() == KeyEvent.VK_A ) {
- LEFT_DOWN = false;
- }else if ( e.getKeyCode() == KeyEvent.VK_D ) {
- RIGHT_DOWN = false;
- }
- }
- });
- }
- /**
- * Octahedron Shape. Extends Shape3D. Uses OctahedronData class for its triangle data.
- * @author Andrew
- *
- */
- static class OctahedronShape extends Shape3D {
- public OctahedronShape() {
- super(new OctahedronData());
- // Create a material for the shape
- Material material = new Material();
- material.setLightingEnable(true);
- Appearance appearance = new Appearance();
- appearance.setPolygonAttributes(new PolygonAttributes(POLYGON_LINE, CULL_NONE, 0));
- appearance.setMaterial(material);
- setAppearance(appearance);
- }
- }
- /**
- * Creates a new triangle array containing the predefined data for a normalized Octahedron.
- * @author Andrew
- *
- */
- static class OctahedronData extends IndexedTriangleArray {
- final static int VERTICES = 6;
- final static int VERTEX_FORMAT = GeometryArray.COORDINATES;
- final static int INDICES = 24;
- public OctahedronData() {
- super(VERTICES, VERTEX_FORMAT, INDICES);
- // Define the final list of vertices
- final Point3f[] verts = new Point3f[] {
- new Point3f(0, 0, 1),
- new Point3f(-1, 0, 0),
- new Point3f(0, -1, 0),
- new Point3f(1, 0, 0),
- new Point3f(0, 1, 0),
- new Point3f(0, 0, -1),
- };
- // Define the final list of indices
- final int[] indices = new int[] {
- 2, 0, 1,
- 1, 0, 4,
- 4, 0, 3,
- 3, 0, 2,
- 5, 2, 1,
- 5, 1, 4,
- 5, 4, 3,
- 5, 3, 2
- };
- // Set the vertices/indices to the triangle array
- this.setCoordinates(0, verts);
- this.setCoordinateIndices(0, indices);
- }
- }
- /**
- * Main canvas to render the 3d scene.
- * @author Andrew
- *
- */
- static class GameCanvas extends JPanel {
- Canvas3D canvas;
- public GameCanvas() {
- setLayout(new BorderLayout());
- // Create 3d canvas
- canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
- this.add(canvas, BorderLayout.CENTER);
- // Universe
- SimpleUniverse universe = new SimpleUniverse(canvas);
- // Create a structure to contain objects
- BranchGroup group = new BranchGroup();
- // Create a octahedron
- Node shape = new OctahedronShape();
- // Create a transformation for the shape
- TransformGroup tg = new TransformGroup();
- tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
- group.addChild(tg);
- tg.addChild(shape);
- // Reset view & add to universe
- universe.getViewingPlatform().setNominalViewingTransform();
- universe.addBranchGraph(group);
- // Create game-loop to rotate the shape
- new Thread(new Runnable() {
- @Override
- public void run() {
- long start = System.nanoTime();
- while(true) {
- // Sleep thread (so CPU doesn't overwork)
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- // Calculate the delta time
- long time = System.nanoTime();
- double delta = (time-start)*1.0e-9;
- start = time;
- // compute direction
- float direction = (LEFT_DOWN?1:0)-(RIGHT_DOWN?1:0);
- // Increment rotation value
- ROTATION += delta*direction;
- // Apply rotation to shape
- Transform3D tr = new Transform3D();
- tr.rotY(ROTATION);
- tr.setScale(0.33);
- tg.setTransform(tr);
- }
- }
- }).start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement