0

Really appreciated the help on the last question, still have a couple of errors however. I am making a treasure Hunt game where the user clicks on a gui to try and reveal the location of a treasure chest. I was using an action listener to display a image of a treasure chest on the button if the location was found but that was a fixed position and I wanted to randomise this. Got some advice to use an array on buttons and random number generator then use an if/else to check. Having complier errors which I will comment on the code below. A good coder will probably pick my novice errors up in a matter of seconds!

    import java.awt.*;
    import javax.swing.*;
    import java.util.Random;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;



  public class Test extends JFrame {


     JLabel label1, label2, label3;    

    ImageIcon image1, image2, image3, image4, image5;


 JTextField textResult;    

  public static void main(String[] args) {



  new Test();

    }


   public Test (){

  this.setSize(700,700);
  this.setLocationRelativeTo(null);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setTitle("Treasure Hunt Game");

  JPanel thePanel = new JPanel();


  thePanel.setLayout(new GridLayout(0,3,0,0));

  image1 = new ImageIcon(getClass().getResource("Treasure.jpg"));
  image2 = new ImageIcon(getClass().getResource("Pirate.jpg"));
  image3 = new ImageIcon(getClass().getResource("sand2.jpg"));
  image4 = new ImageIcon(getClass().getResource("emptyhole.jpg"));   
  image5 = new ImageIcon(getClass().getResource("map.jpg"));

  label1 = new JLabel("Click the buttons to find the Treasure!");
  label2 = new JLabel(image5); 
  label3 = new JLabel(image2);


  JButton [] buttons = new JButton[9]; 
  buttons[0] = new JButton(image3);
  buttons[1] = new JButton(image3);
  buttons[2] = new JButton(image3);
  buttons[3] = new JButton(image3);
  buttons[4] = new JButton(image3);
  buttons[5] = new JButton(image3);
  buttons[6] = new JButton(image3);
  buttons[7] = new JButton(image3);
  buttons[8] = new JButton(image3);


  thePanel.add(buttons[0]);
  thePanel.add(buttons[1]);
  thePanel.add(buttons[2]);
  thePanel.add(buttons[3]);
  thePanel.add(buttons[4]);
  thePanel.add(buttons[5]);
  thePanel.add(buttons[6]);
  thePanel.add(buttons[7]);
  thePanel.add(buttons[8]);
  thePanel.add(label1); 
  thePanel.add(label2);
  thePanel.add(label3);



  this.add(thePanel);

  this.setVisible(true);


  int treasureLocation = new Random().nextInt(buttons.length);


  System.out.println(treasureLocation);

On the complier, it gives me a error message saying it does not know the "buttons" or "treasureLocation" in the if else statement below.

        }
       public void actionPerformed(ActionEvent evt) {
      if (evt.getSource() == buttons[treasureLocation]) {
      }

  else {

  }

  } 

}
2
  • Does it print the statement? Commented Dec 8, 2013 at 12:15
  • Please show more code, at least the location of the actionPerformed Commented Dec 8, 2013 at 12:26

4 Answers 4

1

Is it possible you have a different Listener class?

public class Test extends JFrame {

    public Test(){
        Button[] buttons;
        int treasureLocation;
    }

    private class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            if (evt.getSource() == buttons[treasureLocation]) {
        }
    }
}

This would cause an error as buttons and treasureLocation are not within the scope. If this is not the case, it still seems like a scope issue to me. Try declaring the variables as class members

public class Test extends JFrame {
    Button[] buttons;
    int treasureLocation;

    public Test(){

    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much for the help. It seems to of sorted out the problem. I have encounter a new problem which you may be of assistance? I've got working so it randomly generates the picture of chest on the button when clicked through the if statement, which is great. However, any ideas how I could change the picture on the other buttons so when they are clicked they change to an empty hole? I thought an if else statement but not sure how to do this. I'll make an new question with the code on it.
0
JLabel label1, label2, label3;    
ImageIcon image1, image2, image3, image4, image5;
JTextField textResult;
JButton [] buttons; // Move the declaration here
int treasureLocation; // Move the declaration here

and change

JButton [] buttons = new JButton[9]; 

to

buttons = new JButton[9]; 

and

int treasureLocation = new Random().nextInt(buttons.length);

to

treasureLocation = new Random().nextInt(buttons.length);

1 Comment

Thanks very much for the help Ajai. It seems to of sorted out the problem. I have encounter a new problem which you may be of assistance? I've got working so it randomly generates the picture of chest on the button when clicked through the if statement, which is great. However, any ideas how I could change the picture on the other buttons so when they are clicked they change to an empty hole? I thought an if else statement but not sure how to do this. I'll make an new question with the code on it.
0

You actually defined buttons and treasureLocation in your constructor, which is actually not recommended. Their lifecycle and access is therefore limited to this method. Define them in your class and initialize them.

2 Comments

Thanks very much for the help Ben. It seems to of sorted out the problem. I have encounter a new problem which you may be of assistance? I've got working so it randomly generates the picture of chest on the button when clicked through the if statement, which is great. However, any ideas how I could change the picture on the other buttons so when they are clicked they change to an empty hole? I thought an if else statement but not sure how to do this. I'll make an new question with the code on it.
Yes, create a new question for that. If I find the time I will look it up!
0

You need to have buttons and treasureLocation defined as attributes. So don't define them in a method or a constructor, have them defined in the line after

ImageIcon image1, image2, image3, image4, image5;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.