0

I'm making a text based RPG to help me learn Java. So I have a bunch of different classes that make swing forms right now and each of which will lead to one another. However, in one form I actually create the instance of the object called Player, where I will set the stats (strength, agility, level, etc.) based on the choices the user makes.

However, I can only access that object in that class/window and I can't call it for manipulate that object anywhere else in the application.

How do I make it globally accessible?

Edit
I should have phrased this better. Let's say I have a class called PlayerCreation and in PlayerCreation I make an object by calling the Player class Player p1 = new Player(); Now I move out of that class completely and go to another form called PlayerStats and I want to call p1.getStrength(); but since p1 was in PlayerCreation class, I can't get it.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Ben Allen
  • 23
  • 1
  • 4
  • 2
    possible duplicate of [Global variables in Java](http://stackoverflow.com/questions/4646577/global-variables-in-java) - note the various answers; dependency injection is generally a good route. – Brian Roach Mar 03 '12 at 18:43
  • @Ben Allen: have you made your class as Public? – Parth Soni Mar 03 '12 at 18:44
  • @Brian's recommendation is the way to go: use dependency injection to pass references just as you would for a non-GUI Java class. And continue to follow good object-oriented guidelines by avoiding use of static variables and the like. – Hovercraft Full Of Eels Mar 03 '12 at 18:45
  • I should have phrased this better. Let's say I have a class called PlayerCreation and in PlayerCreation I make an object by calling the Player class Player p1 = new Player(); Now I move out of that class completely and go to another form called PlayerStats and I want to call p1.getStrength(); but since p1 was in PlayerCreation class, I can't get it. – Ben Allen Mar 03 '12 at 18:52
  • @HovercraftFullOfEels do you have a better explanation of that dependency injection? I'm so confused :( – Ben Allen Mar 03 '12 at 19:45
  • I've posted an example below. – Hovercraft Full Of Eels Mar 03 '12 at 19:53

3 Answers3

2

Here's an example of what I'm talking about. I'll add some text in a bit, but if you compile and run this code, you'll see how it works:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class DependInjectEg {
   private static void createAndShowGui() {
      Player[] players = {
            new Player("John Smith", 10, 5),
            new Player("Donald Duck", 8, 3),
            new Player("Doris Day", 5, 2),
            new Player("Bill Jones", 4, 6),
            new Player("Frank Stein", 1, 1),
      };

      PlayerStats mainPanel = new PlayerStats();
      for (Player player : players) {
         mainPanel.addPlayer(player);
      }

      JFrame frame = new JFrame("Dependency Injection Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class Player {
   private String name;
   private int speed;
   private int strength;

   // default constructor
   public Player() {
   }

   public Player(String name, int speed, int strength) {
      this.name = name;
      this.speed = speed;
      this.strength = strength;
   }

   public int getStrength() {
      return strength;
   }

   public void setStrength(int strength) {
      this.strength = strength;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public int getSpeed() {
      return speed;
   }

   public void setSpeed(int speed) {
      this.speed = speed;
   }

   @Override
   public String toString() {
      StringBuffer sb = new StringBuffer();
      sb.append("Name: " + name + ", ");
      sb.append("Speed: " + speed + ", ");
      sb.append("Strength: " + strength);

      return sb.toString();
   }

}

@SuppressWarnings("serial")
class PlayerStats extends JPanel {
   private DefaultListModel playerListModel = new DefaultListModel();
   private JList playerList = new JList(playerListModel);

   public PlayerStats() {
      JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 0));
      btnPanel.add(new JButton(new NewPlayerAction()));
      btnPanel.add(new JButton(new EditPlayerAction()));
      btnPanel.add(new JButton(new ExitAction()));

      setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
      setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createTitledBorder("Player Stats"),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)));
      add(new JScrollPane(playerList));
      add(Box.createVerticalStrut(5));
      add(btnPanel);

   }

   public void addPlayer(Player player) {
      playerListModel.addElement(player);
   }

   private class NewPlayerAction extends AbstractAction {
      public NewPlayerAction() {
         super("New Player");
         putValue(MNEMONIC_KEY, KeyEvent.VK_N);
      }

      public void actionPerformed(ActionEvent evt) {
         PlayerEditorPanel editorPanel = new PlayerEditorPanel();
         int result = JOptionPane.showConfirmDialog(PlayerStats.this,
               editorPanel, "Create Player", JOptionPane.OK_CANCEL_OPTION,
               JOptionPane.PLAIN_MESSAGE);
         if (result == JOptionPane.OK_OPTION) {
            Player player = editorPanel.getNewPlayer();
            playerListModel.addElement(player);
         }
      };
   }

   private class EditPlayerAction extends AbstractAction {
      public EditPlayerAction() {
         super("Edit Player");
         putValue(MNEMONIC_KEY, KeyEvent.VK_E);
      }

      public void actionPerformed(ActionEvent evt) {
         Player player = (Player) playerList.getSelectedValue();
         if (player == null) {
            return; // do nothing if no player selected
         }
         PlayerEditorPanel editorPanel = new PlayerEditorPanel();
         editorPanel.setPlayer(player);

         int result = JOptionPane.showConfirmDialog(PlayerStats.this,
               editorPanel, "Edit Player", JOptionPane.OK_CANCEL_OPTION,
               JOptionPane.PLAIN_MESSAGE);
         if (result == JOptionPane.OK_OPTION) {
            editorPanel.upDatePlayerAttributes();
            playerList.repaint();
         }
      }
   }

   private class ExitAction extends AbstractAction {
      public ExitAction() {
         super("Exit");
         putValue(MNEMONIC_KEY, KeyEvent.VK_X);
      }

      public void actionPerformed(ActionEvent e) {
         Window win = SwingUtilities.getWindowAncestor(PlayerStats.this);
         win.dispose();
      }

   }
}

