0 Members and 3 Guests are viewing this topic.
/* *Created by Jacob Patrick. *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(); 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:"); JLabel lose = new JLabel("You lose!"); JLabel tryagain = new JLabel("Try again!"); JLabel win = new JLabel("You Won!"); JTextArea yourchoice = new JTextArea(1,10); JTextArea compchoice = new JTextArea(1,10); JLabel rockpiclbl, paperpiclbl, scissorspiclbl; Container contentArea; public rockpaperscissors() { contentArea = getContentPane(); contentArea.setBackground(Color.cyan); FlowLayout manager = new FlowLayout(FlowLayout.CENTER,50,50); contentArea.setLayout(manager); rock.addActionListener(this); paper.addActionListener(this); scissors.addActionListener(this); rock.setEnabled(true); paper.setEnabled(true); scissors.setEnabled(true); contentArea.add(rock); contentArea.add(paper); contentArea.add(scissors); contentArea.add(youchoose); contentArea.add(yourchoice); contentArea.add(compchoose); contentArea.add(compchoice); setContentPane(contentArea); } 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 try { 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); } } } catch(InterruptedException ie) { System.out.println("Error: " + ie.getMessage()); } } public static void main(String[]args){ rockpaperscissors GUI = new rockpaperscissors(); GUI.setTitle("Rock Paper Scissors!"); GUI.setSize(1000,1000); GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GUI.pack(); GUI.setVisible(true); //Its not invisible!}}
import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.Random;public class rockpaperscissors extends JFrame implements ActionListener { String[] messages = {"You win!", "You lose!", "Try again!"}; String[] TypeNames = {"Rock", "Paper", "Scissors"}; //didn't have a better name //rock = 0, paper = 1, scissors = 2 //the first index corresponds to the one you chose, so if you chose rock, and the computer chose //paper, it would be referenced as comparisons[0][1] //it's a byte array to save a tiny bit of ram :) //the numbers in the brackets correspond to the index in the messages array byte[][] comparisons ={ {2, 1, 0}, //rock {0, 2, 1}, //paper {1, 0, 2}, //scissors }; Random rpc = new Random(); 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:"); JLabel message = new JLabel("You win!"); JTextArea yourchoice = new JTextArea(1,10); JTextArea compchoice = new JTextArea(1,10); JLabel rockpiclbl, paperpiclbl, scissorspiclbl; Container contentArea; public rockpaperscissors() { contentArea = getContentPane(); contentArea.setBackground(Color.cyan); FlowLayout manager = new FlowLayout(FlowLayout.CENTER,50,50); contentArea.setLayout(manager); rock.addActionListener(this); paper.addActionListener(this); scissors.addActionListener(this); rock.setEnabled(true); paper.setEnabled(true); scissors.setEnabled(true); contentArea.add(rock); contentArea.add(paper); contentArea.add(scissors); contentArea.add(youchoose); contentArea.add(yourchoice); contentArea.add(compchoose); contentArea.add(compchoice); contentArea.add(message); setContentPane(contentArea); } public void actionPerformed(ActionEvent event) { int randint = rpc.nextInt(3); //0 = rock, 1 = paper, 2 = scissors compchoice.setText( TypeNames[randint] ); //rock beats scissors, scissors beats paper, paper beats rock try { 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); message.setText( messages[ comparisons[0][randint] ] ); } if(event.getSource()==paper) { //if you push paper... tracker.addImage(paperpic, 2); tracker.waitForID(2); yourchoice.setText("Paper!"); message.setText( messages[ comparisons[1][randint] ] ); } if(event.getSource()==scissors) { //if you push scissors... tracker.addImage(scissorspic, 3); tracker.waitForID(3); yourchoice.setText("Scissors!"); message.setText( messages[ comparisons[2][randint] ] ); } } catch(InterruptedException ie) { System.out.println("Error: " + ie.getMessage()); } } public static void main(String[]args){ rockpaperscissors GUI = new rockpaperscissors(); GUI.setTitle("Rock Paper Scissors!"); GUI.setSize(1000,1000); GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GUI.pack(); GUI.setVisible(true); //Its not invisible!}}
Me = super-Java-optimizer(I could get rid of almost all the if statements if I wanted to by using an array of buttons, but I don't feel like it)Code: [Select]import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.Random;public class rockpaperscissors extends JFrame implements ActionListener { String[] messages = {"You win!", "You lose!", "Try again!"}; String[] TypeNames = {"Rock", "Paper", "Scissors"}; //didn't have a better name //rock = 0, paper = 1, scissors = 2 //the first index corresponds to the one you chose, so if you chose rock, and the computer chose //paper, it would be referenced as comparisons[0][1] //it's a byte array to save a tiny bit of ram :) //the numbers in the brackets correspond to the index in the messages array byte[][] comparisons ={ {2, 1, 0}, //rock {0, 2, 1}, //paper {1, 0, 2}, //scissors }; Random rpc = new Random(); 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:"); JLabel message = new JLabel("You win!"); JTextArea yourchoice = new JTextArea(1,10); JTextArea compchoice = new JTextArea(1,10); JLabel rockpiclbl, paperpiclbl, scissorspiclbl; Container contentArea; public rockpaperscissors() { contentArea = getContentPane(); contentArea.setBackground(Color.cyan); FlowLayout manager = new FlowLayout(FlowLayout.CENTER,50,50); contentArea.setLayout(manager); rock.addActionListener(this); paper.addActionListener(this); scissors.addActionListener(this); rock.setEnabled(true); paper.setEnabled(true); scissors.setEnabled(true); contentArea.add(rock); contentArea.add(paper); contentArea.add(scissors); contentArea.add(youchoose); contentArea.add(yourchoice); contentArea.add(compchoose); contentArea.add(compchoice); contentArea.add(message); setContentPane(contentArea); } public void actionPerformed(ActionEvent event) { int randint = rpc.nextInt(3); //0 = rock, 1 = paper, 2 = scissors compchoice.setText( TypeNames[randint] ); //rock beats scissors, scissors beats paper, paper beats rock try { 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); message.setText( messages[ comparisons[0][randint] ] ); } if(event.getSource()==paper) { //if you push paper... tracker.addImage(paperpic, 2); tracker.waitForID(2); yourchoice.setText("Paper!"); message.setText( messages[ comparisons[1][randint] ] ); } if(event.getSource()==scissors) { //if you push scissors... tracker.addImage(scissorspic, 3); tracker.waitForID(3); yourchoice.setText("Scissors!"); message.setText( messages[ comparisons[2][randint] ] ); } } catch(InterruptedException ie) { System.out.println("Error: " + ie.getMessage()); } } public static void main(String[]args){ rockpaperscissors GUI = new rockpaperscissors(); GUI.setTitle("Rock Paper Scissors!"); GUI.setSize(1000,1000); GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GUI.pack(); GUI.setVisible(true); //Its not invisible!}}
import java.awt.image.*;import javax.imageio.ImageIO;Image img = (Image) ImageIO.read(new File("tornado.jpg"));//looks for a jpeg image titled "tornado" in the same folder as the .class file of the class.//you can also use paths, such as "C:\\Documents and Settings\\My Documents"..etc