Results 1 to 5 of 5
Thread: I desperately need help
- 04-30-2008, 05:17 AM #1
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 :)
- 04-30-2008, 07:25 AM #2
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; } }
- 04-30-2008, 07:43 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What's the reason for that hardwired? Still I've not tested those codes. Can you let me know the reason. :)
- 04-30-2008, 08:05 AM #4
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 :)
- 04-30-2008, 08:18 AM #5
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 :)


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks