Class Bouncer


  • public class Bouncer
    extends java.lang.Object
    Handle the math calculations needed to determine if the ball hits a paddle or a wall.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int BOTTOM  
      static int LEFT  
      static int NONE  
      static int RIGHT  
      static int TOP  
    • Constructor Summary

      Constructors 
      Constructor Description
      Bouncer()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int getPaddleSideHit​(PongPaddle paddle, PongBall ball)
      Check if the ball has hit the paddle.
      static int getWallHit​(double left, double right, double top, double bottom, PongBall ball)
      Determine which wall, if any, the ball has hit.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Bouncer

        public Bouncer()
    • Method Detail

      • getPaddleSideHit

        public static int getPaddleSideHit​(PongPaddle paddle,
                                           PongBall ball)
        Check if the ball has hit the paddle. If so, return which side of the paddle has been hit. Also shifts the ball's position to the point of impact.
        Parameters:
        paddle - the paddle to be checked
        ball - the ball
        Returns:
        one of Bouncer.LEFT, Bouncer.RIGHT, Bouncer.TOP, Bouncer.BOTTOM indicating which side of the paddle has been hit, or Bouncer.NONE if the paddle has not been hit
      • getWallHit

        public static int getWallHit​(double left,
                                     double right,
                                     double top,
                                     double bottom,
                                     PongBall ball)
        Determine which wall, if any, the ball has hit. Also shifts the ball's position to the point of impact. Note that "top" and "bottom" refer to the relative position on the screen - because lower y coordinates are higher up on the screen, the top wall will have a smaller y coordinate than the bottom wall.
        Parameters:
        left - x coordinate of the left wall (left < right)
        right - x coordinate of the right wall (left < right)
        top - y coordinate of the top wall (top < bottom)
        bottom - y coordinate of the bottom wall (top < bottom)
        ball - the ball
        Returns:
        one of Bouncer.LEFT, Bouncer.RIGHT, Bouncer.TOP, Bouncer.BOTTOM indicating which wall has been hit, or Bouncer.NONE if no wall has been hit