Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Snake X on May 06, 2011, 05:11:05 pm

Title: [Java] FlowLayout issues
Post by: Snake X on May 06, 2011, 05:11:05 pm
I'm currently working on a rock paper scissors game (project) for my adv. comp sci class and I need help figuring as the teacher thought adding a button would fix the issue (lol). The window displays.. however though it does not show anything. Please help. You might need to run it if you don't know what I'm talking about also

(note: I as the author give direct explicit permission to modify/obliterate code :P )

Code: [Select]
/*
 *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);
}
}
Title: Re: [Java] FlowLayout issues
Post by: nemo on May 06, 2011, 06:07:25 pm
you need to call init();

by the way, have you taken the AP Computer Science exam? or are you in a different course?
Title: Re: [Java] FlowLayout issues
Post by: Snake X on May 06, 2011, 06:09:41 pm
<rant>atm im in adv. comp sci, and not planning to take AP comp sci. This is because my teacher is UNBELIEVABLY ANNOYING and she is really a dick to us and a few other friends of mine and she gets on us for EVERYTHING and she doesnt even supply adequate help at all and just helps this one kid out for pretty much the whole class.</rant>

anyhow, how do I call init();?
Title: Re: [Java] FlowLayout issues
Post by: nemo on May 06, 2011, 06:12:32 pm
Code: [Select]
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);
}

although this will work, i'd suggest making a constructor for the rockpaperscissors class and just copy/pasting the code in init() into the constructor
Title: Re: [Java] FlowLayout issues
Post by: Snake X on May 06, 2011, 06:27:13 pm
and from that one line of code I get this error:

E:\adv. comp sci\rpc\rockpaperscissors.java:49: non-static method init() cannot be referenced from a static context
      init();
      ^
1 error

Process completed.
Title: Re: [Java] FlowLayout issues
Post by: nemo on May 06, 2011, 06:28:29 pm
ah. right. i forgot about that. make a constructor for the rockpaperscissors class and that won't happen
Title: Re: [Java] FlowLayout issues
Post by: Snake X on May 06, 2011, 07:09:21 pm
ok now that I have this added:

Code: [Select]
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);
(this is under actionPerformed) it gives me these errors even though they worked in another program of mine:


--------------------Configuration: <Default>--------------------
E:\adv. comp sci\rpc\rockpaperscissors.java:46: cannot find symbol
symbol  : method getCodeBase()
location: class rockpaperscissors
      Image rockpic = getImage(getCodeBase(),"rock.jpg");
                               ^
E:\adv. comp sci\rpc\rockpaperscissors.java:49: cannot find symbol
symbol  : method getCodeBase()
location: class rockpaperscissors
      Image paperpic = getImage(getCodeBase(),"paper.jpg");
                                ^
E:\adv. comp sci\rpc\rockpaperscissors.java:52: cannot find symbol
symbol  : method getCodeBase()
location: class rockpaperscissors
      Image scissorpic = getImage(getCodeBase(),"scissor.jpg");
                                  ^
3 errors

Process completed.
Title: Re: [Java] FlowLayout issues
Post by: ZippyDee on May 06, 2011, 08:31:26 pm
getCodeBase() is a method in the Applet class. If this isn't an applet, you can't use getCodeBase()
Title: Re: [Java] FlowLayout issues
Post by: Snake X on May 06, 2011, 09:00:57 pm
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
Title: Re: [Java] FlowLayout issues
Post by: ZippyDee on May 06, 2011, 09:11:51 pm
You'll want to use a MediaTracker to load your images. Try something like this:
Code: [Select]
        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
.remove(something);
.setText("some new text here");
Title: Re: [Java] FlowLayout issues
Post by: Snake X on May 06, 2011, 09:17:41 pm
errm well it's not structured like that.. As I press a button I want the picture displayed below the button so it'd be under actionPerformed and inside if statements

not sure where to put tracker.waitForAll(); at
Code: [Select]
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);
}
}
}
Title: Re: [Java] FlowLayout issues
Post by: ZippyDee on May 06, 2011, 09:24:15 pm
after addImage() you need to do have it waitForAll, or you can just waitForID(id) where id is the second param of addImage(Image, int)
Title: Re: [Java] FlowLayout issues
Post by: Snake X on May 06, 2011, 09:35:11 pm
x.x
Code: [Select]
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);
}
}
}

errors:
Code: [Select]

--------------------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 errors

Process completed.
Title: Re: [Java] FlowLayout issues
Post by: ZippyDee on May 06, 2011, 09:36:30 pm
Notice that I had my "waitForID" methods inside a try-catch statement.
Title: Re: [Java] FlowLayout issues
Post by: Snake X on May 06, 2011, 09:37:14 pm
yeah i was wandering where i put the try { at so i just left it out since its scattered in the if statements if that meant anything
Title: Re: [Java] FlowLayout issues
Post by: ZippyDee on May 06, 2011, 09:44:25 pm
try-catch statements are crucial for anything that throws an exception, unless the current method also throws an exception...
Title: Re: [Java] FlowLayout issues
Post by: Snake X on May 06, 2011, 09:55:06 pm
hmm well the images arn't working so i dont think ill fool with those.. Ok also, when I win, it doesn't say I won. Another example: I just pressed rock and the computer chose paper. The label is supposed to display 'you lose!' but instead it displays 'try again!' which is only supposed to happen if the computer chooses the same thing as you. I will give you the whole entire code so you can compile it yourself and play with it for a few mins to see what im talking about. Also, you might need to stretch the window out to the side so you can see the win/loose text b/c setSize isn't working.

Code: [Select]
/*
 *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!
}
}
Title: Re: [Java] FlowLayout issues
Post by: Binder News on May 07, 2011, 12:23:36 am
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!
}
}
Title: Re: [Java] FlowLayout issues
Post by: Snake X on May 07, 2011, 10:05:36 am
That's bad in my case. I have to program it in syntax that I know. So basically I just have to have it laid out the way it was x_x

Sorry for not being able to use it but my teacher will look at the code and it has to be easy to read/understand and she will know that someone else did it if it looked like that. Are you able to fix the code that I gave without optimizing it?
Title: Re: [Java] FlowLayout issues
Post by: nemo on May 07, 2011, 12:12:43 pm
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!
}
}

using byte arrays is rarely worth the trouble because you have to cast bytes to ints and ints to bytes, which takes up cycles. the only place i've encountered where using a byte array is necessary would be IO with files.

also, a different way to get an image:

Code: [Select]
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


and as to your code, i'm not certain what's wrong with it, but i'm willing to bet it's a GUI problem. i'd suggest having 1 JLabel object instead of 3 for when you lose/win/tie. then just use setText() to say the correct message.
Title: Re: [Java] FlowLayout issues
Post by: Snake X on May 07, 2011, 03:52:59 pm
ooh good idea, ill try that. Thanks nemo! :)
Title: Re: [Java] FlowLayout issues
Post by: Binder News on May 07, 2011, 07:52:13 pm
Good point about the byte array thing.