Results 1 to 6 of 6
  1. #1
    dc2acgsr99's Avatar
    dc2acgsr99 is offline Member
    Join Date
    Jan 2008
    Location
    Pittsburgh
    Posts
    26
    Rep Power
    0

    Exclamation I desperately need help

    I can not get this Rock, Paper, Scissor game into a GUI interface. The interface itself does not display the given buttons unless you resize the screen. And it will show the computer's choice in the Build output but I don't know how to transfer that to the GUI.. If anyone can help me complete this I will wire some cash, send a check/money order anything. I really need this done by tomorrow and am giving in to help at whatever cost it may be. Here is the code thus far...

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    
    public class kthLab4 extends JFrame 
    {
    
    	private JButton one, two, three;
    	private JTextArea results;
    
    	int userChoice;
    	int compChoice;
    
    	public kthLab4() 
    	{
    		super("GAME");
    		
    		
    		setSize(350,550);
    		setVisible(true);
    		
    		Random generator = new Random();
    		
    		one = new JButton("Rock");
    		two = new JButton("Paper");
    		three = new JButton("Scissors");
    		results = new JTextArea(6,20);
    
    		Container content = getContentPane();
    		content.setLayout (new BorderLayout());
    		JPanel panel = new JPanel(new FlowLayout());
    		panel.add(one);
    		panel.add(two);
    		panel.add(three);
    		content.add(panel, BorderLayout.NORTH);
    		content.add(results, BorderLayout.CENTER);
    
    one.setActionCommand("rock");
    two.setActionCommand("paper");
    three.setActionCommand("scissors");
    
    
    
    		one.addActionListener
    		(
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					userChoice = 1;
    					System.out.println("Rock");
    					compChoice = pickNum();
    					System.out.println(compChoice);
    				}
    			}
    		);
    		
    		two.addActionListener
    		(
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					userChoice = 2;
    					System.out.println("Paper");
    					compChoice = pickNum();
    					System.out.println(compChoice);
    				}
    			}
    		);
    		
    		three.addActionListener
    		(
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					userChoice = 3;
    					System.out.println("Scissors");
    					compChoice = pickNum();
    					System.out.println(compChoice);
    				}
    			}
    		);
    
    }
    	
    	public int pickNum()
    	{
    		int value=(int)(Math.random() * 3)+1;
    		return value;
    	}
    		
    	public static void main(String args[])
    	{
    		kthLab4 application = new kthLab4();
    		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    
    public void getResults(int uc, int cc, String m)
    {
    	if(uc == cc)
    	{
    		m = "tie";
    	}
    	
    	
    }
    
    }
    Hey I'm still learning... Thats my story and I'm sticking to it :)

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    import javax.swing.*;
    
    public class KL4 extends JFrame 
    {
    //    private JButton one, two, three;
        private JTextArea results;
        Random generator = new Random();
    
        int userChoice;
        int compChoice;
    
        public KL4() 
        {
            super("GAME");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            setSize(350,550);
    //        setVisible(true);
    
    //        Random generator = new Random();
    
            JButton one = new JButton("Rock");
            JButton two = new JButton("Paper");
            JButton three = new JButton("Scissors");
            results = new JTextArea(6,20);
    
            Container content = getContentPane();
            content.setLayout (new BorderLayout());
            JPanel panel = new JPanel(new FlowLayout());
            panel.add(one);
            panel.add(two);
            panel.add(three);
            content.add(panel, BorderLayout.NORTH);
            content.add(new JScrollPane(results), BorderLayout.CENTER);
    
            one.setActionCommand("rock");
            two.setActionCommand("paper");
            three.setActionCommand("scissors");
    
            one.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    userChoice = 1;
                    results.append("Rock\n");
                    compChoice = pickNum();
                    results.append(getString(compChoice) + "\n");
                    results.append("results = " +
                                    getResults(userChoice, compChoice) +
                                   "\n--------\n");
                }
            });
    
            two.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    userChoice = 2;
                    results.append("Paper\n");
                    compChoice = pickNum();
                    results.append(getString(compChoice) + "\n");
                    results.append("results = " +
                                    getResults(userChoice, compChoice) +
                                   "\n--------\n");
                }
            });
    
            three.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    userChoice = 3;
                    results.append("Scissors\n");
                    compChoice = pickNum();
                    results.append(getString(compChoice) + "\n");
                    results.append("results = " +
                                    getResults(userChoice, compChoice) +
                                   "\n--------\n");
                }
            });
    
            // The interface itself does not display the given
            // buttons unless you resize the screen.
            // If you add components after the top-level container is
            // realized it has no idea that you changed things. You
            // must tell it to do a new layout.
            // To avoid this always make this the last call:
            setVisible(true);
        }
    
        public int pickNum()
        {
            return 1 + generator.nextInt(3);
        }
    
        private String getString(int n)
        {
            switch(n)
            {
                case 1: return "Rock";
                case 2: return "Paper";
                case 3: return "Scissors";
                default: return "";
            }
        }
    
        public static void main(String[] args)
        {
            new KL4();
        }
    
        public String getResults(int player, int computer)
        {
            String results;
            if(player == 1)
            {
                if(computer == 2)
                    results = "computer wins";
                else if(computer == 3)
                    results = "player wins";
                else
                    results = "tie";
            }
            else if(player == 2)
            {
                if(computer == 1)
                    results = "player wins";
                else if(computer == 3)
                    results = "computer wins";
                else
                    results = "tie";
            }
            else
            {                   // player == 3
                if(computer == 1)
                    results = "computer wins";
                else if(computer == 2)
                    results = "player wins";
                else
                    results = "tie";
            }
            return results;
        }
    }

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    What's the reason for that hardwired? Still I've not tested those codes. Can you let me know the reason. :)

  4. #4
    dc2acgsr99's Avatar
    dc2acgsr99 is offline Member
    Join Date
    Jan 2008
    Location
    Pittsburgh
    Posts
    26
    Rep Power
    0

    Default

    Oh my god.. I can not believe this really works. This is the highlight of my night. Now if its not out of line for forum rules, do you want anything for your time? You have no idea how much I appreciate this. I'm going to stop babbling and just I don't know, THANK YOU.. If you need anything for that let me know and as long as it is in reason I will do it without question.
    Hey I'm still learning... Thats my story and I'm sticking to it :)

  5. #5
    dc2acgsr99's Avatar
    dc2acgsr99 is offline Member
    Join Date
    Jan 2008
    Location
    Pittsburgh
    Posts
    26
    Rep Power
    0

    Default

    Comparing the two pieces of code I see I was on the right track, well almost. But I was hitting a brick wall with every other attempt I was trying. I would post the other versions but they wont even compile and their ugly looking. This last one was the closest I have gotten. You really have no idea how much I appreciate this, and once again if you need anything for your time let me know.
    Hey I'm still learning... Thats my story and I'm sticking to it :)

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: I desperately need help

    I'm closing this old tread.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Posting Permissions

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