Okay, so this has been driving me nuts, but I can't seem to figure out how to handle the events for a left mouse click and right mouse click differently in Java.
Here's a snippet of the code I currently have set up.
public void mouseClicked(MouseEvent e) {
// Get the source first
Object source = e.getSource();
// Get which mouseButton was clicked.
int buttonType = e.getButton();
if (source instanceof Cell) {
Cell cell = (Cell) source;
int r = cell.getRow();
int c = cell.getCol();
//System.out.println(buttonType);
// Left click
if (buttonType == MouseEvent.BUTTON1);
{
//If the game hasn't started yet
if (game == null) {
game = new MineSweeper();
game.startNewGame(9, 9, 10);
}
game.printBoard();
game.uncoverCell(r, c);
game.printBoard();
System.out.println("Left mouse button clicked.");
this.updateGUI();
game.checkWinCondition();
}
// Right click.
if (buttonType == MouseEvent.BUTTON3) {
System.out.println("Right mouse button clicked.");
}
}
}
You can take a look at the entire project at the link in my signature.
What's happening right now is when ever I right click on an object, a left click event is fired as well.
Am I missing something, obvious or not? Is there a better way to do this? I would very much appreciate the help!