Java Swing - Listeners
TOPIC 03 – MOUSEMOTIONLISTENER
LESSON NOTE
INTRO
We use a MouseMotionListener
object to listen for mouse movement and dragging. It works in the same way as the MouseListener & KeyListener
objects.
MOUSEMOTIONLISTENER INTERFACE
All of the following methods must be implemented inside a
class that implements the interface:
public void mouseDragged(MouseEvent e)
public void mouseMoved(MouseEvent e)
Each
method relates to an obvious event.
Note that both events do not occur at the same time. So, if the mouse is being dragged, then
that is not a movement event.
WHAT WILL THIS LOOK LIKE?
public
class MyPanelName extends JPanel
implements MouseMotionListener
{
//datafields go
here
public MyPanelName()
{
//initialize datafields here
//...
addMouseMotionListener(this);
}
public void
paint(Graphics g)
{
//display info based on datafields
}
public void mouseMoved (MouseEvent e)
{
//react to event here (usually update datafields)
}
public void mouseDragged(MouseEvent e)
{
//react to event here (usually update datafields)
}
}
EXAMPLE 1 – SIMULATED DRAGGING
The
following example will show how to drag an item around the screen. There are limitations to this
implementation as we do not currently have access to knowing when the mouse
is clicked or released. Two issues arise:
·
dragging
too quickly will see the object get left behind;
·
dragging before going over the
object, the object will stick to your cursor.
public class App
{
public static void main(String[] args)
{
JFrame jf = new JFrame();
jf.setTitle("MouseMotionListeners");
jf.setSize(800,600);
DragPanel kp = new DragPanel();
jf.add(kp);
jf.setVisible(true);
}
}
|
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
public class DragPanel extends JPanel implements MouseMotionListener
{
public int sx;
//square's x, y, width and height
public int sy;
public int sw;
public int sh;
public DragPanel()
{
sx = 100;
sy = 100;
sw = 20;
sh = 20;
this.addMouseMotionListener(this);
}
public void
paint(Graphics g)
{
super.paint(g);
g.drawRect(sx, sy, sw, sh);
}
public void mouseDragged(MouseEvent e)
{
int ex = e.getX();
int ey
= e.getY();
//if dragging occurs over square
if (ex >= sx && ex <= sx + sw && ey >= sy && ey
<= sy + sh)
{
sx = e.getX() - sw/2;
sy = e.getY() - sh/2;
repaint();
}
}
public void mouseMoved(MouseEvent e)
{
}
}
|
USING
TWO LISTENER TYPES
To get full dragging implementation, we need to use both MouseMotionListener and MouseListener. We simply implement both in a single
class.
PROPPER
DRAGGING OF A SHAPE
To properly drag a shape, it must move only when it is clicked on at
the very start of the drag and should follow the mouse no matter how fast it
moves.
We can now keep track of whether an object is clicked on at the initial
point that a click occurs and hold on to it until the mouse is released.
EXAMPLE
2 – PROPPER DRAGGING
Here is an example of a
object being dragged correctly.
public class App
{
public static void main(String[] args)
{
JFrame jf = new JFrame();
jf.setTitle("MouseMotionListeners");
jf.setSize(800,600);
DragPanel kp = new DragPanel();
jf.add(kp);
jf.setVisible(true);
}
}
|
package multi;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
public class DragPanel
extends JPanel implements MouseMotionListener,MouseListener
{
public
int sx;
public
int sy;
public
int sw;
public
int sh;
public
boolean dragged;
public
DragPanel()
{
sx = 100;
sy = 100;
sw = 20;
sh = 20;
dragged
= false;
this.addMouseMotionListener(this);
this.addMouseListener(this);
}
public
void paint(Graphics g)
{
super.paint(g);
g.drawRect(sx, sy, sw, sh);
}
public
void mouseDragged(MouseEvent
e)
{
if
(dragged == true)
{
sx=e.getX();
sy=e.getY();
repaint();
}
}
public
void mouseMoved(MouseEvent
e)
{
}
public
void mouseClicked(MouseEvent
arg0)
{
}
public
void mouseEntered(MouseEvent
arg0)
{
}
public
void mouseExited(MouseEvent
arg0)
{
}
public
void mousePressed(MouseEvent
e)
{
int ex = e.getX();
int ey = e.getY();
//if object clicked on...
if
(ex >= sx && ex <= sx + sw && ey >= sy && ey <= sy + sh)
{
dragged
= true;
sx = e.getX() - sw/2;
sy = e.getY() - sh/2;
repaint();
}
}
public
void mouseReleased(MouseEvent
e)
{
dragged = false;
}
}
|
|