Guest User

BlockWorld.java

a guest
Oct 31st, 2013
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. package de.skysoldier.blockworld;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Toolkit;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.MouseMotionAdapter;
  9. import java.awt.event.MouseMotionListener;
  10.  
  11. import javax.swing.JPanel;
  12.  
  13. import de.skysoldier.graphics3D.Camera;
  14. import de.skysoldier.graphics3D.Graphics3D;
  15. import de.skysoldier.graphics3D.Line3D;
  16. import de.skysoldier.graphics3D.LinedMesh;
  17. import de.skysoldier.graphics3D.Values3D;
  18.  
  19. public class BlockWorld extends JPanel implements Runnable {
  20.    
  21.     private Gui gui;
  22.    
  23.     private LinedMesh cube = new LinedMesh(Values3D.MESH_PRESET_CUBE);
  24.    
  25.     private Thread thread;
  26.     private Graphics3D g3d;
  27.     private Camera camera;
  28.    
  29.     public BlockWorld(){
  30.         init();
  31.     }
  32.    
  33.     public void run(){
  34.         while(true){
  35.             repaint();
  36.             try{Thread.sleep(10);}catch(Exception e){}
  37.         }
  38.     }
  39.    
  40.     public void paintComponent(Graphics g){
  41.         super.paintComponent(g);
  42. //      g.setColor(Color.RED);
  43. //      g.drawLine(300, 0, 300, 300);
  44. //      g.drawLine(0, 300, 300, 300);
  45.         g.setColor(Color.WHITE);
  46.         g3d.updateRotationDataX(angleX);
  47.         g3d.updateRotationDataY(angleY);
  48.         //the third param (z) doesn't affect anything:
  49.         g3d.updateTranslationData(100, 100, 500);
  50.         for(int i = 0; i < cube.getLineCount(); i++){
  51.             Line3D copy = g3d.renderLine(cube.getLine(i), camera, g3d.getRotationDataX(), g3d.getRotationDataY(), g3d.getTranslationData());
  52.             g3d.drawLine(copy, g);
  53.         }
  54.     }
  55.    
  56.     private void init(){
  57.         addMouseMotionListener(ml);
  58.         gui = new Gui(this);
  59.         thread = new Thread(this);
  60.         g3d = new Graphics3D();
  61.         g3d.updateTranslationData(300, 300, 0);
  62.         camera = g3d.createCamera(Values3D.CAMERA_PERSPECTIVE);
  63.         camera.active = true;
  64.         start();
  65.     }
  66.    
  67.     private void start(){
  68.         gui.init();
  69.         thread.start();
  70.     }
  71.    
  72.     private double angleX = 0;
  73.     private double angleY = 0;
  74.     private Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
  75.     private double xperpixel = 360 / screensize.getWidth();
  76.     private double yperpixel = 360 / screensize.getHeight();
  77.     public MouseMotionListener ml = new MouseMotionAdapter() {
  78.         public void mouseDragged(MouseEvent e) {
  79.             angleY = (double) e.getX() * xperpixel;
  80.             angleX = (double) e.getY() * yperpixel;
  81.         }
  82.     };
  83.    
  84.     public static void main(String[] args) {
  85.         new BlockWorld();
  86.     }
  87. }
Add Comment
Please, Sign In to add comment