Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-30-2008, 06:17 AM
dc2acgsr99's Avatar
Member
 
Join Date: Jan 2008
Location: Pittsburgh
Posts: 26
dc2acgsr99 is on a distinguished road
Send a message via AIM to dc2acgsr99
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...

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-30-2008, 08:25 AM
Senior Member
 
Join Date: Jul 2007
Posts: 933
hardwired is on a distinguished road
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; } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-30-2008, 08:43 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,294
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
What's the reason for that hardwired? Still I've not tested those codes. Can you let me know the reason.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-30-2008, 09:05 AM
dc2acgsr99's Avatar
Member
 
Join Date: Jan 2008
Location: Pittsburgh
Posts: 26
dc2acgsr99 is on a distinguished road
Send a message via AIM to dc2acgsr99
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-30-2008, 09:18 AM
dc2acgsr99's Avatar
Member
 
Join Date: Jan 2008
Location: Pittsburgh
Posts: 26
dc2acgsr99 is on a distinguished road
Send a message via AIM to dc2acgsr99
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 02:34 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org