/Users/lyon/j4p/src/bookExamples/ch26Graphics/draw2d/ComponentMoveFrame.java

1    package bookExamples.ch26Graphics.draw2d; 
2     
3    import ip.gui.frames.ShortCutFrame; 
4     
5    import java.awt.*; 
6    import java.awt.event.MouseEvent; 
7    import java.awt.event.MouseListener; 
8    import java.awt.event.MouseMotionListener; 
9     
10   public class ComponentMoveFrame extends ShortCutFrame 
11           implements 
12           MouseMotionListener, MouseListener { 
13       private int startX,startY,posX,posY; 
14    
15       public void mouseClicked(MouseEvent event) { 
16       } 
17    
18       public void mouseEntered(MouseEvent event) { 
19       } 
20    
21       public void mouseExited(MouseEvent event) { 
22       } 
23    
24       public void mousePressed(MouseEvent event) { 
25           posX = ((Component) event.getSource()).getLocation().x; 
26           posY = ((Component) event.getSource()).getLocation().y; 
27           startX = event.getX(); 
28           startY = event.getY(); 
29       } 
30    
31       public void mouseReleased(MouseEvent event) { 
32       } 
33    
34       public void mouseDragged(MouseEvent event) { 
35           int x = event.getX(); 
36           int y = event.getY(); 
37    
38           posX += (x - startX); 
39           posY += (y - startY); 
40           ((Component) event.getSource()).setLocation(posX, posY); 
41    
42       } 
43    
44       public void mouseMoved(MouseEvent event) { 
45       } 
46    
47       public Component add(Component c) { 
48           c.addMouseListener(this); 
49           c.addMouseMotionListener(this); 
50           return super.add(c); 
51       } 
52    
53   } 
54    
55