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

0 Members and 2 Guests are viewing this topic.

Offline ZippyDee

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +83/-8
  • Why not zoidberg?
    • View Profile
Re: [Java] FlowLayout issues
« Reply #15 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...
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 #16 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!
}
}
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline Binder News

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 785
  • Rating: +46/-3
  • Zombie of Tomorrow
    • View Profile
Re: [Java] FlowLayout issues
« Reply #17 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!
}
}
Spoiler For userbars:







Hacker-in-training!   Z80 Assembly Programmer     Axe Programmer
C++ H4X0R             Java Coder                           I <3 Python!

Perdidisti ludum     Cerebrum non habes

"We are humans first, no matter what."
"Fame is a vapor, popularity an accident, and riches take wings. Only one thing endures, and that is character."
Spoiler For Test Results:





Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [Java] FlowLayout issues
« Reply #18 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?
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 #19 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.


Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [Java] FlowLayout issues
« Reply #20 on: May 07, 2011, 03:52:59 pm »
ooh good idea, ill try that. Thanks nemo! :)
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline Binder News

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 785
  • Rating: +46/-3
  • Zombie of Tomorrow
    • View Profile
Re: [Java] FlowLayout issues
« Reply #21 on: May 07, 2011, 07:52:13 pm »
Good point about the byte array thing.
Spoiler For userbars:







Hacker-in-training!   Z80 Assembly Programmer     Axe Programmer
C++ H4X0R             Java Coder                           I <3 Python!

Perdidisti ludum     Cerebrum non habes

"We are humans first, no matter what."
"Fame is a vapor, popularity an accident, and riches take wings. Only one thing endures, and that is character."
Spoiler For Test Results: