GamesMaxed

Java flikkerend beeld

Nov 24th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. package eu.inmensia.learn;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.image.BufferStrategy;
  8.  
  9. import javax.swing.JFrame;
  10.  
  11. public class Client extends Canvas implements Runnable {
  12.     private static final long serialVersionUID = 1L;
  13.    
  14.     public static final int WIDTH = 300;
  15.     public static final int HEIGHT = WIDTH / 16 * 9;
  16.     public static final byte SCALE = 3;
  17.    
  18.     private Thread thread;
  19.     private JFrame frame = new JFrame();
  20.     private boolean running = false;
  21.    
  22.     public Client() {
  23.         Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
  24.         setPreferredSize(size);
  25.     }
  26.    
  27.     public synchronized void start() {
  28.         running = true;
  29.         thread = new Thread(this, "display");
  30.         thread.start(); // starts the thread
  31.     }
  32.    
  33.     public synchronized void stop() {
  34.         running = false;
  35.         try {
  36.             thread.join(); // ends the thread
  37.         } catch(InterruptedException e){ e.printStackTrace(); }
  38.     }
  39.  
  40.     public void run() {
  41.         while(running){
  42.             tick();
  43.             render();
  44.         }
  45.     }
  46.    
  47.     public void tick() {
  48.        
  49.     }
  50.    
  51.     public void render() {
  52.         BufferStrategy bs = getBufferStrategy();
  53.         if(bs == null){
  54.             createBufferStrategy(3);
  55.             return;
  56.         }
  57.        
  58.         Graphics g = bs.getDrawGraphics();
  59.         {
  60.             // background
  61.             g.setColor(Color.BLACK);
  62.             g.drawRect(0, 0, getWidth(), getHeight());
  63.         }
  64.         g.dispose();
  65.         bs.show();
  66.     }
  67.    
  68.     public static void main(String[] args) {
  69.         Client client = new Client();
  70.         client.frame.setResizable(false);
  71.         client.frame.setTitle("Learning");
  72.         client.frame.add(client);
  73.         client.frame.pack();
  74.         client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  75.         client.frame.setLocationRelativeTo(null);
  76.         client.frame.setVisible(true);
  77.        
  78.         client.start();
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment