Advertisement
codeanticode

Untitled

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