boxglue

Updater.java

May 7th, 2021
653
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package testGameThreaded;
  2.  
  3. import javax.swing.JPanel;
  4.  
  5. /* This class creates and runs two threads.
  6.  * One for moving objects and one for updating the screen
  7.  */
  8.  
  9. class Updater  {
  10.     Paddle player1, player2;
  11.     Thread repainter;
  12.     JPanel drawPanel;
  13.     Ball ball;
  14.     Thread moveTh, repaintTh;
  15.  
  16.     static int moveInterval = 7;
  17.     static int repaintInterval = 1;
  18.  
  19.  
  20.     Updater(JPanel drawPanel, Paddle player1, Ball ball) {
  21.         this.drawPanel = drawPanel;
  22.         this.player1 = player1;
  23.         this.ball = ball;
  24.  
  25.         moveTh = new Mover();
  26.         repaintTh = new Repainter();
  27.         //new Repainter().start();
  28.  
  29.     }
  30.  
  31.     void start() {
  32.         moveTh.start();
  33.         repaintTh.start();
  34.     }
  35.     void start(int tm, int tr) {
  36.         moveInterval = tm;
  37.         repaintInterval = tr;
  38.        
  39.         moveTh.start();
  40.         repaintTh.start();
  41.     }
  42.     class Repainter extends Thread {
  43.  
  44.         @Override
  45.         public void run() {
  46.             while (true) {
  47.                 drawPanel.repaint();
  48.                 try {
  49.                     Thread.sleep(repaintInterval);
  50.                 } catch (InterruptedException e) {}
  51.             }
  52.         }
  53.     }
  54.  
  55.     class Mover extends Thread {
  56.  
  57.         //Constructor not needed
  58.         //Mover() {}
  59.  
  60.         @Override
  61.         public void run() {
  62.             while (true) {
  63.                 player1.move();
  64.                 //player2.move();
  65.                 ball.move();
  66.                 ball.checkPlayerCollision(player1);
  67.                 try {
  68.                     Thread.sleep(moveInterval);
  69.                 } catch (Exception e) {}
  70.             }
  71.         }
  72.     }
  73. }
  74.  
  75.  
  76.  
RAW Paste Data