Results 1 to 2 of 2
  1. #1
    silvercats is offline Member
    Join Date
    Jun 2014
    Posts
    4
    Rep Power
    0

    Default simple ping pong game help.

    Java Code:
    package mypong;
    
    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);                              
            }
        }
    }

    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.
    Last edited by silvercats; 07-14-2014 at 01:48 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default Re: simple ping pong game help.

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Ping Pong Server/Client problem
    By simon.black325 in forum New To Java
    Replies: 0
    Last Post: 09-20-2011, 05:42 AM
  2. ping pong
    By ahmed_ijsh in forum Threads and Synchronization
    Replies: 2
    Last Post: 11-01-2010, 12:56 PM
  3. ping pong
    By ahmed_ijsh in forum Networking
    Replies: 0
    Last Post: 10-31-2010, 03:11 AM
  4. ping pong
    By ahmed_ijsh in forum Java Gaming
    Replies: 0
    Last Post: 10-31-2010, 03:10 AM
  5. Ping pong game
    By adam405 in forum New To Java
    Replies: 8
    Last Post: 10-20-2010, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •