boxglue

GameThreaded.java

May 7th, 2021
714
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package testGameThreaded;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.RenderingHints;
  8. import java.awt.event.KeyEvent;
  9. import java.awt.event.KeyListener;
  10. import javax.swing.JFrame;
  11. import javax.swing.JPanel;
  12.  
  13. /*******************************************************************
  14.  * This is a modified version of Game.java
  15.  * I have removed all of the javax.swing.Timer code
  16.  * and replaced it with an Updater class which runs two threads.
  17.  *
  18.  * This program requires Updater.java.
  19.  * Game.java does NOT require Updater.java
  20.  *
  21.  * Also note: Ball_v2.java moves better than Ball.java
  22.  * So replace Ball.java with the code from Ball_v2.java
  23.  * The difference is that I'm tracking the location of the ball in double precision
  24.  * rather than int (x,y). Since the velocity is also double, this means that
  25.  * the ball position doesn't get the rounding errors that happen with int positions.
  26.  * ******************************************************************
  27.  */
  28.  
  29.  
  30. public class GameThreaded extends JFrame implements KeyListener {
  31.  
  32.     final static int SCREEN_WIDTH = 800;
  33.     final static int SCREEN_HEIGHT = 500;
  34.     Paddle player1, player2;
  35.     Ball ball = new Ball();
  36.     DrawPanel drawPanel;
  37.  
  38.     //I'm going to use a Timer instead of a thread because timers are simpler.
  39.     //However, I think that a thread can be adjusted easier to varying frame rate. I'd have to look into this.
  40.     //private Timer moveTimer, repaintTimer;
  41.  
  42.  
  43.     GameThreaded() {
  44.         //PROBLEM: this sets the size of the JFRAME NOT the JPANEL.  
  45.         //All of the moving happens on the JPANEL so you need to use those coordinates.
  46.         //This is probably why your paddle goes off the bottom of the screen
  47.         //setSize(SCREEN_WIDTH, SCREEN_HEIGHT);//  See DrawPanel constructor  **
  48.  
  49.         /** 1. Set up game objects **/
  50.         //create the players for the left and right sides of the screen
  51.         player1 = new Paddle('L');
  52.         player2 = new Paddle('R');
  53.  
  54.         /** 2. Set up Jpanel **/
  55.         drawPanel = new DrawPanel();
  56.         add(drawPanel);
  57.         this.pack(); //needed for setting JPAnel size
  58.  
  59.         /** 3. Set up JFrame **/
  60.         //This has to be after the DrawPanel is created so that it is positioned correctly
  61.         //because we're setting the JPanel size precisely.
  62.         addKeyListener(this);
  63.         setResizable(false);
  64.         setLocationRelativeTo(null);
  65.         /* Always On Top --- interesting. I think that it would be better to do something to freeze
  66.           the game if the window stops being on top */
  67.         //setAlwaysOnTop(true);
  68.         //setLayout(null);  // I don't think that this is needed
  69.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  70.  
  71.         setVisible(true);
  72.  
  73.         //Update movement and repaint using threads
  74.         Updater upd = new Updater(drawPanel,player1,ball);
  75.         //upd.start();
  76.         upd.start(10,1); // pass in the refresh rates for move and repaint
  77.        
  78.     }
  79.  
  80.     // I'm making this an inner class. It's much easier to work with.
  81.     // You only have one JPanel so it's not a problem to put it here.
  82.     // We'll leave the keylistener on the JFrame since if you add it to a JPanel you have to repeatedly get the focus back
  83.     class DrawPanel extends JPanel {
  84.  
  85.         DrawPanel() {
  86.             this.setBackground(Color.BLACK);
  87.             //Set the size of the JPANEL (see note above **)
  88.             this.setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT));
  89.  
  90.             //This should never be here. Set it up in the main part of the program, where my Timers are being setup.
  91.             //Updater updater = new Updater(player1, this, ball);
  92.  
  93.             //I have no idea why this is here or needed.
  94.             //setBounds(0, 0, Game.SCREEN_WIDTH, Game.SCREEN_HEIGHT);
  95.         }
  96.  
  97.         @Override
  98.         protected void paintComponent(Graphics g) {
  99.             super.paintComponent(g);
  100.             //drawBackground(g);  Delete this line. See below
  101.  
  102.             //make the lines smoother using AntiAliasing
  103.             Graphics2D g2 = (Graphics2D) g;
  104.             g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  105.  
  106.             player1.draw(g);
  107.             player2.draw(g);
  108.             ball.draw(g);
  109.         }
  110.  
  111.         /* This is NOT needed.  Set the background colour to Black and then super.paintcomponent
  112.          * does this.  Why are you calling super.paintComponent() if not to draw the background?
  113.          */
  114.         //      void drawBackground(Graphics g) {
  115.         //          g.setColor(Color.black);
  116.         //          g.fillRect(0, 0, Game.SCREEN_WIDTH, Game.SCREEN_HEIGHT);
  117.         //      }
  118.     }
  119.  
  120.     /**********************************************
  121.      * Event Listener Methods                     *
  122.      **********************************************/
  123.  
  124.     //You'll need to add keys for player2
  125.     @Override
  126.     public void keyTyped(KeyEvent e) {}
  127.  
  128.     @Override
  129.     public void keyPressed(KeyEvent e) {
  130.         if (e.getKeyCode() == KeyEvent.VK_W || e.getKeyCode() == KeyEvent.VK_UP) {
  131.             player1.upAccel = true;
  132.         }
  133.         //ElSEIF is not needed. Only use it when you absolutely have to.
  134.         if (e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_DOWN) {
  135.             player1.downAccel = true;
  136.         }
  137.     }
  138.  
  139.     @Override
  140.     public void keyReleased(KeyEvent e) {
  141.         if (e.getKeyCode() == KeyEvent.VK_W || e.getKeyCode() == KeyEvent.VK_UP) {
  142.             player1.upAccel = false;
  143.         }
  144.         if (e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_DOWN) {
  145.             player1.downAccel = false;
  146.         }
  147.     }
  148.  
  149.    
  150.     //=========================================================//
  151.     public static void main(String[] args) {
  152.         //Game game = new Game();
  153.         //Make the game thread safe. This way all of the threads and timers will work together
  154.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  155.             @Override
  156.             public void run() {
  157.                 new GameThreaded();
  158.             }
  159.         });
  160.     }
  161. }
  162.  
  163.  
  164.  
  165.  
RAW Paste Data