0 Members and 1 Guest are viewing this topic.
/* *Created by <me>. *Copy right © 2011 *Please do not redistribute, modify, or obliterate without direct explicit permission from author. *Printed at A.D. year 2011 at 5th month and 3rd day of said month at militairy time of 15:10 GMT -5 Daylight savings time ON! */import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.Random;public class rockpaperscissors extends JFrame implements ActionListener { Random rpc = new Random(); int randint = rpc.nextInt(3)+1; JButton draw = new JButton("Draw!"); JButton rock = new JButton("Rock"); JButton paper = new JButton("Paper"); JButton scissors = new JButton("Scissors"); JLabel youchoose = new JLabel("You choose:"); JLabel compchoose = new JLabel("The computer chooses:"); JTextArea yourchoice = new JTextArea(3,10); JTextArea compchoice = new JTextArea(3,10); JLabel rockpic, paperpic, scissorspic; Container contentArea; public void init() { contentArea = getContentPane(); contentArea.setBackground(Color.cyan); FlowLayout manager = new FlowLayout(); contentArea.setLayout(manager); draw.addActionListener(this); rock.addActionListener(this); paper.addActionListener(this); scissors.addActionListener(this); draw.setEnabled(true); rock.setEnabled(true); paper.setEnabled(true); scissors.setEnabled(true); contentArea.add(youchoose); contentArea.add(draw); setContentPane(contentArea); } public void actionPerformed(ActionEvent event) { if(event.getSource()==rock) { // yourchoice = "rock!"; } } public static void main(String[]args){ rockpaperscissors GUI = new rockpaperscissors(); GUI.setTitle("Rock Paper Scissors!"); GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GUI.pack(); GUI.setVisible(true);}}
public static void main(String[]args){ rockpaperscissors GUI = new rockpaperscissors(); init(); //this is all i added GUI.setTitle("Rock Paper Scissors!"); GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GUI.pack(); GUI.setVisible(true);}
Image rockpic = getImage(getCodeBase(),"rock.jpg"); ImageIcon pica = new ImageIcon(rockpic); Image paperpic = getImage(getCodeBase(),"paper.jpg"); ImageIcon picb = new ImageIcon(paperpic); Image scissorpic = getImage(getCodeBase(),"scissor.jpg"); ImageIcon picc = new ImageIcon(scissorpic);
Toolkit toolkit = Toolkit.getDefaultToolkit(); MediaTracker tracker = new MediaTracker(this); Image image1 = toolkit.createImage("image_1.jpg"); Image image2 = toolkit.createImage("image_2.jpg"); Image image3 = toolkit.createImage("image_3.jpg"); tracker.addImage(image1, 0); tracker.addImage(image2, 1); tracker.addImage(image3, 2); try { tracker.waitForAll(); } catch(InterruptedException ie) { System.out.println("Error: " + ie.getMessage()); }
ohh..dear. YIKES! What is the equivalent of this in JFrame then?edit: also, you know how there is <constructor>.add();? is there a .delete(); as well? Like if I am tying to add a label that says 'you won' and if the person tries to play the game that im making again, how do I delete that 'you won' label? Another thing, how can I replace text instead of .append("text"); because that is just basically the same thing as concatenation. I need to clear the text field basically and put in it something else. Thanks
public void actionPerformed(ActionEvent event) { int randint = rpc.nextInt(3)+1; //1 = rock, 2 = paper, 3 = scissors //rock beats scissors, scissors beats paper, paper beats rock Toolkit toolkit = Toolkit.getDefaultToolkit(); MediaTracker tracker = new MediaTracker(this); Image rockpic = toolkit.createImage("rock.jpg"); Image paperpic = toolkit.createImage("paper.jpg"); Image scissorspic = toolkit.createImage("scissors.jpg"); if(event.getSource()==rock) { yourchoice.append("Rock!"); tracker.addImage(rockpic, 0); if(randint==1) { compchoice.append("rock"); contentArea.add(tryagain); } if(randint==2) { compchoice.append("paper"); contentArea.add(lose); } if(randint==3) { compchoice.append("scissors"); contentArea.add(win); } } if(event.getSource()==paper) { tracker.addImage(paperpic, 1); yourchoice.append("Paper!"); if(randint==1) { compchoice.append("rock"); contentArea.add(win); } if(randint==2) { compchoice.append("paper"); contentArea.add(tryagain); } if(randint==3) { compchoice.append("scissors"); contentArea.add(lose); } } if(event.getSource()==scissors) { tracker.addImage(scissorspic, 2); yourchoice.append("Scissors!"); if(randint==1) { compchoice.append("rock"); contentArea.add(lose); } if(randint==2) { compchoice.append("paper"); contentArea.add(win); } if(randint==3) { compchoice.append("scissors"); contentArea.add(tryagain); } } }
public void actionPerformed(ActionEvent event) { int randint = rpc.nextInt(3)+1; //1 = rock, 2 = paper, 3 = scissors //rock beats scissors, scissors beats paper, paper beats rock Toolkit toolkit = Toolkit.getDefaultToolkit(); MediaTracker tracker = new MediaTracker(this); Image rockpic = toolkit.createImage("rock.jpg"); Image paperpic = toolkit.createImage("paper.jpg"); Image scissorspic = toolkit.createImage("scissors.jpg"); if(event.getSource()==rock) { //if you push rock... yourchoice.setText("Rock!"); tracker.addImage(rockpic, 1); tracker.waitForID(1); if(randint==1) { contentArea.remove(win); contentArea.remove(lose); contentArea.remove(tryagain); compchoice.setText("rock"); contentArea.add(tryagain); } if(randint==2) { contentArea.remove(win); contentArea.remove(lose); contentArea.remove(tryagain); compchoice.setText("paper"); contentArea.add(lose); } if(randint==3) { contentArea.remove(win); contentArea.remove(lose); contentArea.remove(tryagain); compchoice.setText("scissors"); contentArea.add(win); } } if(event.getSource()==paper) { //if you push paper... tracker.addImage(paperpic, 2); tracker.waitForID(2); yourchoice.setText("Paper!"); if(randint==1) { contentArea.remove(win); contentArea.remove(lose); contentArea.remove(tryagain); compchoice.setText("rock"); contentArea.add(win); } if(randint==2) { contentArea.remove(win); contentArea.remove(lose); contentArea.remove(tryagain); compchoice.setText("paper"); contentArea.add(tryagain); } if(randint==3) { contentArea.remove(win); contentArea.remove(lose); contentArea.remove(tryagain); compchoice.setText("scissors"); contentArea.add(lose); } } if(event.getSource()==scissors) { //if you push scissors... tracker.addImage(scissorspic, 3); tracker.waitForID(3); yourchoice.setText("Scissors!"); if(randint==1) { contentArea.remove(win); contentArea.remove(lose); contentArea.remove(tryagain); compchoice.setText("rock"); contentArea.add(lose); } if(randint==2) { contentArea.remove(win); contentArea.remove(lose); contentArea.remove(tryagain); compchoice.setText("paper"); contentArea.add(win); } if(randint==3) { contentArea.remove(win); contentArea.remove(lose); contentArea.remove(tryagain); compchoice.setText("scissors"); contentArea.add(tryagain); } } }
--------------------Configuration: <Default>--------------------E:\adv. comp sci\rpc\rockpaperscissors.java:67: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown tracker.waitForID(1); ^E:\adv. comp sci\rpc\rockpaperscissors.java:92: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown tracker.waitForID(2); ^E:\adv. comp sci\rpc\rockpaperscissors.java:118: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown tracker.waitForID(3); ^3 errorsProcess completed.