Advertisement
Chiddix

GameCanvasTest

Jun 27th, 2014
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. import java.awt.Canvas;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Graphics2D;
  5. import java.awt.image.BufferStrategy;
  6.  
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9.  
  10. /**
  11.  * An example game canvas which renders using accelerated graphics and updates
  12.  * with nanosecond precision.
  13.  *
  14.  * @author Ryan Greene
  15.  *
  16.  */
  17. @SuppressWarnings("serial")
  18. public final class GameCanvasTest extends Canvas {
  19.  
  20.     private static final long NANOSECONDS_PER_MILISECOND = 1000000;
  21.  
  22.     private static final long TICK_RATE = NANOSECONDS_PER_MILISECOND * 8;
  23.  
  24.     private final BufferStrategy strategy;
  25.  
  26.     public GameCanvasTest() {
  27.         final JFrame container = new JFrame("Game");
  28.  
  29.         final JPanel panel = (JPanel) container.getContentPane();
  30.         panel.setPreferredSize(new Dimension(800, 600));
  31.         panel.setLayout(null);
  32.  
  33.         setBounds(0, 0, 800, 600);
  34.         panel.add(this);
  35.  
  36.         setIgnoreRepaint(true);
  37.  
  38.         container.pack();
  39.         container.setResizable(false);
  40.         container.setVisible(true);
  41.  
  42.         container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43.  
  44.         requestFocus();
  45.  
  46.         createBufferStrategy(3);
  47.         strategy = getBufferStrategy();
  48.     }
  49.  
  50.     private double boxX = 0;
  51.  
  52.     private double boxY = 0;
  53.  
  54.     private double boxDX = .4;
  55.  
  56.     private double boxDY = .2;
  57.  
  58.     public void loop() {
  59.         long lastTickTime = System.nanoTime();
  60.         long delta;
  61.         long sleepTime;
  62.         Graphics2D g;
  63.         while (true) {
  64.             delta = System.nanoTime() - lastTickTime;
  65.             lastTickTime = System.nanoTime();
  66.             g = (Graphics2D) strategy.getDrawGraphics();
  67.            
  68.             // update
  69.             boxX += (delta * boxDX) / NANOSECONDS_PER_MILISECOND;
  70.             boxY += (delta * boxDY) / NANOSECONDS_PER_MILISECOND;
  71.            
  72.             // render
  73.             g.setColor(Color.BLACK);
  74.             g.fillRect(0, 0, 800, 600);
  75.             g.setColor(Color.RED);
  76.             g.drawRect((int) boxX, (int) boxY, 50, 50);
  77.            
  78.             g.dispose();
  79.             strategy.show();
  80.  
  81.             sleepTime = TICK_RATE - (System.nanoTime() - lastTickTime);
  82.             System.out.println("delta: " + delta + " sleep time: " + sleepTime + " (ms: " + sleepTime / NANOSECONDS_PER_MILISECOND + " ns: " + sleepTime % NANOSECONDS_PER_MILISECOND + ")");
  83.             if (sleepTime > 0) {
  84.                 try {
  85.                     Thread.sleep(sleepTime / NANOSECONDS_PER_MILISECOND, (int) (sleepTime % NANOSECONDS_PER_MILISECOND));
  86.                 } catch (final InterruptedException e) {
  87.                     e.printStackTrace();
  88.                 }
  89.             } else {
  90.                 System.out.println("[WARNING]: Load: " + (100 + Math.abs(sleepTime) / (TICK_RATE / 100)) + "%!");
  91.             }
  92.         }
  93.     }
  94.  
  95.     public static void main(final String[] args) {
  96.         new GameCanvasTest().loop();
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement