Hi

I'm new to java, and I'm trying to make a very basic game.
The problem is that i have 3 classes that cooperates and I can't get one of the values from class "Board.java" to "gp1.java".

In Board i have a button that "rolls the dice" x2 and adds the values.
In gp1 i make a picture move around (game marker or whatever it's called), but I want it to move depending on the dice.


Some of the code of "Board":

public class Board extends JPanel implements ActionListener {

        private int dice;

        JButton button;
        TextField text = new TextField(26);
        private String str;


        public int no1, no2;


        public Board(){

        ...

        button = new JButton("Roll the dice");
        button.addActionListener(new MyAction());
        add(button);

        ...
}

        public class MyAction implements ActionListener {
        public void actionPerformed(ActionEvent ae) {
        str = ae.getActionCommand();

        if(str.equals("Roll the dice")) {

        no1 = (int)(Math.random()*6) + 1;
        no2 = (int)(Math.random()*6) + 1;
        dice = no1 + no2;

        text.setText("You rolled " + no1 + " and " + no2 + " which equals: " + dice);
        text.disable();
}
}
}
        public int getDice() {
        return dice;
}

}

I get the textfield to work, and it shows the dice numbers


Some of the code of gp1:

public class gp1 {

    public int dajs;

        ...

        public void dajsen() {
        Board dajse = new Board();
        dajs = dajse.getDice();
}

        ...

}

I just want "dajs" in gp1 to contain the "dice" value from Board

I know that I'm doing it totally wrong, but i need help :/

Since you're assigning dajs=dajse.getDice() in the constructor, it'll have the value that dajse.dice contained whenever you instantiated the gp1 class.
That is, whenever you have a line like gp1 foo = new gp1(); it'll get the value of dice at that moment. Has the user rolled the dice at that point? If not, it'll be zero.

You want gp1 to respond to the player's roll of the dice, no? So maybe the dajs=dajse.getDice() should be in a method that's triggered by your MyAction.

Since you're assigning dajs=dajse.getDice() in the constructor, it'll have the value that dajse.dice contained whenever you instantiated the gp1 class.
That is, whenever you have a line like gp1 foo = new gp1(); it'll get the value of dice at that moment. Has the user rolled the dice at that point? If not, it'll be zero.

You want gp1 to respond to the player's roll of the dice, no? So maybe the dajs=dajse.getDice() should be in a method that's triggered by your MyAction.

At the moment, the dajs value is 0. (I made the picture move to the right if dajs==0, and it did).

Yes, I want gp1 to respond to the roll of dice, but have no idea of how to make a method that's only triggered by MyAction, and not automatically at the beginning. :/

Try making a method in gp1 that does what you want (that is, calls getDice()), and call it from the actionPerformed() method.
Alternatively, you could make a method which allows another class to set gp1.dajs, and call that after you get a value for dice.

One way, your actionPerformed rolls the dice, and tells your gp1 object to call board.getDice(), the other way your actionPerformed rolls the dice and then tells gp1 to set dajs to whatever value has wound up in dice. The result should be the same, which you choose depends on which makes more sense to you.

(Style note: It would be nice if your classes were named in a more descriptive fashion - that way I'd know more about what you're modelling. In future, maybe you'd like to use a name like "GamePlayer" for the class name, if that's what it stands for.)

Try making a method in gp1 that does what you want (that is, calls getDice()), and call it from the actionPerformed() method.
Alternatively, you could make a method which allows another class to set gp1.dajs, and call that after you get a value for dice.

One way, your actionPerformed rolls the dice, and tells your gp1 object to call board.getDice(), the other way your actionPerformed rolls the dice and then tells gp1 to set dajs to whatever value has wound up in dice. The result should be the same, which you choose depends on which makes more sense to you.

(Style note: It would be nice if your classes were named in a more descriptive fashion - that way I'd know more about what you're modelling. In future, maybe you'd like to use a name like "GamePlayer" for the class name, if that's what it stands for.)

Ok, the second one sound easiest, but I have failed at both x/ looks like I just fail at java totally :/

looks like I just fail at java totally

Read my sig.

Okay, so you want to make a setter for "dajs" in "gp1", and then you want to call that from MayAction.actionPerformed.

First things first: what does the setter look like?

Read my sig.

Okay, so you want to make a setter for "dajs" in "gp1", and then you want to call that from MayAction.actionPerformed.

First things first: what does the setter look like?

Isn't it just dajs = dajse.getDice();?
Since

public void dajsen() {
    Board dajse = new Board();
    dajs = dajse.getDice();
    }

The thing is that it runs before the rolling of hte dice.

You want a method that isn't the constructor - something that your actionPerformed() method can call.

So it's a method, it's got to be public, it'll have a return type of "void" (since it's returning a value), and it'll take a single paramter, which will be an int.
Typical name of such a method would be "setDajs".

