01: import java.awt.*;
02: import java.awt.event.*;
03: import java.awt.geom.*;
04: import javax.swing.*;
05: import java.util.*;
06: 
07: /**
08:    A component that shows a scene composed of items.
09: */
10: public class CarComponent extends JComponent
11: {
12:    public CarComponent()
13:    {
14:       car = new CarShape(20, 20, 50);
15:       addMouseListener(new
16:          MouseAdapter()
17:          {
18:             public void mousePressed(MouseEvent event)
19:             {
20:                mousePoint = event.getPoint();
21:                if (!car.contains(mousePoint))
22:                   mousePoint = null;
23:             }
24:          });
25: 
26:       addMouseMotionListener(new
27:          MouseMotionAdapter()
28:          {
29:             public void mouseDragged(MouseEvent event)
30:             {
31:                if (mousePoint == null) return;
32:                Point lastMousePoint = mousePoint;
33:                mousePoint = event.getPoint();
34: 
35:                double dx = mousePoint.getX() - lastMousePoint.getX();
36:                double dy = mousePoint.getY() - lastMousePoint.getY();
37:                car.translate((int) dx, (int) dy);
38:                repaint();
39:             }
40:          });
41:    }
42: 
43:    public void paintComponent(Graphics g)
44:    {
45:       Graphics2D g2 = (Graphics2D) g;
46:       car.draw(g2);
47:    }
48: 
49:    private CarShape car;
50:    private Point mousePoint;
51: }