    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 panel that shows a scene composed of items.
    09: */
    10: public class CarPanel extends JPanel
    11: {
    12:    public CarPanel()
    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:                car.translate(
    36:                   mousePoint.getX() - lastMousePoint.getX(),
    37:                   mousePoint.getY() - lastMousePoint.getY());
    38:                repaint();
    39:             }
    40:          });
    41:    }
    42:
    43:    public void paintComponent(Graphics g)
    44:    {
    45:       super.paintComponent(g);
    46:       Graphics2D g2 = (Graphics2D) g;
    47:       car.draw(g2);
    48:    }
    49:
    50:    private CarShape car;
    51:    private Point mousePoint;
    52: }