Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Mouse Event Question  RSS feed

 
Vanessa Astle
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've recently started working with Java and am working with the acm.graphics Package. I'm trying to understand mouse events but am having some difficulty finding resources on them.

Do mouse events work in a way that everything inside my mouseClicked(MouseEvent e) method completes once? Because that is what I had expected, but this does not seem to be the case. Is there a trick to mouseEvents that I'm not catching? Is there something I should be including in my run() or init() methods besides addMouseListeners() that will make the code inside my mouseClicked method run only once per click?

Help would be greatly appreciated and thanks for your time!!
 
Vanessa Astle
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the area of code I am having problems with. I have tested it several times and am able to see what the problem is, I just have no idea why. The code is used in the GamePlay class of a matching game. Everything does what it is supposed to, simply not in the right order. Either the code runs more than once per click, or I have a problem with my if statements. If you see anything, please let me know!

Color col1 = null;
Color col2 = null;
private GCard firstCardClicked = null;
private GCard secondCardClicked = null;
int numFlipped = 0;

public void mouseClicked(MouseEvent e)
{
if(numFlipped == 0)
{
firstCardClicked = (GCard) this.getElementAt(e.getX(),e.getY());
col1 = firstCardClicked.getColor();
firstCardClicked.flipCard();
//The flipCard() method here is not working.
}
if(numFlipped == 1)
{
secondCardClicked = (GCard) this.getElementAt(e.getX(),e.getY());
col2 = secondCardClicked.getColor();
secondCardClicked.flipCard();
//The flipCard() method here is not working.
}
numFlipped++;
if(numFlipped == 2);
{
if (col1.equals(col2))
{
pause(1000);
firstCardClicked.removeAll();
cardArray.remove(firstCardClicked);
secondCardClicked.removeAll();
cardArray.remove(secondCardClicked);
//This part of the code ALWAYS works correctly.
}

firstCardClicked.unflipCard();
secondCardClicked.unflipCard();
//For some reason, the two method calls above occur before the rest of the code, and I have no idea why...
numFlipped = 0;
firstCardClicked = null;
col1 = null;
secondCardClicked = null;
col2 = null;
}
}
 
Darrin Cartwright
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vanessa,

Check your handling of the numFlipped variable. Best yet, watch it in your debugger as you click on a few cards.
Also the statement,

looks a bit dubious. I'm not sure that's what you intended.
 
Vanessa Astle
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is infact what was causing the problem. Hurray for dumb typos!

Thanks very much for your help!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!