Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package PingPong;
- import java.awt.*;
- import javax.swing.*;
- public class PingPong extends JFrame implements Runnable {
- private Ball ball = new Ball(100, 150, this);
- private Image dbImage;
- private Graphics dbg;
- //-----------------------------------------------------------------
- public PingPong(String title) {
- super(title);
- }
- //-----------------------------------------------------------------
- public void paint (Graphics graphics) {
- super.paint(graphics);
- graphics.drawImage(Toolkit.getDefaultToolkit().getImage("Bilder/Spielfeld.png"), 0, 0, this);
- ball.paint(graphics);
- }
- //-----------------------------------------------------------------
- public void update (Graphics graphics) {
- if ( dbImage == null) {
- dbImage = createImage (this.getSize().width,this.getSize().height);
- dbg = dbImage.getGraphics();
- }
- dbg.setColor(getBackground());
- dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
- dbg.setColor (getForeground());
- run ();
- graphics.drawImage (dbImage, 0, 0, this);
- }
- //-----------------------------------------------------------------
- public void run() {
- Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
- while(true) {
- ball.moveBall();
- update(getGraphics());
- try {
- Thread.sleep(20);
- } catch (InterruptedException ex) {
- } //Wiederholt sich mit Pause: 20ms
- Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
- }
- }
- //-----------------------------------------------------------------
- public static void main(String[] args) {
- PingPong pong = new PingPong("PingPong-Spiel");
- pong.setSize(800, 500);
- pong.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- pong.setVisible(true);
- new Thread(pong).start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement