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

1    package bookExamples.ch26Graphics.draw2d; 
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 class ComponentMovePanel extends Panel 
9            implements 
10           MouseMotionListener, MouseListener { 
11       private int startX,startY,posX,posY; 
12    
13       public void mouseClicked(MouseEvent event) { 
14       } 
15    
16       public void mouseEntered(MouseEvent event) { 
17       } 
18    
19       public void mouseExited(MouseEvent event) { 
20       } 
21    
22       public void mousePressed(MouseEvent event) { 
23           posX = ((Component) event.getSource()).getLocation().x; 
24           posY = ((Component) event.getSource()).getLocation().y; 
25           startX = event.getX(); 
26           startY = event.getY(); 
27       } 
28    
29       public void mouseReleased(MouseEvent event) { 
30       } 
31    
32       public void mouseDragged(MouseEvent event) { 
33           int x = event.getX(); 
34           int y = event.getY(); 
35    
36           posX += (x - startX); 
37           posY += (y - startY); 
38           ((Component) event.getSource()).setLocation(posX, posY); 
39    
40       } 
41    
42       public void mouseMoved(MouseEvent event) { 
43       } 
44    
45       public Component add(Component c) { 
46           c.addMouseListener(this); 
47           c.addMouseMotionListener(this); 
48           return super.add(c); 
49       } 
50    
51   } 
52    
53