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.
Printable View
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.
Hello phoenix. Welcome to the Java Programming Forums.
How are you attempting to run this?
I build classes, then run pong.java and it doesnt show the frame.However, when you change 49.line to false in Pong class,
it shows the frame but doesnt run.
Isn't there anyone who can help me? It is kind of urgent.
Thank you for your attention
Hmm I'm having trouble getting this to run properly..
I will continue to look at it but seeing as your in a rush, i'll let you know now that I don't have much time.
If you need to see a working pong game, try these links:
A Pong Game by Dino Scarcella
Java Pong Game
I have compiled the second one before and know it works.
You are a lifesaver. Thank you billion times.
You still need to get the original game you posted working?
Unfortunately, yes.
A man from another forum solved the problem.
Here it is;
"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."
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 } }
Brilliant! Glad it was solved for you phoenix. Thanks for posting the solution.