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: I need help making a RockPaperScissors GUI please!

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need help making a RockPaperScissors GUI please!

    I need help with this code!

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
     
    public class RockPaperScissorsPanel extends JPanel
    {
       private JLabel quote;
       private JRadioButton rock, paper, scissors;
       private String rockQuote, paperQuote, scissorsQuote;
       private int userIn = -1, compIn = -1, wins=0, ties=0, losses=0, again=100;
     
       public RockPaperScissorsPanel()
       {
          rockQuote = "Rock";
          paperQuote = "Paper";
          scissorsQuote = "Scissors";
     
          quote = new JLabel("Choice:");
          quote.setFont(new Font("Helvetica", Font.BOLD, 24));
          rock = new JRadioButton("Rock");
          rock.setBackground(Color.white);
          paper = new JRadioButton("Paper");
          paper.setBackground(Color.white);
          scissors = new JRadioButton("Scissors");
          scissors.setBackground(Color.white);
     
          ButtonGroup group = new ButtonGroup();
          group.add(rock);
          group.add(paper);
          group.add(scissors);
     
          RockPaperScissors listener = new RockPaperScissors();
          rock.addActionListener(listener);
          ppaper.addActionListener(listener);
          scissors.addActionListener(listener);
     
          add(quote);
          add(rock);
          add(paper);
          add(scissors);
     
          setBackground(Color.white);
          setPreferredSize(new Dimension(320, 600));
       }
     
       private class RockPaperScissorsListener implements ActionListener
       {
          public void actionPerformed(ActionEvent event)
          {
             Object source = event.getSource();
     
             if (source == rock) 
             {
                quote.setText(rockQuote);
                userIn = 1;
             }
             if (source == paper)  
             {
                quote.setText(paperQuote);
                userIn = 2;
             }
             if (source == scissors)  
             {
                quote.setText(scissorsQuote);
                userIn = 3;      
             }
          }
     
         Random generator = new Random();
     
         compIn = generator.nextInt(3) + 1;
     
         switch (compIn)
         {
           case 1: compIn = rockQuote;
    	 		      break;
    	    case 2: compIn = paperQuote;	 		   
    	            break;
    	    case 3: compIn = scissorsQuote;	 			
    				   break;
         }
     
         System.out.println("Your choice: " +  userIn + " Computer choice: " + compIn);
     
         if(userIn == compIn)
    	  {
    	     System.out.println("Tie");
    		  ties++;
    	  }	
         else
    	  {
    	     if((userIn ==1 && compIn ==3)||(userIn == 2 && compIn == 1)||(userIn==3 && compIn== 2))
    	     {
    		     System.out.println("You win");
    			  wins++;
    		  }
    		  else if((userIn==1 && compIn ==2) || (userIn ==2 && compIn == 3) || (userIn ==3 && compIn ==1))
    		  {
    		     System.out.println("You lose");
    			  losses++;
    		  }	
    	   }	
     
     
        }
       }
    }

    If anybody could lend some assistance, that would be great!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I need help making a RockPaperScissors GUI please!

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    What help do you need? Ask specific questions, post error messages you want help with, etc.

Similar Threads

  1. Making my own GUI tookit. Looking for some advice.
    By Cornix in forum Java Theory & Questions
    Replies: 5
    Last Post: January 23rd, 2014, 02:06 PM
  2. Help with Gui+RockPaperScissors
    By Emperor_Xyn in forum Loops & Control Statements
    Replies: 5
    Last Post: December 23rd, 2011, 03:53 PM
  3. RockPaperScissors
    By asdfg in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 16th, 2010, 07:23 AM