1 import java.awt.*;
2 import java.awt.geom.*;
3 import javax.swing.*;
4
5 /**
6
7 */
8 public class CarBean extends JComponent
9 {
10 /**
11
12 */
13 public CarBean()
14 {
15 x = 0;
16 y = 0;
17 width = DEFAULT_CAR_WIDTH;
18 height = DEFAULT_CAR_HEIGHT;
19 }
20
21 /**
22
23 @param newValue
24 */
25 public void setX(int newValue)
26 {
27 x = newValue;
28 repaint();
29 }
30
31 /**
32
33 @return
34 */
35 public int getX()
36 {
37 return x;
38 }
39
40 /**
41
42 @param newValue
43 */
44 public void setY(int newValue)
45 {
46 y = newValue;
47 repaint();
48 }
49
50 /**
51
52 @return
53 */
54 public int getY()
55 {
56 return y;
57 }
58
59 public void paintComponent(Graphics g)
60 {
61 Graphics2D g2 = (Graphics2D) g;
62 Rectangle2D.Double body
63 = new Rectangle2D.Double(x, y + height / 3,
64 width - 1, height / 3);
65 Ellipse2D.Double frontTire
66 = new Ellipse2D.Double(x + width / 6,
67 y + height * 2 / 3, height / 3, height / 3);
68 Ellipse2D.Double rearTire
69 = new Ellipse2D.Double(x + width * 2 / 3,
70 y + height * 2 / 3, height / 3, height / 3);
71
72 //
73 Point2D.Double r1
74 = new Point2D.Double(x + width / 6, y + height / 3);
75 //
76 Point2D.Double r2
77 = new Point2D.Double(x + width / 3, y);
78 //
79 Point2D.Double r3
80 = new Point2D.Double(x + width * 2 / 3, y);
81 //
82 Point2D.Double r4
83 = new Point2D.Double(x + width * 5 / 6, y + height / 3);
84
85 Line2D.Double frontWindshield
86 = new Line2D.Double(r1, r2);
87 Line2D.Double roofTop
88 = new Line2D.Double(r2, r3);
89 Line2D.Double rearWindshield
90 = new Line2D.Double(r3, r4);
91
92 g2.draw(body);
93 g2.draw(frontTire);
94 g2.draw(rearTire);
95 g2.draw(frontWindshield);
96 g2.draw(roofTop);
97 g2.draw(rearWindshield);
98 }
99
100 public Dimension getPreferredSize()
101 {
102 return new Dimension(DEFAULT_PANEL_WIDTH,
103 DEFAULT_PANEL_HEIGHT);
104 }
105
106 private int x;
107 private int y;
108 private int width;
109 private int height;
110
111 private static final int DEFAULT_CAR_WIDTH = 60;
112 private static final int DEFAULT_CAR_HEIGHT = 30;
113 private static final int DEFAULT_PANEL_WIDTH = 160;
114 private static final int DEFAULT_PANEL_HEIGHT = 130;
115 }