@SuppressWarnings("serial")
class PlayerEditorPanel extends JPanel {
   public static final String[] FIELD_TITLES = { "Name", "Speed", "Strength" };
   private static final Insets WEST_INSETS = new Insets(5, 0, 5, 5);
   private static final Insets EAST_INSETS = new Insets(5, 5, 5, 0);
   private JTextField nameField = new JTextField(10);
   private JTextField speedField = new JTextField(10);
   private JTextField strengthField = new JTextField(10);
   private JTextField[] fields = { nameField, speedField, strengthField };
   private Player player;

   public PlayerEditorPanel() {
      setLayout(new GridBagLayout());
      setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createTitledBorder("Player Editor"),
            BorderFactory.createEmptyBorder(5, 5, 5, 5)));
      GridBagConstraints gbc;
      for (int i = 0; i < FIELD_TITLES.length; i++) {
         gbc = createGbc(0, i);
         add(new JLabel(FIELD_TITLES[i] + ":", JLabel.LEFT), gbc);
         gbc = createGbc(1, i);
         add(fields[i], gbc);
      }
   }

   @SuppressWarnings("static-access")
   private GridBagConstraints createGbc(int x, int y) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;

      // bad coding habit using variable name to access static constants
      // done for sake of brevity and clarity
      gbc.anchor = (x == 0) ? gbc.WEST : gbc.EAST;
      gbc.fill = (x == 0) ? gbc.BOTH : gbc.HORIZONTAL;

      gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS;
      gbc.weightx = (x == 0) ? 0.1 : 1.0;
      gbc.weighty = 1.0;
      return gbc;
   }

   public void setPlayer(Player player) {
      this.player = player;
      nameField.setText(player.getName());
      speedField.setText(String.valueOf(player.getSpeed()));
      strengthField.setText(String.valueOf(player.getStrength()));
   }

   public Player getNewPlayer() {
      String name = nameField.getText();
      int strength = 0;
      try {
         strength = Integer.parseInt(strengthField.getText());
      } catch (NumberFormatException e) {
         // TODO: notify user that field was empty
      }
      int speed = 0;
      try {
         speed = Integer.parseInt(speedField.getText());
      } catch (NumberFormatException e) {
         // TODO: notify user that field was empty
      }

      Player p = new Player();
      p.setName(name);
      p.setSpeed(speed);
      p.setStrength(strength);

      return p;
   }

   public void upDatePlayerAttributes() {
      String name = nameField.getText();
      int strength = 0;
      try {
         strength = Integer.parseInt(strengthField.getText());
      } catch (NumberFormatException e) {
         // TODO: notify user that field was empty
      }
      int speed = 0;
      try {
         speed = Integer.parseInt(speedField.getText());
      } catch (NumberFormatException e) {
         // TODO: notify user that field was empty
      }

      player.setName(name);
      player.setSpeed(speed);
      player.setStrength(strength);
   }
}

