1

I've two classes in my program that need to communicate with each other (Tile, Field)

The Tile class implement a MouseListener, when the user clicks on the Tile, I want the Field class to get notified.

I know how to achieve this in C#, Objective-c or Swift, but I've no idea how to do it in Java.

This is the code of the Tile class

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

import org.omg.CORBA.Current;

public class Tile extends JButton implements MouseListener {

private static final long serialVersionUID = -2445981172620162110L;

public boolean isBomb;
private int posX;
private int posY;

public enum TileState {
    HIDDEN, MARKED, EXPOSED
}

private TileState currentState;

public Tile(int posX, int posY) {
    this.currentState = TileState.HIDDEN;
    this.addMouseListener(this);
    this.setOpaque(true);
    this.setBorder(new LineBorder(Color.darkGray, 1));
    this.setBackground(Color.lightGray);

    this.posX = posX;
    this.posY = posY;

    this.isBomb = this.generateBomb();
    if(this.isBomb) {
        this.setBackground(Color.blue);
    }

}

private void changeIcon(TileState state) {

}

private boolean generateBomb() {
    return (new Random().nextInt(5) + 1 ) == 5 ? true : false;
}

private void changeCurrentState(TileState newState) {
    if (newState != null) {
        this.currentState = newState;
        this.changeTileColor(newState);
    }

}

private void changeTileColor(TileState tileState) {     
    switch (tileState) {
    case EXPOSED:
        this.setBackground(Color.white);
        break;
    case HIDDEN:
        this.setBackground(Color.lightGray);
        break;
    case MARKED:
        this.setBackground(Color.red);
        break;
    }
}

public int getXPosition() {
    return this.posX;
}

public int getYPosition() {
    return this.posY;
}

@Override
public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub
    if (e.getButton() == MouseEvent.BUTTON1 || e.getButton() == MouseEvent.BUTTON3) {
        TileState newState = null;

        if (e.getButton() == MouseEvent.BUTTON1) {
            System.out.println("Left Click");
            if (this.currentState == TileState.MARKED) {
                newState = TileState.HIDDEN;
            } else if (this.currentState == TileState.HIDDEN) {
                newState = TileState.EXPOSED;
            }

        }
        else if (e.getButton() == MouseEvent.BUTTON3) {
            System.out.println("Right Click");
            if (this.currentState != TileState.EXPOSED) {
                newState = (this.currentState == TileState.MARKED) ? TileState.HIDDEN : TileState.MARKED;
            }
        }

        this.changeCurrentState(newState);
    }


}

@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}

}
Mo Al Waili
  • 180
  • 1
  • 9
  • 2
    add a reference of the field to the Tile class. if you wanne do it the right way take a look at the observer pattern. – Joshua K Sep 11 '15 at 21:44
  • Implement `Notifiable` and when the Field needs to be notified you call `notify`. –  Sep 11 '15 at 21:50
  • I don't like the first solution, it will cause a problems like retain cycles (not sure if this is the case in Java). I'll take a look at the observer pattern thank you very much – Mo Al Waili Sep 11 '15 at 21:52
  • @nikpon I'll check it out, thank you very much – Mo Al Waili Sep 11 '15 at 21:53
  • 3
    Is this for a MineSweeper game? Myself, I'd use SwingPropertyChangeSupport to allow easy addition of listeners that will be notified via PropertyChangeListeners (and this is important) on the Swing event thread. As side recommendation, I'd try to avoid subclassing JButton but rather would use JButton, and would avoid having a JButton respond to a MouseListener but rather would use the more appropriate ActionListener or a ChangeListener on the ButtonModel. – Hovercraft Full Of Eels Sep 11 '15 at 22:11
  • @HovercraftFullOfEels Yes it's for a MineSweeper game. this is actually my first java assignment and I've zero experience in Java and I don't know what I'm doing hahaha. Thank you for the tips i will look into that as well. – Mo Al Waili Sep 11 '15 at 22:24
  • Please see my code and answer to a similar question [here](http://stackoverflow.com/a/7016492/522444). I now can recommend *strongly* that you don't extend JButton, that this is *far* too restrictive. Instead consider using a CardLayout to swap components as I demonstrate in the example code. Also, you'll see an example of use of PropertyChangeListener for your observer pattern as I mentioned above. – Hovercraft Full Of Eels Sep 11 '15 at 22:35

0 Answers0