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

Thread: Help with Gui+RockPaperScissors

  1. #1
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Help with Gui+RockPaperScissors

    I have tried to succesfully merge my Rock Paper Scissors project with the gui I made for it.

    Summary of my code.
    --I click start, and it makesvisible 3 boxes, rock paper scissors.
    --I start a for loop which loops 3 times starting at these boxes being visible.
    --It goes down various methods of checking the button(The button in its listener turns a boolean true)


    My Problem: It doesn't act like the traditional for loop im used to. It shoots down the loops and still executes stuff not even in the for loop. It's like the for loop is instantanious. It's not like with Scanner, and it had to wait for information to continue it's like its not expecting user to put a button then is still executing below.

    Here is the code for reference, I threw it together in rather of minutes so don't judge it to perfection.
    package rpsgui;
     
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.util.Random;
    import java.util.Scanner;
    import javax.swing.*;
    import javax.swing.Icon;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    public class RPSgui {
     JButton plr;
    JButton rock;
     JButton paper;
     JButton sci;
      JButton scei;
     static int tie=0;
     static int wins=0;
     static int lose=0;
    static boolean rck=false;
    static boolean papr=false;
    static boolean scissor=false;
    static boolean prock=false;
    static boolean ppaper=false;
    static boolean pscissors=false;   
      public static void main(String[] args) {
     
    // Creates JFrame.
    JFrame jf = new JFrame("RPS - @Xyn") ;
    jf.setSize(600, 600);
    jf.setLocation(600,300);
    jf.setVisible(true);    
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setLayout(new GridLayout(7,44));
    // Create Panel
    JPanel jp = new JPanel();
     
     
    // Creates Play JButton.
    final JButton plr = new JButton("PLAY NOW");
    final JButton rock = new JButton("Rock");
    final JButton paper=new JButton("Paper");
    final JButton scei=new JButton("Scissors");
    // Set things
    plr.setSize(200, 200);
    plr.setVisible(true);
    rock.setSize(300,300);
    rock.setVisible(false);
    paper.setSize(300,300);
    paper.setVisible(false);
    scei.setSize(300,300);
    scei.setVisible(false);
     
    //set more things
     
     
     
    // Add things to JFrame.
    jf.add(plr);
    jf.add(jp);
    jf.add(rock);
    jf.add(paper);
    jf.add(scei);
     
    // Action Listener    
    plr.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                   try{
                    System.out.print("Welcome to Tyler Rock, Paper, Scissors =)");
                    plr.setVisible(false);
                    rock.setVisible(true);
                    paper.setVisible(true);
                    scei.setVisible(true);
                  System.out.print("What do you think it is?");
                   }
                   catch(Exception l){
                        System.out.println("Some error ");
     
                   }
     
        // Calling the Introduction Method.
    System.out.println("Welcome to your most advanced rock, paper scissors game yet.");
    System.out.println("");
    System.out.print(">>");                 
     
     
     for (int x=0;x<3;x++){
     
     
     
     
    // Creating booleans, Variables. ECT.
     
     
     
     
    rock.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                   try{
            System.out.println("You have pick rock.");
            prock=true;
                   }
                   catch(Exception l){
                        System.out.println("Some error ");
     
                   }
                }
    });
    paper.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                   try{
             System.out.println("YOU PICKED PAPER");
             ppaper=true;
                   }
                   catch(Exception l){
                        System.out.println("Some error ");
     
                   }
                }
    });
    scei.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                   try{
               System.out.println("YOU PICKED SCISSORS");
               pscissors=true;
                   }
                   catch(Exception l){
                        System.out.println("Some error ");
     
                   }
                }
    });
     
     
     
     
         //Creating my objects I guess.  
     
         Random rnd = new Random();
     
         // My Random Int Out of 3 choices.
         int cp = rnd.nextInt(3);
     
         //Sets boolean of the computers choice.
         if(cp>1){
             scissor=true;
         }
         if(cp>0&&cp<2){
            papr=true;
        }
         if(cp>-1&&cp<1){
             rck=true;
         }
     
     
     
     
         if (ppaper){
             if(rck){
                 System.out.println("Computer Picked Rock, you picked Rock.  You TIE!");
                 System.out.print(">>");
                 tie++;
             }
             if(papr){
                 System.out.println("Computer Picked Paper, you picked Rock.  You LOSE!");
                 System.out.print(">>");
                 lose++;
             }
             if(scissor){
                 System.out.println("Computer Picked Scissors, you picked Rock.  You WIN!");
                 System.out.print(">>");
                 wins++;
             }
         }
         if (prock){
             if(rck){
                 System.out.println("Computer Picked Rock, you picked Paper.  You WIN!");
                 System.out.print(">>");
                 wins++;
             }
             if(papr){
                 System.out.println("Computer Picked Paper, you picked Paper.  You TIE!");
                 System.out.print(">>");
                 tie++;
             }
             if(scissor){
                 System.out.println("Computer Picked Scissors, you picked Paper.  You LOSE!");
                 System.out.print(">>");
                 lose++;
             }         
     
         }     
         if (pscissors){
             if(rck){
                 System.out.println("Computer Picked Rock, you picked Scissors.  You LOSE!");
                 System.out.print(">>");
                 lose++;
             }
             if(papr){
                 System.out.println("Computer Picked Paper, you picked Scissors.  You WIN!");
                 System.out.print(">>");
                 wins++;
             }
             if(scissor){
                 System.out.println("Computer Picked Scissors, you picked Scissors.  You TIE!");
                 System.out.print(">>");
                 tie++;
             }         
     
         }     
     
     
     
        }
     
     
     
    }
    });
        System.out.print("You had "+wins+" wins and "+lose+" losses and "+tie+" ties.");    
            }}
    [/B]

    Thanks guys =)
    Last edited by Emperor_Xyn; December 20th, 2011 at 12:42 AM.

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: Help with Gui+RockPaperScissors

    No, ActionListeners are not like Scanners- your program will not halt and wait for user input. Instead, call a method that checks for a win or a loss after the user chooses an option.
    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. #3
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: Help with Gui+RockPaperScissors

    Can you elaborate more please? I know what you said, and I figured that before you posted. Any classes I can be directed at?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,129
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Help with Gui+RockPaperScissors

    The "loop" when you use GUI includes the JVM waiting for the user to input some data.
    The code presents data in the GUI and exits to the JVM. The user interacts with the GUI perhaps pressing some button.
    The code gets control in a listener, processes the user's input and exits to the JVM. The "loop" goes back to the start of this discussion with the JVM waiting for user input.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: Help with Gui+RockPaperScissors

    Why are you adding your ActionListeners in a loop? That's just going to cause the same thing to happen multiple times.
    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!

  6. #6
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: Help with Gui+RockPaperScissors

    Well I put it in a loop so the boolean is rock true can be seperate from the default value of false.

    But in all honesty I don't know, I didn't know I should try and avoid that.. But ill try taking it out of the loop.

Similar Threads

  1. RockPaperScissors
    By asdfg in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 16th, 2010, 07:23 AM