• (3 Pages)
  • +
  • 1
  • 2
  • 3

Programing an applet game of Pong A Step By Step Walk-Through And Explanation Rate Topic: ***** 3 Votes

#16 roachmmflhyr  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 29-January 11

Posted 03 September 2011 - 11:50 PM

View PostLuckless, on 02 September 2011 - 06:38 AM, said:

View Postcommon_man, on 06 August 2011 - 01:07 PM, said:

I tried out the tutorial.. the game is working perfectly :) but the screen still flickers a bit.. is it normal or am do I have to add anything to the code given in the tutorial?


If you used a swing timer to handle the repainting, it would work much better. I wrote this when I first started out with my tiny toolbox of code so I didn't understand the more efficient ways of writing a graphical applet



How did you guys get this to run, btw I am using the full version of NetBeans with everything installed because this is the output of a java error I get when I run the html file:

java.lang.NoClassDefFoundError: PongMain (wrong name: pongmain/PongMain)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Was This Post Helpful? 0
  • +
  • -

#17 common_man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-August 11

Posted 11 September 2011 - 02:45 AM

View PostLuckless, on 02 September 2011 - 06:38 AM, said:

View Postcommon_man, on 06 August 2011 - 01:07 PM, said:

I tried out the tutorial.. the game is working perfectly :) but the screen still flickers a bit.. is it normal or am do I have to add anything to the code given in the tutorial?


If you used a swing timer to handle the repainting, it would work much better. I wrote this when I first started out with my tiny toolbox of code so I didn't understand the more efficient ways of writing a graphical applet


I used the timer given in your tutorial :
Timer time = new Timer(15, this);



Still getting the flicker :dontgetit:

View Postroachmmflhyr, on 03 September 2011 - 11:50 PM, said:

View PostLuckless, on 02 September 2011 - 06:38 AM, said:

View Postcommon_man, on 06 August 2011 - 01:07 PM, said:

I tried out the tutorial.. the game is working perfectly :) but the screen still flickers a bit.. is it normal or am do I have to add anything to the code given in the tutorial?


If you used a swing timer to handle the repainting, it would work much better. I wrote this when I first started out with my tiny toolbox of code so I didn't understand the more efficient ways of writing a graphical applet



How did you guys get this to run, btw I am using the full version of NetBeans with everything installed because this is the output of a java error I get when I run the html file:

java.lang.NoClassDefFoundError: PongMain (wrong name: pongmain/PongMain)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)


Looks like you have the filename (pongmain) and the class within it (PongMain) spelled differently. You need to have both the class name and the file containing it exactly same - It is case sensitive.
Was This Post Helpful? 0
  • +
  • -

#18 Ian5133  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-September 11

Posted 19 September 2011 - 05:06 PM

I really love this tutorial, the way you teach java makes it really sink into my mind and make me learn it quickly. I made the game and messed around and made these changes:
-The Left hitter is now free to move across its half of the board using the mouse
-The Right hitter moves around its X axis too
-Added scoring with the formula score=(currentTime/1000)^1.4
-The ball is randomly placed in the game when it is reset to prevent too much repetitiveness
its at this link: http://www.iancraft....ancedPlay.shtml

