Results 1 to 2 of 2
  1. #1
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Paddle Class: Trying to code details to ball & paddle..

    Just as the title says, I'm working on a sort of a game like Pong. There's a frame that contains a ball that bounces off the walls. There's also a white paddle at the bottom. When the ball hits the paddle, it bounces off. I'm currently using an image for the ball, but the code allows for program to work without an image (I've temporarily deleted the code from the program to not show the image for your sake). Instead, the program will draw a red ellipse in place of the image with a size of 50 x 50.

    Here's the code. I have three classes, the Ball class, the Paddle class, and the main class, the BouncingBall class.

    Ball class:

    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class Ball
    {
      
      private int xPos, yPos, ballWidth, ballHeight;
      private Color c;
      private ImageIcon imgBall;
      private boolean useImage;
      
      
      public Ball()
      {
        this.xPos = 1;
        this.yPos = 0;
        this.ballWidth = 50;
        this.ballHeight = 50;
        this.c = Color.RED;
        this.useImage = false;
        this.imgBall = null;
      }
      
      public Ball(int xPos, int yPos, int ballWidth, int ballHeight, Color c)
      {
        this.xPos = xPos;
        this.yPos = yPos;
        this.ballWidth = ballWidth;
        this.ballHeight = ballHeight;
        this.c = c;
        this.useImage = false;
        this.imgBall = null;
      }
      
      public Ball(int xPos, int yPos, ImageIcon imgBall)
      {
        this.xPos = xPos;
        this.yPos = yPos;
        this.imgBall = imgBall;
        this.ballWidth = imgBall.getIconWidth();
        this.ballHeight = imgBall.getIconHeight();
        this.useImage = true;
        this.c = null;
      }
      
      // ACCESSOR METHODS (return values of the instance variables)
      
      public int getX()
      {
        return xPos;
      }
      
      public int getY()
      {
        return yPos;
      }
      
      public int getBallWidth()
      {
        return ballWidth;
      }
      
      public int getBallHeight()
      {
        return ballHeight;
      }
      
      public Image getImage()
      {
        return imgBall.getImage();
      }
      
      //MUTATOR METHODS (change the values of the class fields)
      
      public void setBallWidth(int width)
      {
        this.ballWidth = width;
      }
      
      public void setBallHeight(int height)
      {
        this.ballHeight = height;
      }
      
      public void setColor(Color col)
      {
        this.c = col;
      }
      
      public void setImageIcon(ImageIcon imgBall)
      {
        this.imgBall = imgBall;
      }
      
      public void setX(int x)
      {
        this.xPos = x;
      }
      
      public void setY(int y)
      {
        this.yPos
    
    public void draw (Graphics2D g2)
      {
        
        if (useImage == false)
        {
          g2.setColor(c);
          g2.fill(new Ellipse2D.Double(xPos, yPos, ballWidth, ballHeight));
        }
        else
        {
          g2.drawImage(getImage(), xPos, yPos, null);
        }
        
      }
      
      public void move()
      {
        this.xPos += 1;
        this.yPos += 2;
      }
      
      public void move(int xDir, int yDir)
      {
        this.xPos += xDir;
        this.yPos += yDir;
      }
    }
    Paddle Class:

    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    
    
    public class Paddle
    {
      private int x, y, padWidth, padHeight, edge1, edge2;
      private Color c;
      
      public Paddle(int x, int y, int padWidth, int padHeight, int edge1, int edge2, Color c)
      {
        this.x = x;
        this.y = y;
        this.padWidth = padWidth;
        this.padHeight = padHeight;
        this.edge1= edge1;
        this.edge2 = edge2;
        this.c = c;
      }
      
      //ACCESSOR METHODS
      
      public int getWidth()
      {
        return padWidth;
      }
      
      public int getHeight()
      {
        return padHeight;
      }
      
      public int getX()
      {
        return x;
      }
      
      public int getY()
      {
        return y;
      }
      
      //MUTATOR METHODS            
      
      public void setWidth (int width)
      {
        this.padWidth = width;
      }
      
      public void setHeight (int height)
      {
        this.padHeight = height;
      }
      
      public void setX (int xPos)
      {
        this.x = xPos;
      }
      
      public void setY (int yPos)
      {
        this.y = yPos;
      }
      
      public void draw(Graphics2D g2)
      {
        g2.setColor(c);
        g2.fill(new RoundRectangle2D.Double(x, y, padWidth, padHeight, edge1, edge2));
      }
      
      public void moveHorizontal (int pixels)
      {
        this.x += 10;
        
        if (x + padWidth >= 400)
        {
          x = 400 - padWidth;
        }
        
      }
      
      public void moveVertical (int pixels)
      {
        this.x -= 10;
        
        if (x <= 0)
        {
          x = 0;
        }
        
      }
      
    }
    BouncingBall class:

    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class BouncingBall extends JPanel implements ActionListener, KeyListener
    {
      
      private Paddle p1;
      private Ball ball;
      private int xDir, yDir;
      private Timer t;
      
      public static void main (String[]args)
      {
        new BouncingBall();
      }
      
      public BouncingBall()
      {
        
        p1 = new Paddle(0, 550, 120, 20, 10, 10 , Color.WHITE);
        
        xDir = 1;
        yDir = 3;
        
        t = new Timer(20, this);
        
        setLayout(null);
        setBackground(new Color(0, 100, 0));
        addKeyListener(this);
        setFocusable(true);
        
        JFrame frame = new JFrame();
        frame.setContentPane(this);
        frame.setSize(400, 600);
        frame.setTitle ("Bouncing Ball");
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        
        ball = new Ball();
        
        t.start();
      }
      
      public void keyPressed (KeyEvent e)
      {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT)
        {
          p1.moveHorizontal(10);
        }
          
        else if (e.getKeyCode() == KeyEvent.VK_LEFT)
        {
          p1.moveVertical(-10);  
        }
        
      }
      
      public void keyTyped (KeyEvent e) {}
      public void keyReleased (KeyEvent e) {}
      
      public void actionPerformed (ActionEvent e)
      {
        ball.move(xDir, yDir);
        
        if (ball.getX() <= 0 || ball.getX() + ball.getBallWidth() >= getWidth())
        {
          xDir = -xDir;
          
        }
        
        else if (ball.getY() <= 0)
        {
          yDir = -yDir;
        }
        
        
        if (ball.getY() + ball.getBallHeight() >= p1.getY() & 
            ball.getX() + ball.getBallWidth() >= p1.getX()  & 
            ball.getX() <= p1.getX() + p1.getWidth()/6)
        {
          xDir = -xDir;
          yDir = -yDir;
        }
        
        else if (ball.getY() + ball.getBallHeight() >= p1.getY() &
                 ball.getX() >= p1.getX() + p1.getWidth() - 30   &
                 ball.getX() <= p1.getX() + p1.getWidth())
        {
          xDir = -xDir;
          yDir = -yDir;
        }
        
        else if (ball.getY() + ball.getBallHeight() >= p1.getY() &
                 ball.getX() + ball.getBallWidth() >= p1.getX()  &
                 ball.getX() <= p1.getX() + p1.getWidth())
        {
          yDir = -yDir;
        }
        
        repaint();
      }
      
      public void paintComponent (Graphics g)
      {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        ball.draw(g2);
        p1.draw(g2);
      }
    }
    Now, the issue is in the BouncingBall class. I've made it so the paddle can detect when the ball is hitting it (Lines 81 to 102). The three if statements there, in order, check if the ball hits the left side of the paddle, then the ball will ricochet back in the direction it came. Second if statement says, if the ball hits the right side of the paddle, the ball will ricochet back in the direction it came. Last if statement just says that the ball should bounce off if it hits the paddle.

    My issues are with the right/left sides of the paddle. When the ball is traveling south-east and hits the left side of the paddle, I want it to bounce back from the direction it came from, like this:

    Paddle Class: Trying to code details to ball &amp; paddle..-ex1.png

    I have gotten this to work in my first "if" statement of those few lines I showed you earlier. I've also gotten the other side to work; when the ball is traveling south-west and hits the right side of the paddle, it bounces back from whence it came as well:

    Paddle Class: Trying to code details to ball &amp; paddle..-ex3.png

    Now, I'm guessing you know where I'm going with this, because I'm sure I'm not the first to come to this forum with a Pong problem. The next two problems I'm having is what happens to the ball when it hits the right side of the paddle when traveling southeast.. I want it to bounce back in the same direction it is going, like this:

    Paddle Class: Trying to code details to ball &amp; paddle..-ex2.png

    But what happens is the ball bounces back in the direction it was traveling.. like this;

    Paddle Class: Trying to code details to ball &amp; paddle..-ex5.png

    The same for the other side. When traveling south-west and hitting the left side, I would like the ball to bounce off continuing in its direction.

    Paddle Class: Trying to code details to ball &amp; paddle..-ex4.png

    But instead, when the ball hits the left side, it bounces back the way it came;

    Paddle Class: Trying to code details to ball &amp; paddle..-ex6.png

    Now, I know why this is happening. It's happening because I have no if statements taking care of these types of situations. My if statements only reverse the direction of the ball when they hit the left or right side. It doesn't account for which direction the ball is traveling. Does anyone have any ideas how to control an issue like this? I'm sure this is an age-old programming problem considering the age of this game.
    Last edited by CuppaCoffee; 01-08-2012 at 03:52 AM.

  2. #2
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: Paddle Class: Trying to code details to ball & paddle..

    It's sad that no one could have helped me with this issue

    Regardless though, I've come to the conclusion myself and solved the problem.. Just thought I should point this out to save the time of those who would've helped me in the future...
    Last edited by CuppaCoffee; 01-09-2012 at 01:16 AM.

Similar Threads

  1. code tag details
    By pottsmgg in forum New To Java
    Replies: 1
    Last Post: 09-02-2010, 12:22 AM
  2. Showing object details of another related class
    By misbah6 in forum New To Java
    Replies: 7
    Last Post: 04-05-2010, 04:48 PM
  3. Pong Paddle Not Stopping Ball At Certain Speed
    By JDCAce in forum Java Applets
    Replies: 3
    Last Post: 04-02-2009, 12:07 AM
  4. Problem deleting ball from bouncing ball app
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 10:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •