Results 1 to 5 of 5
  1. #1
    Phoeenix is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default The bug in the Pong game

    Hello everyone,
    I am new to this forum. I hope, i will contribute more as time passes.
    I attached the program below. Everything seems fine but the program doesn't start working.
    Any kind of contribution would be appreciated.
    Thanks in advance.

    Note: (From administration) The original author of the attached code requested an indication of his credit. The original code is said to be here. Please note that we did not check the code but found this request reasonable! This note is added to respect intellectual rights and to inform users of the forum.
    Attached Files Attached Files
    Last edited by Java Tip; 11-19-2009 at 10:59 PM.

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Hi Phoeenix, welcome to Java Forums!

    I moved the code in the main() method to the frame's constructor. It works now. ;) Also, when extending components, be sure to call the super() constructor.
    Java Code:
    import java.io.*;
    import java.awt.*;
    import javax.swing.*;
    
    public class Pong extends JFrame implements Runnable
    {
    	//Variables
    	Thread pongThread; //Will be used to turn this runnable object into a working thread
    	PongArena arena = new PongArena(400, 600); //Make a pong arena
    	
    	//Constructors
    	public Pong()
    	{
    		super();
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Tell it to close the program when the frame closes
    		//Setup the layout and how things will be displayed...
    		Container content = this.getContentPane();
    		content.setLayout(new BorderLayout());
    		content.add(arena, BorderLayout.CENTER);
    		this.setTitle("Pong - By: Chris Canik");
    		this.setResizable(false);
    		
    		//This tells the JFrame that it should resize according
    		//to the component's (PongArena) prefered size
    		this.pack();
    		
    		//The boundaries must be created here because getX and getY have no
    		//accurate values when the arena's constructor is originally called
    		arena.side1 = new PongArenaBoundary((int)(arena.getX() - 500),
    											(int)(arena.getY() + arena.topPaddle.getHeight() + 30),
    											(int)(500),
    											(int)(arena.getBounds().getHeight() - arena.topPaddle.getHeight() - arena.bottomPaddle.getHeight() - 60));
    		arena.side2 = new PongArenaBoundary((int)(arena.getX() + arena.getBounds().getWidth()),
    											(int)(arena.getY() + arena.topPaddle.getHeight() + 30),
    											(int)(arena.getX() + arena.getBounds().getWidth() + 500),
    											(int)(arena.getBounds().getHeight() - arena.topPaddle.getHeight() - arena.bottomPaddle.getHeight() - 60));
    		
    		//Give the arena focus within the JPanel
    		arena.requestFocus();
    		
    		setVisible(true); //Show the frame to the player
    
    		//Turn this Runnable JPanel into an actual thread
    		pongThread = new Thread(this);
    		pongThread.start();
    	}
    	
    	//Methods
    	public void run()
    	{
    		boolean keepGoing = true; //Tracks whether there has not been an error
    		
    		while (keepGoing)
    		{
    			try
    			{
    				//Find out what the ball needs to bounce off of and do it
    				arena.ball.bounce(arena.ball.needToBounce(arena));
    
    			}
    			catch (BallOffCourtException ex) //Point has been scored
    			{
    				//Make a new ball for a new round, and register it to
    				//the two AI's so they don't try to follow the old one
    				arena.topAI.registerBall(arena.newBall((arena.getWidth() / 2) - 10, (arena.getHeight() / 2) - 10, 20, 20));
    				arena.bottomAI.registerBall(arena.ball);
    				
    			}
    			
    			//Display FPS in JPanel title
    			this.setTitle("Pong (FPS: " + arena.fps.getFrames() + ") - By: Chris Canik");
    			
    			try
    			{
    				//Redraw everything and wait a bit as math is handled
    				repaint();
    				Thread.sleep(10); //(1000/10) = 100FPS
    			}
    			catch (InterruptedException ex)
    			{
    				//Error...  Time to stop running!
    				System.out.println("Interrupted!");
    				keepGoing = false;
    			}
    	}
    	}
    	
    	//Main Method
    	public static void main(String[] args)
    	{
    		Pong pongGame = new Pong(); //Lets make a game frame
    	}
    
    }
    Very cool game, by the way. :D Glad to have you on the forum!
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    Phoeenix is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default

    Thank you ! Thank you ! Thank you!
    You can't imagine how pleasent I am.

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    hmm... tim, isn't the super-constructor called implicitly? You only need to use super if you're calling a super-constructor with arguments.

    e.g
    Java Code:
    public class Sub extends Super{
         public Sub(){
    
         }
    }
    //the above is equivalent to this:
    public class Sub extends Super{
         public Sub(){
              super();
         }
    }
    //but this is different, and requires arguments to satisfy the super-constructor
    public class Sub extends Super{
         public Sub(Object arg){
              super(arg);
         }
    }
    You don't ALWAYS need to call the super-constructor explicitly.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #5
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Hey guys! :D

    Quote Originally Posted by Phoeenix View Post
    Thank you ! Thank you ! Thank you!
    You can't imagine how pleasent I am.
    It's my pleasure Phoeenix! Good luck with your other projects. ;)

    Quote Originally Posted by Singing Boyo View Post
    hmm... tim, isn't the super-constructor called implicitly? You only need to use super if you're calling a super-constructor with arguments.

    e.g
    Java Code:
    public class Sub extends Super{
         public Sub(){
    
         }
    }
    //the above is equivalent to this:
    public class Sub extends Super{
         public Sub(){
              super();
         }
    }
    //but this is different, and requires arguments to satisfy the super-constructor
    public class Sub extends Super{
         public Sub(Object arg){
              super(arg);
         }
    }
    You don't ALWAYS need to call the super-constructor explicitly.
    Ah, okay. Didn't know that. Thank you Singing Boyo. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. Ping pong game
    By adam405 in forum New To Java
    Replies: 8
    Last Post: 10-20-2010, 03:52 PM
  2. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  3. Pong Paddle Not Stopping Ball At Certain Speed
    By JDCAce in forum Java Applets
    Replies: 3
    Last Post: 04-01-2009, 11:07 PM
  4. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 06:00 AM
  5. Help with pong game
    By Eric in forum New To Java
    Replies: 2
    Last Post: 07-03-2007, 07:02 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
  •