Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Ping Pong Game Problem with ball Drop and Catch

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Ping Pong Game Problem with ball Drop and Catch

    I have created this ping pong game but having problem with ball and paddle collusion. After first collusion it gives score 1 but later it never touch the paddle.
    Can anyone help with the code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.util.Random.*;
    import javax.swing.ImageIcon;
     
    public class Ball extends JFrame implements ActionListener, KeyListener{
    	int xSpeed=0;
    	int x,y;
    	int x1,y1;
    	int score;
    	int randomNum;
        Rectangle blockRect;
        Rectangle ballRect;
    	Timer time;
    	public Ball(String title){
    		super(title);
    		xSpeed=5;
    		x=400;
    		y=100;
    		x1= 325;
    		y1= 500;
    		time=new Timer(100,this);
    		time.start();
    		blockRect = new Rectangle(x1,y1,150,70);
    		addKeyListener(this);
    		setSize(800,600);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    	public void actionPerformed(ActionEvent e){
    		y=y+xSpeed;
    		repaint();
    		if(y> this.getWidth()){
    			y=25;
    			score -=1;
    		}
    		if(blockRect.intersects(ballRect)){
    			y=40;
    			score +=1;
    			x =200;
    			y = 300;
    		}
    	}
    	public void paint(Graphics g){
    		Graphics2D g2d=(Graphics2D) g;
    		Rectangle rect=new Rectangle(0,0,800,600);
    		g2d.setColor(Color.BLACK);
    		g2d.fill(rect);
    		g2d.setColor(Color.CYAN);
    		g2d.fillOval(x,y,25,25);
    		ballRect=new Rectangle(x,y,30,30);
    		g2d.setColor(Color.RED);
    		g2d.fillRect(x1,y1,150,40);
    		g2d.drawString("Score:" +score,500,50);
    	}
        public void left() {
            x1-=5;
        }
        public void right() {
            x1 +=5;;
        }
        public void keyReleased(KeyEvent e)
        	{}
        public void keyTyped(KeyEvent e)
        	{}
        public void keyPressed(KeyEvent e)
        	{
            if (e.getKeyCode()==KeyEvent.VK_LEFT){
            	left();
            	System.out.println("Left:"+x1);
            }
            if (e.getKeyCode()==KeyEvent.VK_RIGHT){
            	  right();
            	  System.out.println("Left:"+x1);
            }
        }
    	public static void main(String args[]){
    		new Ball("Animated Ball");
    	}
    }


    --- Update ---

    I think something is wrong here:
    public void actionPerformed(ActionEvent e){
    y=y+xSpeed;
    repaint();
    if(y> this.getWidth()){
    y=25;
    score -=1;
    }
    if(blockRect.intersects(ballRect)){
    y=40;
    score +=1;
    x =200;
    y = 300;
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Ping Pong Game Problem with ball Drop and Catch

    A general architecture comment:

    Don't draw directly on the JFrame by overriding its paint() method. That is not how painting in Swing is intended to be done. Instead, add a JPanel to the JFrame and draw on the JPanel using its paintComponent() method.

    As for your question, perhaps its the placement of your repaint() call, though I'm not sure I understand what you're asking.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Ping Pong Game Problem with ball Drop and Catch

    The comparison of y with the width seems confused. Shouldn't y be compared to the height?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ping Pong Game Problem with ball Drop and Catch

    can anyone re-write the code I made mistake. I am trying to figure it out for many days without luck.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Ping Pong Game Problem with ball Drop and Catch

    Make a small change to the code by doing what Greg suggested: Create a JPanel with the current paint() method renamed to paintComponent() and adding an instance of that new JPanel to the JFrame.

    Is getWidth() correct for comparing with y?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Falling Ball and Catching with Bar Game Problem Help
    By zille in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 21st, 2014, 06:41 AM
  2. Java Program - Graphics - Ball Drop and Retrieve
    By kbrady481 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 1st, 2013, 04:47 PM
  3. How To Make Pong Ball Slowly Increase Speed
    By drjd24 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2013, 06:28 PM
  4. Ping Pong Table - Issues with Scores
    By jshcamisado in forum What's Wrong With My Code?
    Replies: 14
    Last Post: February 15th, 2013, 06:11 PM
  5. [SOLVED] Fixing of bug for Pong game
    By phoenix in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 14th, 2009, 01:19 PM

Tags for this Thread