I am having a bit of trouble with accessing an object declared in the main from a different class:
public static void main(String[]args)
{
Knight knight=new Knight();
Room2 room2=new Room2()
}
The problem is that I can't pass the object as an argument in this particular method as its an actionListener
overridden method so something like this Accessing objects of other classes
wouldn't work.
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==attack)
{
knight.getHealth();
}
}
I can't pass anything extra after the ActionEvent e
argument
so I wanted to ask is this possible for the knight
object to be recognized in this class or do I have to do something completely different?
thanks
Edit:
The main method is simply creating character objects like knight
which is a concrete class:
public class Room2 extends JFrame implements ActionListener{
private JButton attack;
private JButton defend;
//private JLabel item1read;
CardLayout cl=new CardLayout();
GridBagConstraints gb=new GridBagConstraints();
JPanel panel =new JPanel();
public Room2()
{
//******************************
battlePanel.setLayout(new GridBagLayout());
attack=new JButton("Attack");
gb.gridx=1;
gb.gridy=1;
gb.insets=new Insets(10,10,10,10);
attack.addActionListener(this);
battlePanel.add(attack,gb);
defend=new JButton("defend");
gb.gridx=1;
gb.gridy=2;
battlePanel.add(defend,gb);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==attack)
{
knight.getHealth();
}
}
}
Based on your code, you will want to pass your Knight instance into the Room instance, perhaps via its constructor:
private Knight knight;
public Room2(final Knight knight) {
this.knight = knight;
battlePanel.setLayout(new GridBagLayout());
attack=new JButton("Attack");
gb.gridx=1;
gb.gridy=1;
gb.insets=new Insets(10,10,10,10);
attack.addActionListener(this);
battlePanel.add(attack,gb);
defend=new JButton("defend");
gb.gridx=1;
gb.gridy=2;
battlePanel.add(defend,gb);
public void actionPerformed(ActionEvent e) {
if(e.getSource()==attack){
knight.getHealth();
}
}
}
And when creating it, pass it in:
Room room = new Room(knight);
e.g.,
public static void main(String[]args) {
Knight knight=new Knight();
Room2 room2=new Room2(knight);
}
Better still, if the Room2 represents a physical room, and the knight an actual knight, then I would base my model on reality -- the knight isn't always inside of a room, and so I'd base my code on this reality. Perhaps give Room2 an enter(...)
and exit(...)
method pair, allowing the the GameManager class the ability to call these methods. The Room could also have a public List<Participants> listParticipants()
or something similar type method that would list all knights, monsters or whatever that happen to be in that room. Then room behaviors could depend on its occupants.
Likewise you could give all your participants a location field as well as a setLocation(...)
method with a getter method as well, so that all participant behaviors (including that of the knight) can change depending on their location.