Advertisement
Guest User

DoubleBufferin

a guest
Jan 11th, 2014
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. package PingPong;
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5.  
  6.  
  7. public class PingPong extends JFrame implements Runnable {
  8.  
  9.     private Ball ball = new Ball(100, 150, this);
  10.    
  11.     private Image dbImage;
  12.     private Graphics dbg;
  13.    
  14. //-----------------------------------------------------------------
  15.    
  16.     public PingPong(String title) {
  17.         super(title);
  18.     }
  19. //-----------------------------------------------------------------
  20.    
  21.     public void paint (Graphics graphics) {
  22.        
  23.         super.paint(graphics);
  24.        
  25.         graphics.drawImage(Toolkit.getDefaultToolkit().getImage("Bilder/Spielfeld.png"), 0, 0, this);      
  26.        
  27.         ball.paint(graphics);
  28.     }
  29.    
  30. //-----------------------------------------------------------------
  31.    
  32.     public  void update (Graphics graphics) {
  33.    
  34.         if ( dbImage == null) {
  35.            
  36.             dbImage = createImage (this.getSize().width,this.getSize().height);
  37.             dbg = dbImage.getGraphics();
  38.         }
  39.        
  40.         dbg.setColor(getBackground());
  41.         dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
  42.        
  43.         dbg.setColor (getForeground());
  44.         run ();
  45.        
  46.         graphics.drawImage (dbImage, 0, 0, this);
  47.    
  48.     }
  49.    
  50. //-----------------------------------------------------------------
  51.    
  52.     public void run() {
  53.        
  54.         Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  55.        
  56.         while(true) {                    
  57.                
  58.             ball.moveBall();            
  59.        
  60.             update(getGraphics());                    
  61.            
  62.             try {
  63.                
  64.                 Thread.sleep(20);
  65.                
  66.             } catch (InterruptedException ex) {
  67.        
  68.                
  69.             }             //Wiederholt sich mit Pause: 20ms
  70.            
  71.             Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  72.         }
  73.     }
  74.    
  75. //-----------------------------------------------------------------
  76.    
  77.     public static void main(String[] args) {
  78.         PingPong pong = new PingPong("PingPong-Spiel");
  79.         pong.setSize(800, 500);
  80.         pong.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  81.         pong.setVisible(true);
  82.         new Thread(pong).start();
  83.        
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement