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); } }