001package horstmann.ch07_carbean;
002import java.awt.Dimension;
003import java.awt.Graphics;
004import java.awt.Graphics2D;
005import java.awt.geom.Ellipse2D;
006import java.awt.geom.Line2D;
007import java.awt.geom.Point2D;
008import java.awt.geom.Rectangle2D;
009
010import javax.swing.JComponent;
011
012/**
013   A component that draws a car shape.
014 */
015@SuppressWarnings("serial")
016public class CarBean extends JComponent
017{
018        /**
019      Constructs a default car bean.
020         */
021        public CarBean()
022        {
023                x = 0;
024                y = 0;
025                width = DEFAULT_CAR_WIDTH;
026                height = DEFAULT_CAR_HEIGHT;
027        }
028
029        /**
030      Sets the x property.
031      @param newValue the new x position
032         */
033        public void setX(int newValue)
034        {
035                x = newValue;
036                repaint();
037        }
038
039        /**
040      Gets the x property.
041      @return the x position
042         */
043        public int getX()
044        {
045                return x;
046        }
047
048        /**
049      Sets the y property.
050      @param newValue the new y position
051         */
052        public void setY(int newValue)
053        {
054                y = newValue;
055                repaint();
056        }
057
058        /**
059      Gets the y property.
060      @return the y position
061         */
062        public int getY()
063        {
064                return y;
065        }
066
067        public void paintComponent(Graphics g)
068        {
069                Graphics2D g2 = (Graphics2D) g;
070                Rectangle2D.Double body
071                = new Rectangle2D.Double(x, y + height / 3,
072                                width - 1, height / 3);
073                Ellipse2D.Double frontTire
074                = new Ellipse2D.Double(x + width / 6,
075                                y + height * 2 / 3, height / 3, height / 3);
076                Ellipse2D.Double rearTire
077                = new Ellipse2D.Double(x + width * 2 / 3,
078                                y + height * 2 / 3, height / 3, height / 3);
079
080                // The bottom of the front windshield
081                Point2D.Double r1
082                = new Point2D.Double(x + width / 6, y + height / 3);
083                // The front of the roof
084                Point2D.Double r2
085                = new Point2D.Double(x + width / 3, y);
086                // The rear of the roof
087                Point2D.Double r3
088                = new Point2D.Double(x + width * 2 / 3, y);
089                // The bottom of the rear windshield
090                Point2D.Double r4
091                = new Point2D.Double(x + width * 5 / 6, y + height / 3);
092
093                Line2D.Double frontWindshield
094                = new Line2D.Double(r1, r2);
095                Line2D.Double roofTop
096                = new Line2D.Double(r2, r3);
097                Line2D.Double rearWindshield
098                = new Line2D.Double(r3, r4);
099
100                g2.draw(body);
101                g2.draw(frontTire);
102                g2.draw(rearTire);
103                g2.draw(frontWindshield);
104                g2.draw(roofTop);
105                g2.draw(rearWindshield);
106        }
107
108        public Dimension getPreferredSize()
109        {
110                return new Dimension(DEFAULT_PANEL_WIDTH,
111                                DEFAULT_PANEL_HEIGHT);
112        }
113
114        private int x;
115        private int y;
116        private int width;
117        private int height;
118
119        private static final int DEFAULT_CAR_WIDTH = 60;
120        private static final int DEFAULT_CAR_HEIGHT = 30;
121        private static final int DEFAULT_PANEL_WIDTH = 160;
122        private static final int DEFAULT_PANEL_HEIGHT = 130;
123}