In the body of the method, you'll set dajs equal to the parameter. It's literally a one-liner.

You want a method that isn't the constructor - something that your actionPerformed() method can call.

So it's a method, it's got to be public, it'll have a return type of "void" (since it's returning a value), and it'll take a single paramter, which will be an int.
Typical name of such a method would be "setDajs".

In the body of the method, you'll set dajs equal to the parameter. It's literally a one-liner.

Ah, so if I got it right, it'll look like

public void setDajs(int valOfDajs) {
    dajs = valOfDajs;
}

Bingo.
Style-wise, and this is just a side note, some people like to use distinct names as you've done for the incoming parameter and the variable being set.
Others prefer to use the same name, and it would look like this:

public void setDajs(int dajs) {
    this.dajs = valOfDajs;
}
this.dajs

means "the dajs that is a field of the class in which this method appears" -this is neeed to get around the shadowing caused by having a formal parameter with the same name. Don't worry if this doesn't make sense - I mention it because you'll sometimes see this form, and it's good to be aware of it. It means exactly the same thing as the form you wrote.

Now, how are you going to modify actionPerformed()?

Bingo.
Style-wise, and this is just a side note, some people like to use distinct names as you've done for the incoming parameter and the variable being set.
Others prefer to use the same name, and it would look like this:

public void setDajs(int dajs) {
    this.dajs = valOfDajs;
}
this.dajs

means "the dajs that is a field of the class in which this method appears" -this is neeed to get around the shadowing caused by having a formal parameter with the same name. Don't worry if this doesn't make sense - I mention it because you'll sometimes see this form, and it's good to be aware of it. It means exactly the same thing as the form you wrote.

Now, how are you going to modify actionPerformed()?

Well... I assume that I in some way need to call "valOfDajs" or, the parameter.
(btw modify "Board"? the "actionPerformed(ActionEvent ae)")

My bad. Yes, you're going to modify MyAction.actionPerformed. You want to roll the dice, then you want that "dajs" variable to get that value. You just wrote the method to do this.

My bad. Yes, you're going to modify MyAction.actionPerformed. You want to roll the dice, then you want that "dajs" variable to get that value. You just wrote the method to do this.

Is this right in gp1:

public void setDajs(int dajs) {
    this.dajs = dajs;
    }

I don't really know.. Hum..
In MyAction.actionPerformed after the dice

gp1 gp1Dice = new gp1();
        gp1Dice.setDajs(dice);

Your setDajs(int dajs) is correct - it can be written either way. The way you wrote it the first time is also correct.

In actionPerformed, I don't know whether you want to instantiate a new gpl (or is it gp1? letter 'l' or numeral '1'?) each time someone rolls the dice, because I don't realy know what that class is. If it's supposed to keep some state of play, then you want to refer to an existing gp1 object.

So what else does that class do?

Your setDajs(int dajs) is correct - it can be written either way. The way you wrote it the first time is also correct.

In actionPerformed, I don't know whether you want to instantiate a new gpl (or is it gp1? letter 'l' or numeral '1'?) each time someone rolls the dice, because I don't realy know what that class is. If it's supposed to keep some state of play, then you want to refer to an existing gp1 object.

So what else does that class do?

It's gp1 gp(one) - numeral (As you said, GamePlayer1) :)

Each time the dice is rolled, dajs should get a new value, (the value of dice).
((Later) I'll store that value in another int (let's call it "position") in gp1.java, then add position with dajs, which will replace the current position with (position+dajs), so position=position+dajs.)

I've written everything MyAction does.
But Board also draws the moving picture, the background and adds the button

Okay!

I have this in gp1

public void setDajs(int dice) {
    this.dajs = dice;
    JOptionPane.showMessageDialog(null, dajs);
    }

And this in Board

public class MyAction implements ActionListener {
        public void actionPerformed(ActionEvent ae) {
        str = ae.getActionCommand();

        if(str.equals("Roll the dice")) {

        gp1 gp1Dice = new gp1();


        no1 = (int)(Math.random()*6) + 1;
        no2 = (int)(Math.random()*6) + 1;
        dice = no1 + no2;

        text.setText("You rolled " + no1 + " and " + no2 + " which equals: " + dice);
        text.disable();

        gp1Dice.setDajs(dice);
}
}
}

So now I have a message that tells me the right number! That must mean that dajs in gp1 has the dice value in Board?

That's what that means. So you're in business. Good job!

That's what that means. So you're in business. Good job!

Thank you so much!
I still have a problem though, but I think I can handle it now ;-)
Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.