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.

Results 1 to 2 of 2

Thread: simple ping pong game help

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default simple ping pong game help

    When I ran the methods "go1,go2,go3" separately without clicking on the menu , all of them worked well. by separately I mean, when I closed the program changed the source code from "go1;" to "go2" and go3. But when I click on the "change ball" and then "1 ball" or any thing, all I get is a new frame which is blank. And the old frame freezes.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    /**
     *
     * @author Administrator
     */
     
     
    public class MyPong extends JFrame
    {
     
        Ball ball=new Ball(400,300); 
        Ball ball2=new Ball(250,400);
        Ball ball3=new Ball(150,200);
     
        AIPad aipad=new AIPad();   
     
         public static void main(String[] args)
         {
             MyPong mypong=new MyPong();
             mypong.go1();
         }  
     
         //1 ball method
         public void go1() 
         {          
     
             JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            MyDrawPanel1 drawPanel = new MyDrawPanel1();     
     
            frame.getContentPane().add(drawPanel);
            frame.setSize(650,450);
     
            //menu bar code
     
            JMenuBar menubar = new JMenuBar();        
     
            JMenu jmenu = new JMenu("Change ball");     
     
            JMenuItem b1MenuItem = new JMenuItem("1 Ball"); 
            JMenuItem b2MenuItem = new JMenuItem("2 Balls");     
            JMenuItem b3MenuItem = new JMenuItem("3 Balls");     
     
            b1MenuItem.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent event) {
     
                    go1(); 
            }            
            });
     
            b2MenuItem.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent event) {
                   go2(); 
                }            
            });
     
            b3MenuItem.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent event) {
                    go3(); 
                }            
            });
     
            jmenu.add(b1MenuItem);
            jmenu.add(b2MenuItem);
            jmenu.add(b3MenuItem);
     
            menubar.add(jmenu);
     
            frame.setJMenuBar(menubar);
     
            //menu bar code above
     
            frame.setVisible(true);               
     
            while (true)
            {    
     
               ball.move();
     
                if(ball.dx==-1)
                {
                    aipad.checkLocation(ball.x, ball.y,ball.dx);
     
                }                            
                drawPanel.repaint();
     
     
                try
                {
                    Thread.sleep(5);
                }
                catch(Exception x)
                {
                   x.printStackTrace();
     
                }
            }//end while
     
         }//end go1 method
     
         //2 balls
         public void go2() 
         {          
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            MyDrawPanel2 drawPanel = new MyDrawPanel2();     
     
            frame.getContentPane().add(drawPanel);
            frame.setSize(650,450);
     
              //menu bar code
     
            JMenuBar menubar = new JMenuBar();        
     
            JMenu jmenu = new JMenu("Change ball");     
     
            JMenuItem b1MenuItem = new JMenuItem("1 Ball"); 
            JMenuItem b2MenuItem = new JMenuItem("2 Balls");     
            JMenuItem b3MenuItem = new JMenuItem("3 Balls");     
     
            b1MenuItem.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent event) {
     
                }            
            });
     
            b2MenuItem.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent event) {
     
                }            
            });
     
            b3MenuItem.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent event) {
     
                }            
            });
     
            jmenu.add(b1MenuItem);
            jmenu.add(b2MenuItem);
            jmenu.add(b3MenuItem);
            menubar.add(jmenu);      
     
            frame.setJMenuBar(menubar);
            //menu bar code above
     
            frame.setVisible(true);               
     
            while (true)
            {     
               //several balls logic stays under here   
     
                ball.move();
                ball2.move();
     
     
                if(ball.dx==-1)
                {
                    aipad.checkLocation(ball.x, ball.y,ball.dx);
     
                }
                else
                {
                    aipad.checkLocation(ball2.x, ball2.y,ball2.dx); //give the location of the bal to the aiPad   
     
                }        
     
                drawPanel.repaint();
                //multiple balls logic. yup this is all folks!  
     
                try
                {
                    Thread.sleep(5);
                }
                catch(Exception x)
                {
                   x.printStackTrace();
     
                }
            }//end while
     
         }//end go2 method
     
         //3 balls
         public void go3() 
         {          
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            MyDrawPanel3 drawPanel = new MyDrawPanel3();     
     
            frame.getContentPane().add(drawPanel);
            frame.setSize(650,450);
            frame.setVisible(true);
     
              //menu bar code
     
            JMenuBar menubar = new JMenuBar();        
     
            JMenu jmenu = new JMenu("Change ball");     
     
            JMenuItem b1MenuItem = new JMenuItem("1 Ball"); 
            JMenuItem b2MenuItem = new JMenuItem("2 Balls");     
            JMenuItem b3MenuItem = new JMenuItem("3 Balls");     
     
            b1MenuItem.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent event) {
     
                }            
            });
     
            b2MenuItem.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent event) {
     
                }            
            });
     
            b3MenuItem.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent event) {
     
                }            
            });
     
            jmenu.add(b1MenuItem);
            jmenu.add(b2MenuItem);
            jmenu.add(b3MenuItem);
            menubar.add(jmenu);        
            frame.setJMenuBar(menubar);
            //menu bar code aboves
     
            while (true)
            {     
               //several balls logic stays under here   
     
                ball.move();
                ball2.move();
                ball3.move(); 
     
                if(ball.dx==-1)
                {
                    aipad.checkLocation(ball.x, ball.y,ball.dx);
                    //drawPanel.repaint();
                }
                else if(ball2.dx==-1)
                {
                    aipad.checkLocation(ball2.x, ball2.y,ball2.dx); //give the location of the bal to the aiPad   
     
                }
                else
                {
                    aipad.checkLocation(ball3.x, ball3.y,ball3.dx); //give the location of the bal to the aiPad   
     
                }
     
                drawPanel.repaint();
                //multiple balls logic. yup this is all folks!  
     
                try
                {
                    Thread.sleep(5);
                }
                catch(Exception x)
                {
                   x.printStackTrace();
     
                }
            }//end while
     
         }//end go method
     
        //inner classes drawpanel     
        class MyDrawPanel3 extends JPanel
        {
            @Override
            public void  paintComponent(Graphics g)
            {
                g.setColor(Color.black);
                g.fillRect(0,0,this.getWidth(),this.getHeight());        
     
                aipad.paint(g);             
                ball.paint(g);
                ball2.paint(g); 
                ball3.paint(g);                  
            }
        }
     
        class MyDrawPanel2 extends JPanel
        {
            @Override
            public void  paintComponent(Graphics g)
            {
                g.setColor(Color.black);
                g.fillRect(0,0,this.getWidth(),this.getHeight());        
     
                aipad.paint(g);             
                ball.paint(g);
                ball2.paint(g);                
            }
        }
     
        class MyDrawPanel1 extends JPanel
        {
            @Override
            public void  paintComponent(Graphics g)
            {
                g.setColor(Color.black);
                g.fillRect(0,0,this.getWidth(),this.getHeight());               
                aipad.paint(g);             
                ball.paint(g);                              
            }
        }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,607
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: simple ping pong game help

    I'm not sure what you're trying to do with the while(true) loops. Once you start executing the first one, that thread will never get to any of the other ones.

    It looks like you're trying to setup a game loop, and this isn't how you do that. I suggest looking at threading, Timers, and the EDT. Read up on each and ask a more specific question when you get stuck.

    But the gist is that you should probably use a Swing Timer to setup a game loop instead of abusing the main thread like this.

    --- Update ---

    This thread has been cross posted here:

    http://www.java-forums.org/java-code/90808-simple-ping-pong-game-help.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    GregBrannon (July 14th, 2014)

Similar Threads

  1. Pong Game won't update
    By jimbagas in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 20th, 2014, 07:28 PM
  2. [SOLVED] Need help with Pong game
    By SauronWatchesYou in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 16th, 2014, 07:57 AM
  3. Ping Pong Game Problem with ball Drop and Catch
    By zille in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 26th, 2014, 07:55 AM
  4. Ping Pong Table - Issues with Scores
    By jshcamisado in forum What's Wrong With My Code?
    Replies: 14
    Last Post: February 15th, 2013, 06:11 PM
  5. [SOLVED] Fixing of bug for Pong game
    By phoenix in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 14th, 2009, 02:19 PM