Source of CarBean.java


  1: import java.awt.*;
  2: import java.awt.geom.*;
  3: import javax.swing.*;

  5: /**
  6:    A component that draws a car shape.
  7: */
  8: public class CarBean extends JComponent
  9: {
 10:    /**
 11:       Constructs a default car bean.
 12:    */
 13:    public CarBean()
 14:    {
 15:       x = 0;
 16:       y = 0;
 17:       width = DEFAULT_CAR_WIDTH;
 18:       height = DEFAULT_CAR_HEIGHT;      
 19:    }

 21:    /**
 22:       Sets the x property.
 23:       @param newValue the new x position
 24:    */
 25:    public void setX(int newValue)
 26:    {
 27:       x = newValue;
 28:       repaint();
 29:    }

 31:    /**
 32:       Gets the x property.
 33:       @return the x position
 34:    */
 35:    public int getX()
 36:    {
 37:       return x;
 38:    }

 40:    /**
 41:       Sets the y property.
 42:       @param newValue the new y position
 43:    */
 44:    public void setY(int newValue)
 45:    {
 46:       y = newValue;
 47:       repaint();
 48:    }

 50:    /**
 51:       Gets the y property.
 52:       @return the y position
 53:    */
 54:    public int getY()
 55:    {
 56:       return y;
 57:    }

 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);

 72:       // The bottom of the front windshield
 73:       Point2D.Double r1
 74:          = new Point2D.Double(x + width / 6, y + height / 3);
 75:       // The front of the roof
 76:       Point2D.Double r2
 77:          = new Point2D.Double(x + width / 3, y);
 78:       // The rear of the roof
 79:       Point2D.Double r3
 80:          = new Point2D.Double(x + width * 2 / 3, y);
 81:       // The bottom of the rear windshield
 82:       Point2D.Double r4
 83:          = new Point2D.Double(x + width * 5 / 6, y + height / 3);

 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);

 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:    }

100:    public Dimension getPreferredSize() 
101:    { 
102:       return new Dimension(DEFAULT_PANEL_WIDTH, 
103:          DEFAULT_PANEL_HEIGHT);
104:    }

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: }