/Users/lyon/j4p/src/net/rmi/rmiimage/MovableComponent.java

1    package net.rmi.rmiimage; 
2     
3    import java.awt.*; 
4    import java.awt.event.MouseEvent; 
5    import java.awt.event.MouseListener; 
6    import java.awt.event.MouseMotionListener; 
7     
8    public abstract class MovableComponent 
9        extends Component 
10       implements MouseMotionListener, 
11       MouseListener { 
12     private int w = 10; 
13     private int h = 5; 
14     private boolean initialized = init(); 
15     private int x1,y1,x2,y2; 
16    
17     private boolean init() { 
18       addMouseMotionListener(this); 
19       addMouseListener(this); 
20       setCursor( 
21           Cursor.getPredefinedCursor( 
22               Cursor.HAND_CURSOR)); 
23       return true; 
24     } 
25    
26     public Dimension getPreferredSize() { 
27       return new Dimension(w, h); 
28     } 
29    
30     public void mouseReleased(MouseEvent e) { 
31     } 
32    
33     public void mouseEntered(MouseEvent e) { 
34     } 
35    
36     public void mouseExited(MouseEvent e) { 
37     } 
38    
39     public void mouseClicked(MouseEvent e) { 
40     } 
41    
42     public void mouseDragged(MouseEvent e) { 
43       e.consume(); 
44       x1 = x2; 
45       y1 = y2; 
46       x2 = e.getX(); 
47       y2 = e.getY(); 
48       setLocation(x2, y2); 
49       repaint(); 
50     } 
51    
52     public void mouseMoved(MouseEvent e) { 
53     } 
54    
55     public void mousePressed(MouseEvent e) { 
56       e.consume(); 
57       x1 = e.getX(); 
58       y1 = e.getY(); 
59       x2 = -1; 
60     } 
61   } 
62