Desktop multiplayer ping-pong game

Hello all,
I've come here to seek advice related to my university project. I am almost a complete newbie at Java - during the past years I've only done programming in C, C++, Symbian and PHP. I remember doing a project in Java on the first year, but now I don't remeber anything.
Let's get to the point. I think I won't have problems with the language itself and the structure of the application. Yet, I would be very grateful if you could guide me to the resources/libraries/classes I should use in my project.
It will be a multiplayer network ping-pong game in a frame window, with a game frame, some menubar and a chat below the game frame. My question is: what class should I use to create a window for the whole application? What class for the game screen, where it will be neccesary to paint a ball jumping here and there and two rackets? What classes would you recommend for handling the network communication between two instances of the application? Any other suggestions would also be welcome. I have less than three weeks to implement this game:)
Thanks in advance.
Maciej Gawronski

I guess you haven't taken the google class yet...
http://www.softlookup.com/tutorial/games/ch2.asp
http://javaboutique.internet.com/tutorials/Java_Game_Programming/
Edited by: Mr.E.H. on Jan 2, 2008 3:01 PM

Similar Messages

  • Ping Pong Game In Java

    Ello,
    Im Trying to write a simple ping pong game in a java netbeans.
    the problem is that i got the ball to move but it doesnt seem to move around the screen just in the same direction. heres the code:
    * PingPong.java
    * Created on 23 November 2006, 15:33
    * @author  rshabbir
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    public class PingPongGame extends javax.swing.JFrame {
        /** Creates new form PingPong */
        public PingPongGame() {
            initComponents();
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
        private void initComponents() {
            jPanel1 = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jButton5 = new javax.swing.JButton();
            jButton6 = new javax.swing.JButton();
            jPanel3 = new DrawingPanel();
            getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("PING PONG");
            jPanel1.setBackground(new java.awt.Color(0, 0, 0));
            jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 255, 255)));
            jLabel1.setFont(new java.awt.Font("Brush Script MT", 1, 14));
            jLabel1.setForeground(new java.awt.Color(255, 255, 255));
            jLabel1.setText("CONTROL PANEL");
            jButton1.setFont(new java.awt.Font("Brush Script MT", 1, 12));
            jButton1.setText("BAT");
            jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton1MouseClicked(evt);
            jButton2.setFont(new java.awt.Font("Brush Script MT", 1, 12));
            jButton2.setText("SPEED");
            jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton2MouseClicked(evt);
            jButton5.setFont(new java.awt.Font("Brush Script MT", 1, 12));
            jButton5.setText("BALL");
            jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton5MouseClicked(evt);
            jButton6.setFont(new java.awt.Font("Brush Script MT", 1, 12));
            jButton6.setText("EXIT");
            jButton6.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton6MouseClicked(evt);
            org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(jLabel1)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                                .add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(jButton6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jButton5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap(60, Short.MAX_VALUE))
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jLabel1)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 15, Short.MAX_VALUE)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jButton1)
                        .add(jButton5))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jButton6)
                        .add(jButton2))
                    .addContainerGap(38, Short.MAX_VALUE))
            getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 290, 140));
            jPanel3.setBackground(new java.awt.Color(0, 0, 0));
            org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
            jPanel3.setLayout(jPanel3Layout);
            jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 290, Short.MAX_VALUE)
            jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 370, Short.MAX_VALUE)
            getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 140, 290, 370));
            pack();
        }// </editor-fold>
        private void jButton6MouseClicked(java.awt.event.MouseEvent evt) {
    // TODO add your handling code here:
        private void jButton5MouseClicked(java.awt.event.MouseEvent evt) {
    // TODO add your handling code here:
        private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
        private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
         * @param args the command line arguments
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new PingPongGame().setVisible(true);
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton5;
        private javax.swing.JButton jButton6;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JPanel jPanel1;
        private DrawingPanel jPanel3;
        // End of variables declaration
    class DrawingPanel extends JPanel
        int x,y,xdir=1,ydir=2 ;
        int interval = 30;
        int x_pos = 10;
        int y_pos = 10;
        int radius = 5;
        int x_speed = 10;
        int y_speed = 10;
        int jframesize_x = 280;
        int jframesize_y = 280;
        //int x = 150, y = 100, r=50;      // Position and radius of the circle
        //int dx = 8, dy = 5;              // Trajectory of circle
    public void paintComponent(Graphics g)
           Rectangle2D rect;
          Ellipse2D e;
           GradientPaint gp;
           Graphics2D gg;
           super.paintComponent(g);
           setBackground(new java.awt.Color(0, 0, 0));
           gg = (Graphics2D)g;
           gg.setColor(Color.red);
           rect = new Rectangle2D.Float(40, 30, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.red);
           rect = new Rectangle2D.Float(110, 30, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.red);
           rect = new Rectangle2D.Float(180, 30, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.green);
           rect = new Rectangle2D.Float(40, 60, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.green);
           rect = new Rectangle2D.Float(110, 60, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.green);
           rect = new Rectangle2D.Float(180, 60, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.blue);
           rect = new Rectangle2D.Float(40, 90, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.blue);
           rect = new Rectangle2D.Float(110, 90, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.blue);
           rect = new Rectangle2D.Float(180, 90, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.yellow);
           rect = new Rectangle2D.Float(40, 120, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.yellow);
           rect = new Rectangle2D.Float(110, 120, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.yellow);
           rect = new Rectangle2D.Float(180, 120, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.magenta);
           rect = new Rectangle2D.Float(110, 150, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.magenta);
           rect = new Rectangle2D.Float(110, 180, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.magenta);
           rect = new Rectangle2D.Float(110, 210, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.pink);
           rect = new Rectangle2D.Float(180, 150, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.pink);
           rect = new Rectangle2D.Float(180, 180, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.pink);
           rect = new Rectangle2D.Float(180, 210, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.cyan);
           rect = new Rectangle2D.Float(40, 150, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.cyan);
           rect = new Rectangle2D.Float(40, 180, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.cyan);
           rect = new Rectangle2D.Float(40, 210, 60, 20);
           gg.draw(rect);
           gg.fill(rect);
           gg = (Graphics2D)g;
           gg.setColor(Color.white);
           rect = new Rectangle2D.Float(110, 350, 60, 10);
           gg.draw(rect);
           gg.fill(rect);
           //g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
            try{
               Thread.sleep(15);
           } catch(InterruptedException E){}
           repaint();
           x_pos++;
           y_pos++;
           repaint();
            e = new Ellipse2D.Float(x,y,10,10); 
            gp = new GradientPaint(150,50, Color.white, 200,100, Color.white, true);
            gg.setPaint(gp);
            gg.fill(e);
            try{
               Thread.sleep(15);
           } catch(InterruptedException E){}
           repaint();
           x=x+1;
           y=y+1;
           repaint();
           if(x > jframesize_x - radius)
            x_speed = -1;
          else if(x < radius)
             x_speed = +1;
          x += x_speed;
          y += x_speed;
          x--;
          y--;
          //if ((x - r + x_pos < 0) || (x + r + x_pos > jframesize_x)) x_pos = x_pos;
          //if ((y - r + y_pos < 0) || (y + r + y_pos > jframesize_y)) y_pos = y_pos;
          //x += x_pos;  y += y_pos;
          //repaint(x-r-x_pos, y-r-x_pos, 2*r, 2*r);   // repaint old position of circle
          //repaint(x-r, y-r, 2*r, 2*r);  
         // try { Thread.sleep(50); } catch (InterruptedException e) { ; }
           

    go away. u just dont want to helpFuck off jackfuck.
    You whined the same thing in the other thread when people asked for code tags. You have also got lots of actual advice and suggestions in your other thread and ignore them all.
    Crossposting is rude but I suppose we should not expect any better from you because so far all you have demonstrated on this site is that you are rude, incredibly stupid and lazy.

  • Part II - Playing Ping-Pong ... now with KaLin?

    Just call me flummoxed ...
    I went to my "profile page for the forum" by clicking on my name. (So far so good)
    I looked "at the top of the middle column," (Puzzled, I search for KaLin's promised support.)
    THERE IS NOTHING REMOTELY RESEMBLING "an area titled "My Private Support Cases," nor any selection that leads to "My Private Support Cases."
    THERE IS NO LINK to "the private board where you and the agent may exchange information."
    And thus, it seems like the ping-pong game continues ...
     I used to wonder why nobody responded to their original posts with updates.  It appears that moderators prevent this from happening.
    Eager for assistance, I remain yours sincerely,
    marlowrog

    Please check your Private Message Inbox. It should look like this -->>
    If a forum member gives an answer you like, give them the Kudos they deserve. If a member gives you the answer to your question, mark the answer that solved your issue as the accepted solution.

  • Trying to make a Ping Pong Ball generate in random location

    Hey guys and Gals,
    im trying to make a pingpong ball, well 2 of them spawn in random locations so that it makes the game a little harder as of right now the ping pong balls spawn right next to each other and untill score 5-6 there right next to each other

    I disagree with Caillen.   If somebody is trying to learn a programming language writing a game is an excellent way of learning. In the constructor to the form (or form load function ) you can change the X,Y location of the ball(s). You also want
    to make sure the two balls don't overlap. To make the programming simpler I would start the two balls at the same Y coordinate (or X).
    See code below.  I used a little clever programming to prevent the two ball from overlapping. The code assumes the left side of the panel x = 0.  If it is not zero then add the X of the left side of the panel to the results.
    int widthPanel = 10;
    int diameterBall = 2;
    Random rand = new Random();
    int ball1X = rand.Next(diameterBall / 2, widthPanel - (diameterBall / 2));
    //now get a random number that is space not taken up by ball one
    int ran2 = rand(0, widthPanel - diameterBall);
    int ball1Left = ballX - (diameterBall / 2);
    int ball2X = 0;
    if(rand2 <= ball1X)
    ball2X = rand2;
    else
    int ball1Right = ballX + (diameterBall / 2);
    ran2 -= ball1Left;
    ball2X = ball1Right + ran2;
    jdweng

  • Why is my pong game not working?

    I made a kinda nice pong game in which two players can play over the internet...or that's the idea.
    I made a very simple UDP client and server, they both work very well in localhost and with computers hooked up with in a LAN.
    Then I went to whatismyip.com to find out what is my IP, I started my server on that machine and when I tried using the client on my other computer my server didn't receive anything.
    I'm fairly sure the IP is correct since it pinged for <1ms.
    What I am doing wrong?
    Thanks.

    Firewall!

  • Motion Tracking Ping pong ball

    I'm superimposing a ping pong ball into live footage of a match but with the actions acting the hitting.
    What do people think is the best way to do this?
    I was thinking maybe its best to put the 'ball' under a null, motion track the null and animate the ball underneath for scaling and position.
    Maybe motion tracking isn't an option and I should just animate the ball movements?
    Has anyone done this or may have any possible suggestions?
    Thanks!
    SG

    Make your life easy, hand-animate it. Pingpong is a fast game, resulting in lots of motionblurred elements and it will be a nightmare to track. You'll waste a lot of time on fixing a poor track that you could have spent refining your animation to begin with....
    Mylenium

  • Creating a Pong game in AS3 using a tutorial

    Hi,
    I'm a beginner using AS3 and found a great tutorial on how to create a Pong game: http://www.foundation-flash.com/tutorials/pong4/. There's a link to the source code: http://www.foundation-flash.com/source/viewer.php?file=pong4.txt.
    When I add the source code to a new AS file (using Flash CS 5.5), save it as "Main.as", create a new Fla and link it in the actionscript settings to the Main.as and run it, I get errors:
    C:\Users\xxx\Desktop\P4\Main.as, Line 117
    1093: Syntax error.
    C:\Users\xxx\Desktop\P4\Main.as, Line 117
    1084: Syntax error: expecting rightparen before 270.
    C:\Users\xxx\Desktop\P4\Main.as, Line 121
    1093: Syntax error.
    C:\Users\xxx\Desktop\P4\Main.as, Line 121
    1084: Syntax error: expecting rightparen before 270.
    Here's the section of code being referred to in the error message:
    116 public function resetHandler(e:Event){
    117         if(ball.x < (0 – 270)){
                    scoreText2.text = String(Number(scoreText2.text ) + 1)
                    reset();
    121         if(ball.x > (550 – 270)){
                    scoreText1.text = String(Number(scoreText1.text ) + 1)
                    reset();
    I must be doing something wrong? Is there some way to get this code and Pong game sample running ?
    Any help would be appreciated.
    Regards,
    saratogacoach

    As Raymond said, its an issue with the 'dashes' in your if statements; they need to be minus signs, its just the wrong character.
    On a related note..why are you (the tutorial?) doing basic equations in a conditional statement? 0 – 270 and 550 – 270 are always going to be the same value...why compute it on every check?

  • Ping online game

    i want to create a ping-pong online game as applet and i have a few problems:
    the game contains 4 classes:
    user pedal
    computer pedal
    ball
    boardgame
    do i have to use 4 applet(1 applet to every class)?
    or to use one applet that contains 4 panels?
    did every class contain the paint metode?

    The way i will do it is: 1 Applet, 4 classes that extends Component each have a paint method.
    Noah

  • Pong Game Problem

    I have a problem with my pong game. IT says its missing a return statement at :71: and at someplace else. How do I fix this? Below is the source code. Note that I haven't done puck-paddle collisions yet. It isn't supposed to do those right now anyway, so please try to fix the return statement problem, not that. Here is the code:
    import java.applet.*;
    import java.awt.*;
    public class Ponggame extends Applet implements Runnable
         int score = 0;
         int otherscore = 0;
         int y_speed = 0;
         int other_y_speed = 0;
         int appletsize_x = 640;
         int appletsize_y = 480;     
         int xpos = 620;
         int ypos = 220;
         int otherxpos = 20;
         int otherypos = 220;
         int ball_xpos = 130;
         int ball_ypos = 130;
         int radius = 20;
         int ball_xspeed = 2;
         int ball_yspeed = 2;
         int paddle_width = 32;
         int paddle_height = 80;
         int other_paddle_width = 32;
         int other_paddle_height = 32;
         //Variables for Double Buffering
         private Image dbImage;
         private Graphics dbg;
         public void init()
              setBackground (Color.black);
         public void start()
              Thread th = new Thread (this);
              th.start ();
         public void stop()
         public void destroy()
         public boolean keyDown (Event e, int key)
              if (key == Event.UP)
                   y_speed = -2;
              else if (key == Event.DOWN)
                   y_speed = 2;
              else if (key == 119)
                   other_y_speed = -2;
              else if (key == 115)
                   other_y_speed = 2;
         public boolean keyUp (Event e, int key)
              if (key == Event.UP)
                   y_speed = 0;
              else if (key == Event.DOWN)
                   y_speed = 0;
              if (key == 119)
                   other_y_speed = 0;
              else if (key == 115)
                   other_y_speed = 0;
         public void run()
              Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
              while (true)
                   if (ball_xpos > appletsize_x - radius)
                        otherscore += 1;
                        ball_xpos = 120;
                        ball_ypos = 120;
                   else if (ball_xpos < radius)
                        score += 1;
                        ball_xpos = 120;
                        ball_ypos = 120;     
                   else if (ball_ypos > appletsize_y - radius)
                        y_speed = -2;
                   else if (ball_ypos < radius)
                        ball_yspeed = +2;
                   else if (ypos<0)
                        ypos = 0;
                   else if (ypos+paddle_height>appletsize_y)
                        ypos = (appletsize_y - paddle_height);
                   else if (otherypos<0)
                        otherypos=0;
                   else if (otherypos+other_paddle_height>appletsize_y)
                        otherypos = (appletsize_y - other_paddle_height);
                   ypos += y_speed;
                   otherypos += other_y_speed;
                   ball_xpos += ball_xspeed;
                   ball_ypos += ball_yspeed;
                   // Neuzeichnen des Applets
                   repaint();
                   Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
         public void update (Graphics g)
              if (dbImage == null)
                   dbImage = createImage (this.getSize().width, this.getSize().height);
                   dbg = dbImage.getGraphics ();
              dbg.setColor (getBackground ());
              dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
              dbg.setColor (getForeground());
              paint (dbg);
              g.drawImage (dbImage, 0, 0, this);
         public void paint (Graphics g)
              g.setColor (Color.red);
              g.fillOval (ball_xpos - radius, ball_ypos - radius, 2 * radius, 2 * radius);
              g.setColor (Color.white);
              g.fillRect (xpos, ypos, paddle_width, paddle_height);
              g.fillRect (otherxpos, otherypos, other_paddle_width, other_paddle_height);
    }

    Your public boolean keyXXX methods don't return a boolean.
    And use code tags when posting code. There is a code button above the message editor.

  • Pong Game - HELP

    Hi guys, I'd firstly like to apologise if I've posted in the wrong forum - as you've moved probably guessed, I'm new round "there here parts"! :P
    I recently started attempting some basic java graphics in a swing. So far, everything is working but I have the following questions:
    1) How do I stop the graphics flickering? - I know it's due to the thread refresh rate in the continuous loop, but can I stop the flickers?
    2) How do I add a JPanel ABOVE the canvas in which I can add buttons to restart the game? - I assume it would involve a GridBagLayout which I'm unfamiliar with, so your assistance would be appreciated. Note: I'll only need assistance in adding the JPanel above the canvas, I can do the rest! :)
    My current code is shown below - please don't complain about imperfections, as it's just a development:
    import javax.swing.*;
    import java.awt.image.*;
    import java.awt.event.*;
    import java.awt.*;
    public class Pong
       private static JFrame frame;
       private static Canvas canvas;
       private static KeyListener listener;
       private static boolean inPlay = true;
       private static boolean gamePaused = false;
       private static int yP1 = 160;
       private static int yP2 = 160;
       private static Graphics Bgfx;
       private static boolean update = false;
       private static BufferedImage bi;
       private static int ballPosx = 600;
       private static int ballPosy = 200;
       private static int ballXSpeed = 1;
       private static int ballYSpeed = 1;
       private static int ballOffSetx = 1;
       private static int ballOffSety = -1;
       private static int paddleSpeed = 10;
       private static boolean winnerP1 = false;
       private static boolean winnerP2 = false;
       //Upper panel variables
       private static JPanel upperPNL;
       private static JButton restartButton;
       private static GridBagConstraints gbc;
       public static void main (String args[])
          //Initialize GUI
          frame = new JFrame("Pong Game");
           //Canvas / frame parameters
          canvas = new Canvas();
          canvas.setSize(1200, 500);
           //Content pane
           Container content = frame.getContentPane();
           content.add(canvas);
          frame.setIgnoreRepaint(true);                
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
          frame.pack();
          frame.setVisible(true);
           frame.addKeyListener(listener);
           frame.addKeyListener(new KeyListener() {
              public void keyPressed(KeyEvent e) {
                   if (e.getKeyCode() == 'Q') {
                        if (yP1 > 20) {
                             yP1 -= paddleSpeed;
                             update = true;
                      if (e.getKeyCode() == 'A') {
                           if (yP1 < 380) {
                             yP1 += paddleSpeed;
                             update = true;
                   if (e.getKeyCode() == 'P') {
                        if (yP2 > 20) {
                             yP2 -= paddleSpeed;
                             update = true;
                   if (e.getKeyCode() == 'L') {
                        if (yP2 < 380) {
                             yP2 += paddleSpeed;
                             update = true;
                   public void keyReleased(KeyEvent e) {
                   update = false;
                    public void keyTyped(KeyEvent e) {}
          gameLoop(canvas.getGraphics());                     //Begin Game Loop
       private static void gameLoop(final Graphics gfx)
          //Create a BufferedImage to use it's graphics.
          bi = new BufferedImage(1200, 500, BufferedImage.TYPE_INT_RGB);
          //While the game isn't paused
          while(inPlay)
         while(!gamePaused)
              try
                      Thread.sleep(1);
              catch(Exception ex)
                   System.out.println("Interrupted Sleep");
              gameRender(bi, gfx);
              moveBall();
              checkCollisions();
       private static void moveBall()
              if (winnerP1 || winnerP2) { ballOffSety = 0; ballOffSetx = 0; }
              ballPosx += ballXSpeed * ballOffSetx;
              ballPosy += ballYSpeed * ballOffSety;
              if (ballPosy < 0) {
                   ballOffSety *= -1;
              if (ballPosy > 500) {
                   ballOffSety *= -1;
              if (ballPosx < 0) {
                   ballOffSetx *= -1;
              if (ballPosx > 1200) {
                   ballOffSetx *= -1;
         private static void checkCollisions() {
              if ((ballPosx >= 72 && ballPosx <= 107) && (ballPosy >= yP1 && ballPosy <= (yP1 + 100)))
                   ballOffSetx *= -1;
              if ((ballPosx >= 1072 && ballPosx <= 1107) && (ballPosy >= yP2 && ballPosy <= (yP2 + 100)))
                   ballOffSetx *= -1;
              if (ballPosx < 107)
                   winnerP2 = true;
              if (ballPosx > 1107)
                   winnerP1 = true;
       private static void gameRender(BufferedImage bi, Graphics gfx)
           if (winnerP1) {
              bi.getGraphics();
              Font font = new Font("Arial", Font.PLAIN, 36);
              gfx.setFont(font);
              gfx.setColor(Color.RED);
              gfx.drawString("PLAYER 1 WINS!!!", 520, 200);
              gfx.drawImage(bi, 0, 0, null);
           if (winnerP2) {
              bi.getGraphics();
              Font font = new Font("Arial", Font.PLAIN, 36);
              gfx.setFont(font);
              gfx.setColor(Color.RED);
              gfx.drawString("PLAYER 2 WINS!!", 520, 200);
              gfx.drawImage(bi, 0, 0, null);
          //Get Buffered Graphics to Draw To
          Bgfx = bi.getGraphics();
          //Draw Background
          Bgfx.setColor(Color.BLACK);
          Bgfx.fillRect(0,0,1200,500);
          //Draw Bar 1
          gfx.setColor(Color.GREEN);
           gfx.fillRect(72,yP1,35,100);
          //Draw Bar 2
          gfx.fillRect(1072,yP2,35,100);
          //Draw Ball
          gfx.fillRect(ballPosx,ballPosy,20,20);
          //Draw the Buffered Graphics to the screen
          gfx.drawImage(bi, 0, 0, null);
    }Constructive criticism is welcomed, but major edits which I'm not likely to understand as of yet are not.
    Many thanks for your assistance, it's appreciated.
    Edited by: -Barto- on Jun 30, 2010 5:21 PM

    -Barto- wrote:
    Hi guys, I'd firstly like to apologise if I've posted in the wrong forum - as you've moved probably guessed, I'm new round "there here parts"! :PWelcome, and not to worry -- this is the correct forum for your question.
    I recently started attempting some basic java graphics in a swing. So far, everything is working but I have the following questions:
    1) How do I stop the graphics flickering? - I know it's due to the thread refresh rate in the continuous loop, but can I stop the flickers?
    2) How do I add a JPanel ABOVE the canvas in which I can add buttons to restart the game? - I assume it would involve a GridBagLayout which I'm unfamiliar with, so I think that you're main problem is that you're trying to mix AWT components (i.e., Canvas) and Swing components (everything else) in one program. In addition, you need to update your game loop and graphics to be in line with Swing best practices. So, get rid of Canvas, read up in the tutorials on how to do graphics in Swing (using paintComponent override for instance) and use a Swing Timer for your Game loop and you'll be half way there. Best of luck!

  • Code i use to increase the difficulties on a flash pong game?

    i need code to apply on my flash game to make the pong game more difficult as the game goes on.
    help?

    lwood655 wrote:
    I changed the passcode needed to open my phone,  when I try to do a restore on this phone, it is asking for a passcode and not accepting the one I changed it to
    There are several different passcodes associated with any iOS device:
    The screen unlock passcode. This is normally a 4 digit code (although there is an option to make it longer).
    Your iCloud passcode. This controls Find my iPhone and Activation Lock, the theft deterrent measure that prevents a stolen or lost phone from being used. This will be prompted for if Find my iPhone is enabled when you restore the phone.
    Possibly the passcode to unlock an encrypted backup, as @Drew Reece pointed out.

  • What do i need for a classic pong game with real-life interface/ speedy-33?

    Lo guys, i'm a student, for my academic project i was kinda hoping to do a classic pong game with LabVIEW... 2 Palettes, a ball... except the goal is to involve the "outside world in this poject". So i was thinking of moving the palletes when a player's finger is moved (i guess i need a sensor, was thinking of placing a bright color on each finger so as to be detected). Note that i would be using speedy-33
    please guys i am very new to labview, and i need a project, i got one but i still dunno how to produce it... Thank you all
    Best regards.
    T. A.

    This should give you a good start. We did this as a wait window when we were updating software. It is old code so I do not want any comments on the coding style. It was done in fun and has not been updated in almost ten years.
    You will have to update the controls to work from a control vs automatically. Have fun let me know how this turns out.
    Tim
    Johnson Controls
    Holland Michigan
    Attachments:
    Update Software.vi ‏10 KB
    PONG.vi ‏55 KB

  • Ping Pong -- Effect Help

    I'm new to Motion, I want to make my a segment of my clip play play backwards play ... ect... but when I use the ping- pong effect which seems like the one to use my audio gets messed up.
    Is there a 'snap to audio' function .. so at the end of the loop I can get the rest of the audio for the clip.

    i tried that but when take the clip and add ping pong ... and then extend the clip to get the rest of the video, the audio doesn't extend as well / auto loop

  • Ping Pong Balls!

    Yes! Someone described it!
    I've upgraded to an iMac G5, and am passing my 600mHz Graphite onto someone else. When the Graphite was in my ownership, every time I started up, I heard one or two ping pong balls type sound, and everything started up hunky dory.
    At the time, my HD was partitioned into 3 (15/15/10) with Panther on one and OS9 on another.
    Now, I'm trying to get everything set up all virginal like for the hand-over.
    Originally repartioned the HD into 2 (36/4) and reinstalled OSX on the larger, OS9 on the smaller. Transferred a lot of stuff from an old Rev B 233 iMac into the smaller partition for later use. Did Software updates and started hearing really strange sounds - like a constant arcing (though it didn't quite sound electrical).
    Have repartitioned again the HD - 1 piece only. Now I'm hearing a LOT of the pingpong ball sound and startup and other operations sometimes are painfully slow (even shutdown). Have zapped PRAM (helped a little, it seems), but not perfectly. Is the HD on the way out?

    www.iamnotamerican.com...
    Every HD that I have heard that from has died, however partitioning may have a different effect.
    I have an IBM HD that once was partitioned for use in my iMac years ago. It was a 5400 drive that would spinup and "yaw" (like a donkey noise) for each partition at boot. I soon replaced it however it is still alive for use in a USB enclosure for backup purposes.
    So it may be on the way out OR in the process of booting it is going through the mounting of each partition to complete the boot. I would start backing up and be prepared for the worse should it later go sour.
    ...Ron

  • Is there a way to make a "folder" on the desktop to store purchased games, etc instead of taking up space on the homepage?

    Is there a way to make a "folder" on the desktop to store purchased games, etc. instead of the icons taking up space on the desktop...

    Yes.  See p. 39 of the manual... http://manuals.info.apple.com/en_US/ipad_2_user_guide.pdf

Maybe you are looking for

  • Docs won't open in Pages on Mac, from previous edit on iPad

    I broached this subject in another post on iCloud b/c I thought maybe that was the culprit.  I was able to get things working that time by updating passwords...although NOT because any passwords were wrong.  Here's the post from that discussion (whic

  • ITunes won't sync my music to my iPhone

    I am trying to get music from my iTunes to my iPhone but it won't transfer any of the songs to it. Even if uncheck sync all music and choose exactly what i want it will sync but it wont add or delete anything ive told it too. Ive told it to sync mult

  • How to locate the source code which populate the SO number?

    Hi, For example: In T-code: VA01 Put your cusor on the screen field : Standard Order Then press F1, get the technical info of this field as below shows: Screen field     VBAK-VBELN Program name     SAPMV45A Screen no.       4001 So my question is, ho

  • Macbook hardisk ticking, is my hardisk almost dead?

    I have an Alu Unibody Macbook.. Been taking good care of it over the past 3-4 months..Yesterday I had the lid closed and it was sleeping..I lifted the lid and I got the rainbow wheel..My hardisk then began to tick loudly until it stopped. I closed th

  • Redirecting from a frame to _top

    When I want to do this: Click here I can use: <SCRIPT LANGUAGE=JAVASCRIPT>window.navigate("somepage.html")</SCRIPT> But when I use the above code in a frame the somepage.html opens in the frame. What can I do to let it open, not only in the frame, bu