The Player class is a simple class that has 3 Player attributes -- name, strength and speed.

The PlayerStats class is the main GUI class that holds a bunch players in a JList's model and has functionality to add or edit players.

The PlayerEditor is a class that displays the stats of a single player and allows creation of new players or editing of current player attributes in JTextFields. If you want to edit an existing Player, you "inject" the Player object into this class via the setPlayer(Player player) method:

public void setPlayer(Player player) {
  this.player = player;
  nameField.setText(player.getName());
  speedField.setText(String.valueOf(player.getSpeed()));
  strengthField.setText(String.valueOf(player.getStrength()));
}

This will display the Player's attributes in the JTextField and will set an internal private variable player to refer to this injected Player object. This is called in the PlayerStats class like so:

  public void actionPerformed(ActionEvent evt) {

     // *** first get the selected Player from the JList
     Player player = (Player) playerList.getSelectedValue();
     if (player == null) {
        return; // do nothing if no player selected
     }

     // then create a PlayerEditorPanel object
     PlayerEditorPanel editorPanel = new PlayerEditorPanel();

     // and then "inject" the selected Player into the editor panel.
     editorPanel.setPlayer(player);

     // and display it in a JOptionPane
     int result = JOptionPane.showConfirmDialog(PlayerStats.this,
           editorPanel, "Edit Player", JOptionPane.OK_CANCEL_OPTION,
           JOptionPane.PLAIN_MESSAGE);
     if (result == JOptionPane.OK_OPTION) {
        editorPanel.upDatePlayerAttributes();
        playerList.repaint();
     }
  }

Then later if you decide that you want to accept the changed attributes, you'd call the upDatePlayerAttributes() method which would give those attributes to the Player object held by the player variable:

public void upDatePlayerAttributes() {
  String name = nameField.getText();
  int strength = 0;
  try {
     strength = Integer.parseInt(strengthField.getText());
  } catch (NumberFormatException e) {
     // TODO: notify user that field was empty
  }
  int speed = 0;
  try {
     speed = Integer.parseInt(speedField.getText());
  } catch (NumberFormatException e) {
     // TODO: notify user that field was empty
  }

  player.setName(name);
  player.setSpeed(speed);
  player.setStrength(strength);
}

Make sense?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

I am taking a Java class right now, and I think this example from my book will be helpful to you. In the example they are taking a 'rooms' class that is publically accessable.

http://sites.google.com/site/javaremejy/java-applications/reservations-java

Erik
  • 103
  • 7
-2

Use a singleton pattern. If you give your class a field like:

private static final int agility

Or whatever variable you like, and a getter like this:

public static int getAgility() {
    return agility;
}

You can instantiate the class at startup like this:

new ClassNameYouChoose();

And you can access the getter from anywhere:

int agility = ClassNameYouChoose();

Note that you shouldn't "abuse" this; if you decide to change something later on in the development of your game, it can be very challenging to find out where you used the class.

Bartvbl
  • 2,878
  • 3
  • 28
  • 45
  • 1
    He wants to be able to make changes to a Player object that is accessible in a modification class and a game class. The singleton pattern is most definitely not how to solve this. Again, the solution is by dependency injection. -1 – Hovercraft Full Of Eels Mar 03 '12 at 18:50