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 10 of 10

Thread: Need Loop Help for Game

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Need Loop Help for Game

    im trying to create a game where there are two buttons and you have to click the one with the even number to continue or else you lose. I need help on how to make it loop. I am a noob at java programming so please forgive me : D.
    This is my code:
    package button_game;
     
    import javax.swing.*; 
    import java.awt.*;
    import java.awt.event.*;
    public class Button_Game {
     
    //A1 and B1 contain what to do when they are clicked. Right now they terminate the program until i figure out how to loop it.
    private static class A1 implements ActionListener{
        public void actionPerformed(ActionEvent e){
               System.exit(0);
        }
     
    }
     private static class B1 implements ActionListener {
            public void actionPerformed(ActionEvent e){  
                System.exit(0);
        }  
        }
     //addComponentsToPane positions the buttons on the screen randomly
        public static void addComponentsToPane(Container pane){
     
                     int x; 
          x = (int)(200*Math.random());
          int y;
          y = (int)(200*Math.random());
            pane.setLayout(null);
     
           int a = (int)(1000*Math.random());
            int b = a%2;
            if (b == 0){
                String c = Integer.toString(a);
        JButton A2 = new JButton(c);
        JButton B2 = new JButton();
        A1 listener = new A1();
        A2.addActionListener(listener);                
        B2.addActionListener(listener);
        pane.add(B2);
        pane.add(A2);
        Insets insets = pane.getInsets();
        Dimension size = B2.getPreferredSize();
        B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
        size = A2.getPreferredSize();
        A2.setBounds(y + insets.left, y + insets.top, size.width, size.height);
            }
            else if(b != 0);
            int c = a +1;
            String d = Integer.toString(c);
     JButton A2 = new JButton(d);
        JButton B2 = new JButton();
        A1 listener = new A1();
        A2.addActionListener(listener);                
        B2.addActionListener(listener);
        pane.add(B2);
        pane.add(A2);
        Insets insets = pane.getInsets();
        Dimension size = B2.getPreferredSize();
        B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
        size = A2.getPreferredSize();
        A2.setBounds(y + insets.left, y + insets.top, size.width, size.height); 
     
     
            }
        //this runs the game
     
        private static void runGUI(){
     
            JFrame window = new JFrame("Button Game");
            addComponentsToPane(window.getContentPane());
     
            window.setSize(300,300);
            window.setVisible(true);
     
        }
     
    //this part runs the subroutine which runs the game
     
       public static void main(String Args[]){
     
      javax.swing.SwingUtilities.invokeLater(new Runnable(){
          public void run(){
              runGUI();
          }
      });
     
      }
     
    }
    Last edited by Shivam24; July 17th, 2011 at 05:53 PM.


  2. #2
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Need Loop Help for Game

    please someone respond!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: Need Loop Help for Game

    I need help on how to make it loop.
    I need some info about your loop.
    What is to be done in the loop?
    When will the execution flow exit the loop?

    Please edit your code and wrap it in code tags to preserve formatting. Your code is all shifted left making it hard to read. See: BB Code List - Java Forums
    or Go Advanced and use the # icon
    Last edited by Norm; July 17th, 2011 at 08:45 PM.

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Need Loop Help for Game

    Quote Originally Posted by Norm View Post
    I need some info about your loop.
    What is to be done in the loop?
    When will the execution flow exit the loop?

    Please edit your code and wrap it in code tags to preserve formatting. Your code is all shifted left making it hard to read. See: BB Code List - Java Forums
    or Go Advanced and use the # icon
    I want to loop the whole program so that if you click on button A, the whole thing restarts and the buttons shift positions. If you click button B, the program exits.

    Alright i think this is what you mean by adding code tags. I am new so i don't know much about posting code. Any suggestions would be helpful! : )
     package button_game;
     
    import javax.swing.*; 
    import java.awt.*;
    import java.awt.event.*;
    public class Button_Game {
     
    //A1 and B1 contain what to do when they are clicked. Right now they terminate the program until i figure out how to loop it.
    private static class A1 implements ActionListener{
        public void actionPerformed(ActionEvent e){
               System.exit(0);
        }
     
    }
     private static class B1 implements ActionListener {
            public void actionPerformed(ActionEvent e){  
                System.exit(0);
        }  
        }
     //addComponentsToPane positions the buttons on the screen randomly
        public static void addComponentsToPane(Container pane){
     
                     int x; 
          x = (int)(200*Math.random());
          int y;
          y = (int)(200*Math.random());
            pane.setLayout(null);
     
           int a = (int)(1000*Math.random());
            int b = a%2;
            if (b == 0){
                String c = Integer.toString(a);
        JButton A2 = new JButton(c);
        JButton B2 = new JButton();
        A1 listener = new A1();
        A2.addActionListener(listener);                
        B2.addActionListener(listener);
        pane.add(B2);
        pane.add(A2);
        Insets insets = pane.getInsets();
        Dimension size = B2.getPreferredSize();
        B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
        size = A2.getPreferredSize();
        A2.setBounds(y + insets.left, y + insets.top, size.width, size.height);
            }
            else if(b != 0);
            int c = a +1;
            String d = Integer.toString(c);
     JButton A2 = new JButton(d);
        JButton B2 = new JButton();
        A1 listener = new A1();
        A2.addActionListener(listener);                
        B2.addActionListener(listener);
        pane.add(B2);
        pane.add(A2);
        Insets insets = pane.getInsets();
        Dimension size = B2.getPreferredSize();
        B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
        size = A2.getPreferredSize();
        A2.setBounds(y + insets.left, y + insets.top, size.width, size.height); 
     
     
            }
        //this runs the game
     
        private static void runGUI(){
     
            JFrame window = new JFrame("Button Game");
            addComponentsToPane(window.getContentPane());
     
            window.setSize(300,300);
            window.setVisible(true);
     
        }
     
    //this part runs the subroutine which runs the game
     
       public static void main(String Args[]){
     
      javax.swing.SwingUtilities.invokeLater(new Runnable(){
          public void run(){
              runGUI();
          }
      });
     
      }
     
    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: Need Loop Help for Game

    click on button A, the whole thing restarts and the buttons shift positions.
    What is the "whole thing" that should restart? What are the exact lines of code that you want executed?
    You might be able to make the "whole thing" a method and call it when the A button is clicked.

    Your formatting doesn't align the ending } with the line with the opening {
    That makes it hard to see code levels in if statements.
    Last edited by Norm; July 17th, 2011 at 09:27 PM.

  6. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Need Loop Help for Game

    Quote Originally Posted by Norm View Post
    What is the "whole thing" that should restart? What are the exact lines of code that you want executed?
    You might be able to make the "whole thing" a method and call it when the A button is clicked.

    Your formatting doesn't align the ending } with the line with the opening {
    That makes it hard to see code levels in if statements.
    By whole thing, I mean that I want the whole program to run again and again as long as you keep clicking Button A1 and it to exit when you click Button B1.

    Here is my revised code:
    I think i got most of the brackets
    package button_game;
     
    import javax.swing.*; 
    import java.awt.*;
    import java.awt.event.*;
    public class Button_Game {
     
    //A1 and B1 contain what to do when they are clicked. Right now they terminate the program until i figure out how to loop it.
    private static class A1 implements ActionListener{
        public void actionPerformed(ActionEvent e){
               System.exit(0);
                                                                  }
     
                                                                     }
     private static class B1 implements ActionListener {
            public void actionPerformed(ActionEvent e){  
                System.exit(0);
                                                                      }  
                                                                       }
     //addComponentsToPane positions the buttons on the screen randomly
        public static void addComponentsToPane(Container pane){
                     int x; 
          x = (int)(200*Math.random());
          int y;
          y = (int)(200*Math.random());
            pane.setLayout(null);
           int a = (int)(1000*Math.random());
            int b = a%2;
            if (b == 0){
                String c = Integer.toString(a);
        JButton A2 = new JButton(c);
        JButton B2 = new JButton();
        A1 listener = new A1();
        A2.addActionListener(listener);                
        B2.addActionListener(listener);
        pane.add(B2);
        pane.add(A2);
        Insets insets = pane.getInsets();
        Dimension size = B2.getPreferredSize();
        B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
        size = A2.getPreferredSize();
        A2.setBounds(y + insets.left, y + insets.top, size.width, size.height);
                            }
            else if(b != 0);
            int c = a +1;
            String d = Integer.toString(c);
     JButton A2 = new JButton(d);
        JButton B2 = new JButton();
        A1 listener = new A1();
        A2.addActionListener(listener);                
        B2.addActionListener(listener);
        pane.add(B2);
        pane.add(A2);
        Insets insets = pane.getInsets();
        Dimension size = B2.getPreferredSize();
        B2.setBounds(x + insets.left, x + insets.top, size.width, size.height);
        size = A2.getPreferredSize();
        A2.setBounds(y + insets.left, y + insets.top, size.width, size.height); 
                                                              }
        //this runs the game
     
        private static void runGUI(){
     
            JFrame window = new JFrame("Button Game");
            addComponentsToPane(window.getContentPane());
     
            window.setSize(300,300);
            window.setVisible(true);
     
                                    }
     
    //this part runs the subroutine which runs the game
     
       public static void main(String Args[]){
     
      javax.swing.SwingUtilities.invokeLater(new Runnable(){
          public void run(){
              runGUI();
                                }
                                                                              });
     
                                                                              }
     
                             }

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Need Loop Help for Game

    never mind they un-align when i post the code

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: Need Loop Help for Game

    I want the whole program to run again
    Are you sure? Starting with creating a new Frame and new buttons and new listeners etc?
    If that is the case, then start a new thread and call the class's main() method.

  9. The Following User Says Thank You to Norm For This Useful Post:

    Shivam24 (July 18th, 2011)

  10. #9
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: Need Loop Help for Game

    Im not sure exactly what you meant by starting a new thread. Do you mean start it inside:
    private static class A1 implements ActionListener{
        public void actionPerformed(ActionEvent e){
     
     
     
              }
                                                  }
    or just anywhere before the main method

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: Need Loop Help for Game

    I want the whole program to run again and again as long as you keep clicking Button A1
    I guess in the actionPerformed method would be ok. Create a Thread that calls the class's main() method.

    I've never done it so not sure what will happen.
    You'll probably get a whole new window, etc.

Similar Threads

  1. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 11:05 AM
  2. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  3. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 04:27 PM
  4. A thread as game loop
    By maikeru in forum Threads
    Replies: 0
    Last Post: December 25th, 2009, 08:01 PM
  5. Job offers to program Hobo Wars
    By MooncakeZ in forum Paid Java Projects
    Replies: 7
    Last Post: September 17th, 2009, 09:41 PM