t1nman

GamePanel.java

Feb 23rd, 2014
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. import javax.swing.JPanel;
  2.  
  3. import java.awt.*;
  4. import java.awt.image.*;
  5. import java.awt.event.*;
  6.  
  7. public class GamePanel extends JPanel implements Runnable, KeyListener {
  8.    
  9.     public static final int WIDTH = 400;
  10.     public static final int HEIGHT = 400;
  11.    
  12.     private Thread thread;
  13.    
  14.     private BufferedImage image;
  15.     private Graphics2D g;
  16.     private boolean running;
  17.    
  18.     public GamePanel() {
  19.         super();
  20.         setPreferredSize(new Dimension(WIDTH, HEIGHT));
  21.         setFocusable(true);
  22.         requestFocus();
  23.     }
  24.    
  25.     public void addNotify() {
  26.         super.addNotify();
  27.         if(thread == null) {
  28.             addKeyListener(this);
  29.             thread = new Thread(this);
  30.             thread.start();
  31.         }
  32.     }
  33.    
  34.     // initializes variables
  35.     private void init() {
  36.        
  37.         image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  38.         g = (Graphics2D) image.getGraphics();
  39.        
  40.         running = true;
  41.        
  42.     }
  43.    
  44.     // the "main" function
  45.     public void run() {
  46.        
  47.         init();
  48.        
  49.         int FPS = 30;
  50.         int targetTime = 1000 / FPS;
  51.        
  52.         long start;
  53.         long elapsed;
  54.         long wait;
  55.        
  56.         // simple game loop
  57.         while(running) {
  58.            
  59.             start = System.nanoTime();
  60.            
  61.             update();
  62.             draw();
  63.             drawToScreen();
  64.            
  65.             elapsed = (System.nanoTime() - start) / 1000000;
  66.            
  67.             wait = targetTime - elapsed;
  68.             if(wait < 0) wait = 5;
  69.            
  70.             try {
  71.                 Thread.sleep(wait);
  72.             }
  73.             catch(Exception e) {
  74.                 e.printStackTrace();
  75.             }
  76.            
  77.         }
  78.        
  79.     }
  80.    
  81.     // updates the game
  82.     private void update() {
  83.        
  84.     }
  85.    
  86.     // draws the game onto an off-screen buffered image
  87.     private void draw() {
  88.        
  89.     }
  90.    
  91.     // draws the off-screen buffered image to the screen
  92.     private void drawToScreen() {
  93.         Graphics g2 = getGraphics();
  94.         g2.drawImage(image, 0, 0, null);
  95.         g2.dispose();
  96.     }
  97.    
  98.     public void keyTyped(KeyEvent k) {
  99.        
  100.     }
  101.     public void keyPressed(KeyEvent k) {
  102.        
  103.     }
  104.     public void keyReleased(KeyEvent k) {
  105.        
  106.     }
  107.    
  108. }
Advertisement
Add Comment
Please, Sign In to add comment