I have one question: how would I make it so that when you lose a life (originally, when the pRight's "score" is increased by one), the white background of the gameboard flashes red for about half a second then reverts back to normal?
Was This Post Helpful? 0
  • +
  • -

#19 htc(hot-taco-cheese)  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 71
  • Joined: 04-December 10

Posted 12 October 2011 - 05:33 PM

There were no errors that showed up in Eclipse. But when I went to compile PongMain.java in my terminal, I got these errors...


PongMain.java:10: cannot find symbol
symbol  : class Ball
location: class pong.PongMain
	Ball ball;
	^
PongMain.java:11: cannot find symbol
symbol  : class PaddleLeft
location: class pong.PongMain
	PaddleLeft pLeft;
	^
PongMain.java:12: cannot find symbol
symbol  : class PaddleRight
location: class pong.PongMain
	PaddleRight pRight;
	^
PongMain.java:25: cannot find symbol
symbol  : class Ball
location: class pong.PongMain
		ball = new Ball();
		           ^
PongMain.java:26: cannot find symbol
symbol  : class PaddleLeft
location: class pong.PongMain
		pLeft = new PaddleLeft();
		            ^
PongMain.java:27: cannot find symbol
symbol  : class PaddleRight
location: class pong.PongMain
		pRight = new PaddleRight(ball.getY() - 35);
		             ^
PongMain.java:75: operator + cannot be applied to PaddleRight.getScore,int
			pRight.setScore(pRight.getScore() + 1);
			                               ^
PongMain.java:84: operator + cannot be applied to PaddleLeft.getPos,int
		if((pLeft.getPos() - 10) <= ball.getY() && (pLeft.getPos() + 70) > ball.getY())
		                                                        ^
8 errors



PaddleLeft , PaddleRight , and Ball all compiled perfectly though.
Was This Post Helpful? 0
  • +
  • -

#20 sabretooth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 25-June 09

Posted 03 December 2011 - 06:17 AM

"you have finished your first Game Java applet in a very professionally accepted and methodical way"

Please tell me you are joking.

System resource use is amazingly high due to that while loop which does not even need to be there.
The timer is already polling every 15ms, you don't need the while loop also.

I suggest making the Timer an instance variable and ending the start method at time.start();

This way your actionPerformed can look like this:
  public void actionPerformed(ActionEvent arg0) 
            {
                if(pRight.getScore() == 10)
                 {
                    time.stop();
                    currentTime = System.currentTimeMillis() - currentTime;
                 }
                 else
                 {
	         //moves the ball
	         ball.move();
	         //lines the computer paddle up with the ball
	         pRight.setPos(ball.getY() - 35);
	         //checks the ball for a collision
	         checkCollision();
                 }


	         //repaints the applet
	         repaint();
	    }




Much more efficient.
Was This Post Helpful? 1
  • +
  • -

#21 aoshi96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-January 12

Posted 03 January 2012 - 03:07 AM

I followed your tutorial and everything seemed to compile fine in Eclipse. I'm kinda new to coding so I'm not sure what happened but when I tried to run the game using the html I got this error :

java.lang.ClassFormatError: Incompatible magic value 791289869 in class file pongMain
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.ClassFormatError: Incompatible magic value 791289869 in class file pongMain

I also tried using the exact code that you posted up but it didn't work as well. Help, guys(or girls :P)?
Was This Post Helpful? 0
  • +
  • -

#22 EventHorizon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 19-September 09

Posted 03 January 2012 - 04:55 PM

View Postaoshi96, on 03 January 2012 - 03:07 AM, said:

when I tried to run the game using the html I got this error :

yeah, i also get the same error. i don't know what to do
Was This Post Helpful? 0
  • +
  • -

#23 evanf45  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-January 12

Posted 10 January 2012 - 04:14 PM

i am completly new to programing so how do you compile it.

and please tell me every thing i am soposed to do thank you :smile2:

sorry im new :blush: :oops: :/
Was This Post Helpful? 0
  • +
  • -

#24 evanf45  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-January 12

Posted 11 January 2012 - 11:59 AM

what is the html file?
Was This Post Helpful? 0
  • +
  • -

#25 unicanser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-January 12

Posted 29 January 2012 - 06:40 PM

Hi people, I have followed this tutorial but when i tried to start the applet using eclipse it complies fine. But it shows a blank screen with Applet initialised. After awhile the game window shows up with the game over screen with a error Start: Applet not Initialised, what is wrong here? All the codes are fine and the html document is working correctly.
Was This Post Helpful? 0
  • +
  • -

#26 Xente  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 31-January 12

Posted 01 February 2012 - 05:52 PM

View Postevanf45, on 10 January 2012 - 04:14 PM, said:

i am completly new to programing so how do you compile it.

and please tell me every thing i am soposed to do thank you :smile2:

sorry im new :blush: :oops: :/


What did you write it with?
Was This Post Helpful? 0
  • +
  • -

#27 ttoiilledivad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-February 12

Posted 22 February 2012 - 05:01 PM

View Postunicanser, on 29 January 2012 - 06:40 PM, said:

Hi people, I have followed this tutorial but when i tried to start the applet using eclipse it complies fine. But it shows a blank screen with Applet initialised. After awhile the game window shows up with the game over screen with a error Start: Applet not Initialised, what is wrong here? All the codes are fine and the html document is working correctly.


Same here, I'm using eclipse too have checked over the code several times and all that happens is a white screen with "Applet Initialized" In the Bottom corner. I just cant get it to work :(
Was This Post Helpful? 0
  • +
  • -

#28 eekie  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 08-August 12

Posted 09 August 2012 - 08:49 AM

The applet only loading a blank white page seems to be a reocurrance with using Eclipse as the compiler. If there are any suggestions as to why this might be occurring please let us new programmers know.

Something that occurs with me is that under pongMain.java I have:
public class pongMain extends Applet implements MouseMotionListener, ActionListener{

where pongMain is underlined and it prompts me to either suppress it or generate a serial version UID.

Any suggestions?
Was This Post Helpful? 0
  • +
  • -

#29 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10188
  • View blog
  • Posts: 37,629
  • Joined: 27-December 08

Posted 09 August 2012 - 09:07 AM

This is probably a yellow warning, rather than a red error. Some IDEs (note that Eclipse is NOT a compiler) complain about the serialVersionUID. You can add one as prompted or have your IDE ignore it.
Was This Post Helpful? 0
  • +
  • -

#30 eekie  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 08-August 12

Posted 10 August 2012 - 07:12 AM

View Postmacosxnerd101, on 09 August 2012 - 09:07 AM, said:

This is probably a yellow warning, rather than a red error. Some IDEs (note that Eclipse is NOT a compiler) complain about the serialVersionUID. You can add one as prompted or have your IDE ignore it.



Thanks for the information. Adding in the serialVersionUID seemed to resolve the problem. :bananaman:
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3