Reputation: 816
I have an applet that makes use of the AWT event model. It has a boolean that says if the left button is pressed or not. Here is a sample code:
public class Game extends Applet implements MouseListener
{
boolean isLeftButtonPressed;
public void init()
{
addMouseListener(this);
isLeftButtonPressed = false;
}
public void paint(Graphics g)
{
g.drawString("Is Button Pressed: " + isLeftButtonPressed, 20, 20);
}
@Override
public void mouseClicked(MouseEvent e)
{
isLeftButtonPressed = true;
repaint();
}
@Override
public void mouseReleased(MouseEvent e)
{
isLeftButtonPressed = false;
repaint();
}
//Other MouseListener methods not listed but have to be implemented
}
But it seems as if the left button is never released, even after you actually do so. What could be the problem?
Upvotes: 1
Views: 915
Reputation: 6054
The fundamental in this is incorrect,
These are the mouse events,
MousePressed -> a mouse button is pressed
MouseReleased -> a mouse button is released
MouseClicked -> a mouse button is clicked (pressed and released)
So, when you handle the click event that means mouse is clicked and released.
So i think you have to use mousepressed instead of clicked.
Upvotes: 4
Reputation: 94645
Method mouseClicked
will be called after mouseReleased
method so value of isLgetButtonPressed will be true. You have to use MouseEvent.getButton()
method to check which mouse button is pressed.
Upvotes: 4