Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / April 2005

Tip: Looking for answers? Try searching our database.

GUI simple pong newbie

Thread view: 
breakingod - 21 Apr 2005 13:59 GMT
Hello i have just recently finished completing the code to a simple applet
that bounces a ball around the screen i am just wondering what it would
take to turn that into a pong program where would i set up the method for
the paddles and how to i distinguish between events of ball movement key
pressing and collisions?
breakingod - 21 Apr 2005 14:00 GMT
here is the code that i have so far...

import java.applet.Applet;
import java.awt.*;

// The "Graphics" class.
public class BouncingBall extends Applet
{
   final int WIDTH = 300;
   final int HEIGHT = 180;

   Button bounce = new Button ("Bounce Ball");
   Canvas display = new Canvas ();

   public void init ()
   {
       setLayout (new BorderLayout ());
       add ("North", bounce);
       display.resize (WIDTH, HEIGHT);
       add ("Center", display);

   }

   public boolean action (Event e, Object o)
   {
       final int RADIUS = 10;
       Graphics displayG;
       int x;
       int y;
       int dx;
       int dy;

       displayG = display.getGraphics ();
       displayG.setColor (Color.blue);
       displayG.fillRect (0, 0, WIDTH, HEIGHT);
       displayG.setColor (Color.black);
       displayG.drawRect (0, 0, WIDTH, HEIGHT);
       x = (int) (Math.random () * (WIDTH - 2 * RADIUS)) + RADIUS;
       y = (int) (Math.random () * (HEIGHT - 2 * RADIUS)) + RADIUS;
       drawBall (displayG, x, y, RADIUS, Color.red);
       dx = 1;
       dy = 1;
       for (int i = 0 ; i <= 10000 ; i++)
       {
           drawBall (displayG, x, y, RADIUS, getBackground ());
           x += dx;
           y += dy;
           if (x <= RADIUS || x >= WIDTH - RADIUS)

               dx = -dx;
           if (y <= RADIUS || y >= HEIGHT - RADIUS)

               dy = -dy;
           displayG.setColor (Color.blue);
           displayG.fillRect (0, 0, WIDTH, HEIGHT);
           drawBall (displayG, x, y, RADIUS, Color.red);
           delay (100000);
       }
       Font f = new Font ("Serif", Font.BOLD, 72);
       displayG.setColor (Color.white);
       displayG.setFont (f);
       displayG.drawString ("Done!", WIDTH / 2 - 80, HEIGHT / 2);
       return true;
   }

   public void drawBall (Graphics g, int x, int y, int radius, Color clr)
   {
       g.setColor (clr);
       g.fillOval (x - radius, y - radius, 2 * radius, 2 * radius);
   }

   public void delay (int howLong)
   {
       for (int i = 1 ; i <= howLong ; i++)
       {
           double garbage = Math.PI * Math.PI;
       }
   }
}
John - 22 Apr 2005 14:17 GMT
> here is the code that i have so far...
>
[quoted text clipped - 75 lines]
>     }
> }

That's nice clean coding. Good effort.

If I were you I would create a separate method that dealt with
collisions. Every "step", you would call this method, and it would check
to see if the ball had hit a wall. It would then return the new velocity
of your ball. You could then extend this method so that it checks for
hitting a bat as well as for hitting a wall.

Get it working for a stationary bat, then look at moving the bat. The
standard way of doing this is to use the mouse. You can "listen" for
mouse movements and cause events to occur each time the mouse is moved
(like moving the bat for example). You may like to look at
java.awt.event.MouseMotionAdapter.

John


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.