vendredi 15 mars 2019

Using an if statement for a panel onClick? or a better approach?

I'm having trouble deciding what the best approach for my problem is. I have 2 rows of 5 panels called MyPanel within a for-loop (numbered going up to 10) and would like to say, If panel 8 is clicked, display a certain message and allow no further panels to be clicked until the next game (done via a restart button, which i've sorted).

However, I don't know the best approach to this, the only thing that comes to mind is an IF-ELSE statement but i'm not sure if theres a better or simpler way of doing this: here is the relevant code:

The for-loop that contains the panels.

for (int i = 0; i < panel.length; i++) {
        panel[i] = new MyPanel(this);
        pane.add(panel[i]);

        if (i % COLS == 0) {
            tempColor = col1;

            }
        if (i  <= 10) {
            panel[i].setBackground(col1);
        }
        //Some sort of if statement that says when panel 8 is clicked, a game over message appears
        //and doesn't allow further panel presses until game restart.
    }

Information from the MyPanel Class (where i maybe could but an event for this?):

public MyPanel(Checkers checkers) {

    this.checkers = checkers;

    addMouseListener(this);
}

@Override
public void mouseClicked(MouseEvent e) {
    setBackground(Color.BLACK);
    checkers.incrementScore(); 

}

Any advice would be great, any formatting errors please let me know.

Aucun commentaire:

Enregistrer un commentaire