Help me to view my mistakes in mouseMoved
Hallo !
I've been around here looking for an article or discussion about mouseMoved..but none has answered my problem. Hmm..how do I describe my problem...ok, sorry for the long *** post here, but I am not a good expainer.
The Case:
I want to make a cell which has 45 x 45 pixels size. So, I made a class namely BlankCell by extending JPanel. BlankCell has 2 events, they are mouseClicked and mouseMoved (exactly mouseHover but there is no mouseHover in Java).
What I want to do:
To make a HOVER State by loading another image. And when the mouse is not over the BlankCell it returns to Normal state.
With my code below...the image will turn into hover state as soon as the cursor enters JFrame (in the main method and I didn't include the main method here). And when you see in the Inner Class ofMyMouseMotionAdapter, you'll see that I've tried to use a flag:
private boolean hover = false;
But still...the problem exists.
Where did I make mistakes ? Are there ways to do this better, I mean maybe I can set a boundaries or something like that ? Please help !
Thank you for your time to review this...
Here's my code:
publicclass BlankCellextends JPanel{
private ImageIcon img;
//to hold image name on normal state
privatefinalstatic String IMAGE_NORMAL_STATE_NAME ="BlankCells";
//to hold image name on hover state
privatefinalstatic String IMAGE_HOVER_STATE_NAME ="BlankCellsHover";
//initialize BlankCell by loading image
public BlankCell (){
//load image
img =new ImageIcon (getClass().getResource("img/" + IMAGE_NORMAL_STATE_NAME +".gif"));
//all image's width and heights are the same (45 x 45 pixels)
width = img.getIconWidth ();
height = img.getIconHeight ();
// register MouseMotionListener
addMouseMotionListener (new MyMouseMotionAdapter (this));
}// end constructor
// accessor methods...but I didn't write it here, it would be too long
//Inner class to Handle Mouse Motion Listener
class MyMouseMotionAdapterextends MouseMotionAdapter{
privateboolean hover =false;
BlankCell blankCell;
public MyMouseMotionAdapter (BlankCell blankCell){
this.blankCell = blankCell;
}
//To Handle Mouse Entered
publicvoid mouseMoved (MouseEvent me){
if (this.hover != (contains(me.getX(), me.getY()))){
this.hover = ! this.hover;
if (this.hover){
// "mouse hover" code
img =new ImageIcon (getClass().getResource("img/" + IMAGE_HOVER_STATE_NAME +".gif"));
}else{
// "mouse leave" code
img =new ImageIcon (getClass().getResource("img/" + IMAGE_NORMAL_STATE_NAME +".gif"));
}//end inner if - else
}//end outer if-else
repaint();
}//end mouseMoved method
}//end inner class MyMouseMotionAdapter
}//end BlankCell class

