2

I have 5 classes for this project, Button Minefield GUI MenuBar Game.

Button is an individual mine cell.

Minefield contains a 2d array of buttons, and operations that concern all of the buttons.

GUI is the portion above the MineField grid, displaying the reset button, time lapsed and how many flags remain,

Game puts all of the other classes in Panels and places them in the applet. No game logic here.

Here is a screenshot of my applet

I'm having problems when it comes to using inheritance. If I can solve this issue, I can do the other problems too. It has to do with invoking a method in the super class.

One of my problems:

When a Button mine cell is right-clicked, the mouse listener picks it up and changes the text of the JButton to "F" for flag. However, I want to update the counter of how many flags are available, which is a variable in the Minefield class. Finally, I want the GUI class to listen for changes to that variable and update the JLabel.

In the MouseListener for Button:

 if (e.getButton() == MouseEvent.BUTTON3)
        {
           advanceCurrentState();     

           if (currentState == "FLAG")
             super.setNumFlagsRemaining(-1); //update the Minefields variable
        }

Back in Minefield:

   public void setNumFlagsRemaining(int delta)
    {
        numFlagsRemaining += delta;
    }

But this doesn't work. When I am debugging, super.setNumFlagsRemaining(-1) is creating a new instance of Minefield and I cannot update the variable inside the super class. I thought that it would update the existing object of Minefield, but I read that Java does not pass by reference, so I was confused as how to do it. If you need to see additional code please say so, I tried to not clutter this post with 5 whole classes and say, "fix it".

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • This might help: http://stackoverflow.com/questions/22348685/jbutton-with-both-actionlistener-mouselistener – Martijn Burger Feb 17 '16 at 16:57
  • 2
    Calling `super.setNumFlagsRemaining(-1)` is not creating a new Minefield instance, it is updating the current one. Any use of the word _super_ in a class is referring to the same instance, but as it's parent type. – Andrew Williamson Feb 17 '16 at 16:57
  • Do I understand it correctly that `Button` extends `Minefield`, but at the same time `Minefield` contains a 2D array of `Button`s? – radoh Feb 17 '16 at 16:58
  • 1
    Why would you do that? Tbh `Button` shouldn't extend `Minefield`. If you have whole game logic in `Minefield` then every `Button` will handle/modify its own game logic...? I don't understand this hierarchy... – radoh Feb 17 '16 at 17:04
  • Well, the way I was thinking it was : the Minefield class is used only to place the random mines at start, and to go through each Button and count the adjacent mines, storing that information inside of that Button's instance. The Button is just changing its state and has a boolean isMine – Tysia Krepps Feb 17 '16 at 17:12
  • `if (currentState == "FLAG")` Don't use == to compare Strings; it will eventually fail. See http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java . – VGR Feb 17 '16 at 18:24

1 Answers1

1

You do not provide enough code so I can only guess here. I think you are confused with key word super It actually mean to call method from the super class which you had inherited. I doubt that JButton inherits anything from your class Minefield. Usually it is in opposite way.

Also count of the fields is stored and displayed in the JLabel. So you, actually, have to call method of the class which has this JLabel and takes care of total count.

Alex
  • 4,457
  • 2
  • 20
  • 59