can anyone tell me why the mouseMotionListener is not firing the mouseDragged event(I have googled for hours and even copy-and-pasted code from the net!) Below is the code for the class - I create an object of the class and add it to a JPanel called canvas.
PS the mousePressed() is the only method that gets fired, mouseDragged() and mouseReleased(), they do not.
class MouseActions extends MouseInputAdapter
{
@Override
public void mousePressed(MouseEvent e)
{
super.mousePressed(e);//245 220
java.awt.Point Pos = e.getPoint();
System.out.println("at Mouse Pressed, Again");
if(e.getButton() == MouseEvent.BUTTON3)
{
if(ArrayOfShapes == null)
return;
for(int i = 0; i < ArrayOfShapes.length; i++)
{
if(hasEntered(ArrayOfShapes[i], Pos))
{
removeAtIndex(i);
return;
}
}
}
}
@Override
public void mouseDragged(MouseEvent e)
{
System.out.println("at Mouse Dragged");
int MovableIndex = -1;
java.awt.Point Pos = e.getPoint();
if(e.getButton() == MouseEvent.BUTTON1)
{
bDragged = true;
while(bDragged)
{
for(int i = 0; i < ArrayOfShapes.length; i++)
{
if(hasEntered(ArrayOfShapes[i], Pos))
{
MovableIndex = i;
break;
}
}
ArrayOfShapes[MovableIndex].setX(e.getX());
ArrayOfShapes[MovableIndex].setY(e.getY());
thisCurrentWindow.repaint();
}
}
}
@Override
public void mouseReleased(MouseEvent e)
{
System.out.println("at Mouse Release");
bDragged = false;
}
}
/// Now the code to add the listeners
MouseActions MA = new MouseActions();
canvas.addMouseListener(MA);
canvas.addMouseMotionListener(MA);
Again Thanks alot~
M
PS.... For all who are doubting my mad inheritance skills
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;
public class CustomJPanel extends JPanel
{
class MouseActions extends MouseInputAdapter
{
@Override
public void mousePressed(MouseEvent e)
{
super.mousePressed(e);
System.out.println("Pressed");
}
@Override
public void mouseDragged(MouseEvent e)
{
super.mouseDragged(e);
System.out.println("Dragged");
}
@Override
public void mouseReleased(MouseEvent e)
{
super.mouseReleased(e);
System.out.println("Released");
}
}
/**
* @param args the command line arguments
*/
private CustomJPanel()
{
MouseActions ma = new MouseActions();
addMouseListener(ma);
addMouseMotionListener(ma);
}
public static void main(String[] args)
{
// TODO code application logic here
JFrame frame = new JFrame();
CustomJPanel cP = new CustomJPanel();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cP.setSize(500, 500);
frame.add(cP);
frame.setVisible(true);
}
}
Again when I assign this to canvas it never fires the release or the dragged
M