001: import java.awt.*;
002: import java.awt.geom.*;
003: import javax.swing.*;
004:
005: /**
006: A panel that draws a car shape.
007: */
008: public class CarBean extends JPanel
009: {
010: /**
011: Constructs a default car bean.
012: */
013: public CarBean()
014: {
015: x = 0;
016: y = 0;
017: width = DEFAULT_WIDTH;
018: height = DEFAULT_HEIGHT;
019: color = DEFAULT_COLOR;
020: fill = false;
021: }
022:
023: /**
024: Sets the color property.
025: @param color the new color
026: */
027: public void setColor(Color color)
028: {
029: this.color = color;
030: repaint();
031: }
032:
033: /**
034: Gets the color property.
035: @return the current color
036: */
037: public Color getColor() { return color; }
038:
039: /**
040: Sets the dimension property.
041: @param dimension the new dimension of the house
042: */
043: public void setDimension(Dimension dimension)
044: {
045: width = (int) dimension.getWidth();
046: height = (int) dimension.getHeight();
047: repaint();
048: }
049:
050: /**
051: Gets the dimension property.
052: @return the current dimension of the house
053: */
054: public Dimension getDimension()
055: {
056: return new Dimension(width, height);
057: }
058:
059: /**
060: Sets the drawMode property.
061: @param drawMode the new drawMode (DRAW or FILL)
062: */
063: public void setDrawMode(DrawMode drawMode)
064: {
065: if (drawMode == DrawMode.DRAW)
066: {
067: fill = false;
068: repaint();
069: }
070: else if (drawMode == DrawMode.FILL)
071: {
072: fill = true;
073: repaint();
074: }
075: }
076:
077: /**
078: Gets the drawMode property.
079: @return the current drawMode (DRAW or FILL)
080: */
081: public DrawMode getDrawMode()
082: {
083: if (fill) return DrawMode.FILL;
084: else return DrawMode.DRAW;
085: }
086:
087: public void paintComponent(Graphics g)
088: {
089: super.paintComponent(g);
090: Graphics2D g2 = (Graphics2D) g;
091: Rectangle2D.Double body
092: = new Rectangle2D.Double(x, y + height / 3,
093: width - 1, height / 3);
094: Ellipse2D.Double frontTire
095: = new Ellipse2D.Double(x + width / 6,
096: y + height * 2 / 3, height / 3, height / 3);
097: Ellipse2D.Double rearTire
098: = new Ellipse2D.Double(x + width * 2 / 3,
099: y + height * 2 / 3, height / 3, height / 3);
100:
101: // the bottom of the front windshield
102: Point2D.Double r1
103: = new Point2D.Double(x + width / 6, y + height / 3);
104: // the front of the roof
105: Point2D.Double r2
106: = new Point2D.Double(x + width / 3, y);
107: // the rear of the roof
108: Point2D.Double r3
109: = new Point2D.Double(x + width * 2 / 3, y);
110: // the bottom of the rear windshield
111: Point2D.Double r4
112: = new Point2D.Double(x + width * 5 / 6, y + height / 3);
113: Line2D.Double frontWindshield
114: = new Line2D.Double(r1, r2);
115: Line2D.Double roofTop
116: = new Line2D.Double(r2, r3);
117: Line2D.Double rearWindshield
118: = new Line2D.Double(r3, r4);
119:
120: g2.setColor(color);
121: if (fill)
122: {
123: g2.fill(body);
124: g2.fill(frontTire);
125: g2.fill(rearTire);
126: }
127: else
128: {
129: g2.draw(body);
130: g2.draw(frontTire);
131: g2.draw(rearTire);
132: }
133: g2.draw(frontWindshield);
134: g2.draw(roofTop);
135: g2.draw(rearWindshield);
136: }
137:
138: public static final int DRAW = 0;
139: public static final int FILL = 1;
140:
141: private int x;
142: private int y;
143: private Color color;
144: private int width;
145: private int height;
146: private boolean fill;
147:
148: private static final int DEFAULT_WIDTH = 50;
149: private static final int DEFAULT_HEIGHT = 80;
150: private static final Color DEFAULT_COLOR = Color.BLACK;
151: }