Author Topic: [Java] FlowLayout issues  (Read 11241 times)

0 Members and 1 Guest are viewing this topic.

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
[Java] FlowLayout issues
« 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);
}
}
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: [Java] FlowLayout issues
« Reply #1 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?


Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [Java] FlowLayout issues
« Reply #2 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();?
« Last Edit: May 06, 2011, 06:09:59 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: [Java] FlowLayout issues
« Reply #3 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


Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [Java] FlowLayout issues
« Reply #4 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.
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: [Java] FlowLayout issues
« Reply #5 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


Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [Java] FlowLayout issues
« Reply #6 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.
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: [Java] FlowLayout issues
« Reply #7 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()
There's something about Tuesday...


Pushpins 'n' stuff...


Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [Java] FlowLayout issues
« Reply #8 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
« Last Edit: May 06, 2011, 09:07:02 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: [Java] FlowLayout issues
« Reply #9 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");
« Last Edit: May 06, 2011, 09:13:31 pm by ZippyDee »
There's something about Tuesday...


Pushpins 'n' stuff...


Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [Java] FlowLayout issues
« Reply #10 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);
}
}
}
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: [Java] FlowLayout issues
« Reply #11 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)
There's something about Tuesday...


Pushpins 'n' stuff...


Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [Java] FlowLayout issues
« Reply #12 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.
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: [Java] FlowLayout issues
« Reply #13 on: May 06, 2011, 09:36:30 pm »
Notice that I had my "waitForID" methods inside a try-catch statement.
There's something about Tuesday...


Pushpins 'n' stuff...


Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [Java] FlowLayout issues
« Reply #14 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
Loved this place, still the best producers of power metal, and sparked my dreams of coding.