Advertisement
codeanticode

Untitled

Feb 23rd, 2012
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.09 KB | None | 0 0
  1. package p5.test_awt_canvas;
  2.  
  3. import java.applet.Applet;
  4. import java.awt.Canvas;
  5. import java.awt.Frame;
  6. import java.awt.GraphicsDevice;
  7. import java.awt.GraphicsEnvironment;
  8. import java.awt.Insets;
  9. import java.awt.event.WindowAdapter;
  10. import java.awt.event.WindowEvent;
  11.  
  12. import org.lwjgl.LWJGLException;
  13. import org.lwjgl.opengl.Display;
  14. import org.lwjgl.opengl.GL11;
  15.  
  16. public class MiniPApplet extends Applet {
  17.   private static final long serialVersionUID = 1L;
  18.  
  19.   /////////////////////////////////////////////////////////////
  20.   //
  21.   // Test parameters  
  22.  
  23.   public int frameRate = 120;
  24.   public boolean resizeableFrame = false;
  25.  
  26.   /////////////////////////////////////////////////////////////
  27.   //
  28.   // Internal variables
  29.  
  30.   int width;
  31.   int height;
  32.  
  33.   private Frame frame;
  34.   private Canvas canvas;
  35.  
  36.   private long beforeTime;
  37.   private long overSleepTime;  
  38.   private long frameRatePeriod = 1000000000L / frameRate;
  39.  
  40.   private boolean initialized = false;  
  41.  
  42.   private double theta = 0;
  43.   private double s = 0;
  44.   private double c = 0;  
  45.  
  46.   private long millisOffset;
  47.   private int fcount, lastm;
  48.   private float frate;
  49.   private int fint = 3;
  50.  
  51.   void run() {
  52.     Thread loop = new Thread("Animation Thread") {
  53.       public void run() {
  54.         int frameCount = 0;
  55.         while (true) {
  56.           if (!initialized) {
  57.             setup();            
  58.           }
  59.          
  60.           draw();
  61.           Display.update();
  62.          
  63.           clock();
  64.          
  65.           frameCount++;
  66.         }
  67.       }
  68.     };
  69.     loop.start();        
  70.   }
  71.  
  72.   void setup() {
  73.     millisOffset = System.currentTimeMillis();    
  74.  
  75.     // Frame setup ----------------------------------------------------------
  76.    
  77.     width = 300;
  78.     height = 300;    
  79.     MiniPApplet applet = this;
  80.    
  81.     GraphicsEnvironment environment =
  82.       GraphicsEnvironment.getLocalGraphicsEnvironment();
  83.     GraphicsDevice displayDevice = environment.getDefaultScreenDevice();
  84.     frame = new Frame(displayDevice.getDefaultConfiguration());
  85.    
  86.     frame.setTitle("MiniPApplet");
  87.     frame.setLayout(null);
  88.     frame.add(applet);
  89.     frame.pack();
  90.     frame.setResizable(resizeableFrame);
  91.    
  92.     Insets insets = frame.getInsets();
  93.  
  94.     int windowW = applet.width + insets.left + insets.right;
  95.     int windowH = applet.height + insets.top + insets.bottom;
  96.     int locationX = 100;
  97.     int locationY = 100;
  98.      
  99.     frame.setSize(windowW, windowH);
  100.     frame.setLocation(locationX, locationY);
  101.      
  102.     int usableWindowH = windowH - insets.top - insets.bottom;
  103.     applet.setBounds((windowW - applet.width)/2, insets.top + (usableWindowH - applet.height)/2, applet.width, applet.height);      
  104.    
  105.     frame.add(this);
  106.     frame.addWindowListener(new WindowAdapter() {
  107.       public void windowClosing(WindowEvent e) {
  108.           System.exit(0);
  109.       }
  110.     });    
  111.    
  112.     frame.setVisible(true);
  113.  
  114.     // Canvas setup ----------------------------------------------------------
  115.  
  116.     canvas = new Canvas();
  117.     applet.add(canvas);
  118.    
  119.     canvas.setBounds(0, 0, applet.width, applet.height);      
  120.     canvas.setFocusable(true);
  121.     canvas.requestFocus();
  122.     canvas.setIgnoreRepaint(true);    
  123.    
  124.     try {
  125.       Display.setParent(canvas);      
  126.       Display.create();
  127.       Display.setVSyncEnabled(false);      
  128.     } catch (LWJGLException e) {
  129.       e.printStackTrace();
  130.     }
  131.    
  132.     initialized = true;    
  133.   }
  134.  
  135.   void draw() {
  136.     GL11.glClearColor(0, 0, 0, 1);
  137.     GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
  138.    
  139.     theta += 0.01;
  140.     s = Math.sin(theta);
  141.     c = Math.cos(theta);      
  142.    
  143.     GL11.glBegin(GL11.GL_TRIANGLES);
  144.     GL11.glColor3f(1, 0, 0);
  145.     GL11.glVertex2d(-c, -c);
  146.     GL11.glColor3f(0, 1, 0);
  147.     GL11.glVertex2d(0, c);
  148.     GL11.glColor3f(0, 0, 1);
  149.     GL11.glVertex2d(s, -s);
  150.     GL11.glEnd();    
  151.    
  152.     GL11.glFlush();
  153.    
  154.     fcount += 1;
  155.     int m = (int) (System.currentTimeMillis() - millisOffset);
  156.     if (m - lastm > 1000 * fint) {
  157.       frate = (float)(fcount) / fint;
  158.       fcount = 0;
  159.       lastm = m;
  160.       System.out.println("fps: " + frate);
  161.     }
  162.   }
  163.  
  164.   void clock() {
  165.     long afterTime = System.nanoTime();
  166.     long timeDiff = afterTime - beforeTime;
  167.     long sleepTime = (frameRatePeriod - timeDiff) - overSleepTime;
  168.  
  169.     if (sleepTime > 0) {  // some time left in this cycle
  170.       try {
  171.         Thread.sleep(sleepTime / 1000000L, (int) (sleepTime % 1000000L));
  172.       } catch (InterruptedException ex) { }
  173.  
  174.       overSleepTime = (System.nanoTime() - afterTime) - sleepTime;
  175.  
  176.     } else {    // sleepTime <= 0; the frame took longer than the period
  177.       overSleepTime = 0L;
  178.     }
  179.  
  180.     beforeTime = System.nanoTime();    
  181.   }  
  182.  
  183.   public static void main(String[] args) {
  184.     MiniPApplet mini;
  185.     try {
  186.       Class<?> c = Thread.currentThread().getContextClassLoader().loadClass(MiniPApplet.class.getName());
  187.       mini = (MiniPApplet) c.newInstance();
  188.     } catch (Exception e) {
  189.       throw new RuntimeException(e);
  190.     }    
  191.     if (mini != null) {
  192.       mini.run();
  193.     }
  194.   }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement