Name: diC59631 Date: 09/22/97
Using the app below, move the mouse onto the black bar. Press the button, and QUICKLY move the mouse to left or
right. Notice that the Draggable receives a MOUSE_PRESSED event, then a MOUSE_EXITED event, but no
MOUSE_DRAGGED events.
Here is some typical console output for such a situation ...
mousePressed java.awt.event.MouseEvent[MOUSE_PRESSED,(198,29),mods=16,clickCount
=1] on panel1
setCursor(java.awt.Cursor@1eee04)
mouseExited java.awt.event.MouseEvent[MOUSE_EXITED,(233,27),mods=0,clickCount=0]
on panel1
Here is the source code ...
import java.awt.*;
import java.awt.event.*;
public class test extends Frame {
Draggable d;
Button b1, b2;
public static void main(String[] args) {
new test();
}
test() {
super("Test Mouse dragging");
Panel p = new Panel();
d = new Draggable();
d.setLayout(null);
d.add(b1 = new Button("Button 1"));
d.add(b2 = new Button("Button 2"));
p.add(d);
add(p);
show();
b1.setBounds(0, 0, 50, 100);
b2.setBounds(55, 0, 445, 100);
setSize(500, 100);
d.setBackground(Color.white);
d.setForeground(Color.black);
}
}
class Draggable extends Panel implements MouseListener, MouseMotionListener {
boolean dragging;
int x, y;
Rectangle dragArea = new Rectangle(50, 0, 5, 100);
Cursor currentCursor, originalCursor;
public Draggable() {
addMouseListener(this);
addMouseMotionListener(this);
}
public Dimension preferredSize() {
return new Dimension(500, 100);
}
public Dimension minimumSize() {
return preferredSize();
}
/** Change the mouse cursor if over a draggable border. */
public void mouseMoved(MouseEvent e) {
System.out.println("mouseMoved " + e);
if(!dragging) {
if(dragArea.contains(e.getX(), e.getY()))
cursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
else
cursor(null);
}
}
/** Select a border to drag. */
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed " + e);
dragging = true;
cursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
x = e.getX();
y = e.getY();
}
/** Stop dragging a border. */
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased " + e);
if(dragging) {
dragging = false;
mouseMoved(e);
}
}
public void mouseClicked(MouseEvent e) {
System.out.println("mouseClicked " + e);
}
public void mouseEntered(MouseEvent e) {
System.out.println("mouseEntered " + e);
}
public void mouseExited(MouseEvent e) {
System.out.println("mouseExited " + e);
if(!dragging)
cursor(null);
}
/** If a border is selected, change it's position. */
public void mouseDragged(MouseEvent e) {
if(e.getX() != x) {
x = e.getX();
System.out.println("mouseDragged " + e);
if (dragging) {
System.out.println("Moved to " + e.getX() + "," + e.getY());
dragArea.x = x;
getComponent(0).setSize(x, 100);
getComponent(1).setBounds(x + 5, 0, 495 - x, 100);
repaint();
}
}
}
private void cursor(Cursor newcursor) {
System.out.println("setCursor(" + newcursor + ")");
if(newcursor != null) {
// Want to set cursor to new shape
if(originalCursor == null) // Save original shape
originalCursor = getCursor();
setCursor(newcursor);
} else if(originalCursor != null) {
setCursor(originalCursor);
originalCursor = null;
}
currentCursor = newcursor;
}
public void paint(Graphics g) {
super.paint(g);
g.fillRect(dragArea.x, dragArea.y, dragArea.width, dragArea.height);
}
}
======================================================================