0

So I am trying to pass a variable from a button press in one class to another class, and can't quite figure it out. The button press creates a random number to simulate a dice roll, adds it to a variable which then is suppose to be passed to the board class which has the game board built on it and will then use said variable to determine which space on the board the player is on. Thank you in advance.

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.util.Random;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


public class Game extends JPanel{
private JLabel lblP1Name, lblP2Name, lblRules, lblDiceRoll;
private JTextField txtP1Name, txtP2Name;
private JButton diceRoll;
private JRadioButton rdP1, rdP2;
private int dice;
private static int countP1;
private int countP2;
private JPanel panelNorth;

private void groupButton( ) {

    ButtonGroup bg1 = new ButtonGroup( );

    bg1.add(rdP1);
    bg1.add(rdP2);

    }
public Game() throws FileNotFoundException {
setLayout (new BorderLayout());




rdP1 = new JRadioButton("Player 1");
rdP2 = new JRadioButton("Player 2");
ButtonListener listener = new ButtonListener();
Player1 player1 = new Player1(countP1);
Player2 player2 = new Player2(countP2);
Board board = new Board();
Rules rules = new Rules();
JButton diceRoll = new JButton("Roll the dice!");
panelNorth = new JPanel();
panelNorth.setLayout(new GridLayout(1,3));
lblRules = new JLabel(rules.toString());

add(panelNorth, BorderLayout.NORTH);
panelNorth.add(rdP1);
panelNorth.add(diceRoll);
panelNorth.add(rdP2);

Card card = new Card();

add(player1, BorderLayout.WEST);
add(player2, BorderLayout.EAST);
add(lblRules, BorderLayout.SOUTH);
add(board, BorderLayout.CENTER);
diceRoll.addActionListener(listener);
}
private class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent diceRoll){
        Random random = new Random();
        dice = random.nextInt(6)+1;


        if(rdP1.isSelected()){
            countP1 = countP1+dice;
            if(countP1>48){
                countP1=countP1-48;

            }
        }else if(rdP2.isSelected()){
            countP2 = countP2+dice;
            if(countP2>48){
                countP2=countP2-48;

            }
        }

    }
}


}

1 Answers1

1

It's simple; just use references.

Instance your classes and pass the reference:

Board board = new Board();
YourClass yourClass = new YourClass(board);

This way you can set Board attributes from the class YourClass. It's really easy, you could have learned how to do this just by reading basic Java books.

Dalton
  • 420
  • 4
  • 12
  • I know how to use a constructor and pass the variables, but I am unsure as to how to pass the information after it has already been instantiated. – user2974899 Sep 26 '14 at 22:23
  • Right, but do I create the getter in the main class for countP1/countP2? Then how do I call it to the other class. Whenever I try to call it like I normally would with game.methodname it isn't working. – user2974899 Sep 26 '14 at 22:35
  • Pass Game as reference to ButtonListener: ButtonListener listener = new ButtonListener(this); Then you create a constructor that receives it in ButtonListener: public ButtonListener(Game game){ this.game = game; } When you finish counting, just pass the value to the game: game.setCountP1(countP1); game.setCountP2(countP2); – Dalton Sep 26 '14 at 23:02