    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_CAR_WIDTH;
    018:       height = DEFAULT_CAR_HEIGHT;
    019:    }
    020:
    021:    /**
    022:       Sets the x property.
    023:       @param newValue the new x position
    024:    */
    025:    public void setX(int newValue)
    026:    {
    027:       x = newValue;
    028:       repaint();
    029:    }
    030:
    031:    /**
    032:       Gets the x property.
    033:       @return the x position
    034:    */
    035:    public int getX()
    036:    {
    037:       return x;
    038:    }
    039:
    040:    /**
    041:       Sets the y property.
    042:       @param newValue the new y position
    043:    */
    044:    public void setY(int newValue)
    045:    {
    046:       y = newValue;
    047:       repaint();
    048:    }
    049:
    050:    /**
    051:       Gets the y property.
    052:       @return the y position
    053:    */
    054:    public int getY()
    055:    {
    056:       return y;
    057:    }
    058:
    059:    public void paintComponent(Graphics g)
    060:    {
    061:       super.paintComponent(g);
    062:       Graphics2D g2 = (Graphics2D) g;
    063:       Rectangle2D.Double body
    064:          = new Rectangle2D.Double(x, y + height / 3,
    065:             width - 1, height / 3);
    066:       Ellipse2D.Double frontTire
    067:          = new Ellipse2D.Double(x + width / 6,
    068:             y + height * 2 / 3, height / 3, height / 3);
    069:       Ellipse2D.Double rearTire
    070:          = new Ellipse2D.Double(x + width * 2 / 3,
    071:             y + height * 2 / 3, height / 3, height / 3);
    072:
    073:       // the bottom of the front windshield
    074:       Point2D.Double r1
    075:          = new Point2D.Double(x + width / 6, y + height / 3);
    076:       // the front of the roof
    077:       Point2D.Double r2
    078:          = new Point2D.Double(x + width / 3, y);
    079:       // the rear of the roof
    080:       Point2D.Double r3
    081:          = new Point2D.Double(x + width * 2 / 3, y);
    082:       // the bottom of the rear windshield
    083:       Point2D.Double r4
    084:          = new Point2D.Double(x + width * 5 / 6, y + height / 3);
    085:
    086:       Line2D.Double frontWindshield
    087:          = new Line2D.Double(r1, r2);
    088:       Line2D.Double roofTop
    089:          = new Line2D.Double(r2, r3);
    090:       Line2D.Double rearWindshield
    091:          = new Line2D.Double(r3, r4);
    092:
    093:       g2.draw(body);
    094:       g2.draw(frontTire);
    095:       g2.draw(rearTire);
    096:       g2.draw(frontWindshield);
    097:       g2.draw(roofTop);
    098:       g2.draw(rearWindshield);
    099:    }
    100:
    101:    public Dimension getPreferredSize()
    102:    {
    103:       return new Dimension(DEFAULT_PANEL_WIDTH,
    104:          DEFAULT_PANEL_HEIGHT);
    105:    }
    106:
    107:    private int x;
    108:    private int y;
    109:    private int width;
    110:    private int height;
    111:
    112:    private static final int DEFAULT_CAR_WIDTH = 60;
    113:    private static final int DEFAULT_CAR_HEIGHT = 30;
    114:    private static final int DEFAULT_PANEL_WIDTH = 160;
    115:    private static final int DEFAULT_PANEL_HEIGHT = 130;
    116: }