001: import java.awt.*;
002: import java.awt.geom.*;
003: import javax.swing.*;
004: 
005: /**
006:    A component that draws a car shape.
007: */
008: public class CarBean extends JComponent
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:       Graphics2D g2 = (Graphics2D) g;
062:       Rectangle2D.Double body
063:          = new Rectangle2D.Double(x, y + height / 3, 
064:             width - 1, height / 3);
065:       Ellipse2D.Double frontTire
066:          = new Ellipse2D.Double(x + width / 6, 
067:             y + height * 2 / 3, height / 3, height / 3);
068:       Ellipse2D.Double rearTire
069:          = new Ellipse2D.Double(x + width * 2 / 3, 
070:             y + height * 2 / 3, height / 3, height / 3);
071: 
072:       // The bottom of the front windshield
073:       Point2D.Double r1
074:          = new Point2D.Double(x + width / 6, y + height / 3);
075:       // The front of the roof
076:       Point2D.Double r2
077:          = new Point2D.Double(x + width / 3, y);
078:       // The rear of the roof
079:       Point2D.Double r3
080:          = new Point2D.Double(x + width * 2 / 3, y);
081:       // The bottom of the rear windshield
082:       Point2D.Double r4
083:          = new Point2D.Double(x + width * 5 / 6, y + height / 3);
084: 
085:       Line2D.Double frontWindshield
086:          = new Line2D.Double(r1, r2);
087:       Line2D.Double roofTop
088:          = new Line2D.Double(r2, r3);
089:       Line2D.Double rearWindshield
090:          = new Line2D.Double(r3, r4);
091: 
092:       g2.draw(body);
093:       g2.draw(frontTire);
094:       g2.draw(rearTire);
095:       g2.draw(frontWindshield);
096:       g2.draw(roofTop);
097:       g2.draw(rearWindshield);
098:    }
099: 
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: }