SE450: Car Mover Program [20/65] Previous pageContentsNext page

file:horstmann/ch06_car/CarComponent.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package horstmann.ch06_car;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JComponent;

/**
   A component that shows a scene composed of items.
 */
@SuppressWarnings("serial")
public class CarComponent extends JComponent
{
  public CarComponent()
  {
    car = new CarShape(20, 20, 50);
    addMouseListener(new
        MouseAdapter()
    {
      public void mousePressed(MouseEvent event)
      {
        mousePoint = event.getPoint();
        if (!car.contains(mousePoint))
          mousePoint = null;
      }
    });

    addMouseMotionListener(new
        MouseMotionAdapter()
    {
      public void mouseDragged(MouseEvent event)
      {
        if (mousePoint == null) return;
        Point lastMousePoint = mousePoint;
        mousePoint = event.getPoint();

        double dx = mousePoint.getX() - lastMousePoint.getX();
        double dy = mousePoint.getY() - lastMousePoint.getY();
        car.translate((int) dx, (int) dy);
        repaint();
      }
    });
  }

  public void paintComponent(Graphics g)
  {
    Graphics2D g2 = (Graphics2D) g;
    car.draw(g2);
  }

  private CarShape car;
  private Point mousePoint;
}

file:horstmann/ch06_car/CarMover.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package horstmann.ch06_car;
import javax.swing.JFrame;

/**
   A program that allows users to move a car with the mouse.
 */
public class CarMover
{
  public static void main(String[] args)
  {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(new CarComponent());
    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    frame.setVisible(true);
  }

  private static final int FRAME_WIDTH = 400;
  private static final int FRAME_HEIGHT = 400;
}


file:horstmann/ch06_car/CarShape.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package horstmann.ch06_car;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/**
   A car shape.
 */
public class CarShape
{
  /**
      Constructs a car shape.
      @param x the left of the bounding rectangle
      @param y the top of the bounding rectangle
      @param width the width of the bounding rectangle
   */
  public CarShape(int x, int y, int width)
  {
    this.x = x;
    this.y = y;
    this.width = width;
  }

  public void draw(Graphics2D g2)
  {
    Rectangle2D.Double body
    = new Rectangle2D.Double(x, y + width / 6,
        width - 1, width / 6);
    Ellipse2D.Double frontTire
    = new Ellipse2D.Double(x + width / 6, y + width / 3,
        width / 6, width / 6);
    Ellipse2D.Double rearTire
    = new Ellipse2D.Double(x + width * 2 / 3,
        y + width / 3,
        width / 6, width / 6);

    // The bottom of the front windshield
    Point2D.Double r1
    = new Point2D.Double(x + width / 6, y + width / 6);
    // The front of the roof
    Point2D.Double r2
    = new Point2D.Double(x + width / 3, y);
    // The rear of the roof
    Point2D.Double r3
    = new Point2D.Double(x + width * 2 / 3, y);
    // The bottom of the rear windshield
    Point2D.Double r4
    = new Point2D.Double(x + width * 5 / 6, y + width / 6);
    Line2D.Double frontWindshield
    = new Line2D.Double(r1, r2);
    Line2D.Double roofTop
    = new Line2D.Double(r2, r3);
    Line2D.Double rearWindshield
    = new Line2D.Double(r3, r4);

    g2.draw(body);
    g2.draw(frontTire);
    g2.draw(rearTire);
    g2.draw(frontWindshield);
    g2.draw(roofTop);
    g2.draw(rearWindshield);
  }

  public boolean contains(Point2D p)
  {
    return x <= p.getX() && p.getX() <= x + width
        && y <= p.getY() && p.getY() <= y + width / 2;
  }

  public void translate(int dx, int dy)
  {
    x += dx;
    y += dy;
  }

  private int x;
  private int y;
  private int width;
}

Previous pageContentsNext page