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.
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.
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.
Last edited by Deep_4; November 8th, 2012 at 01:20 PM.
Hello phoenix. Welcome to the Java Programming Forums.
How are you attempting to run this?
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
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.
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
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.
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
phoenix (July 14th, 2009)
You are a lifesaver. Thank you billion times.
You still need to get the original game you posted working?
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
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."
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.
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.