SE450: Packaging a Bean [84/86] Previous pageContentsNext page

file:horstmann/ch07_carbean/CarBean.java [source] [doc-public] [doc-private]
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package horstmann.ch07_carbean;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;

/**
   A component that draws a car shape.
 */
@SuppressWarnings("serial")
public class CarBean extends JComponent
{
  /**
      Constructs a default car bean.
   */
  public CarBean()
  {
    x = 0;
    y = 0;
    width = DEFAULT_CAR_WIDTH;
    height = DEFAULT_CAR_HEIGHT;
  }

  /**
      Sets the x property.
      @param newValue the new x position
   */
  public void setX(int newValue)
  {
    x = newValue;
    repaint();
  }

  /**
      Gets the x property.
      @return the x position
   */
  public int getX()
  {
    return x;
  }

  /**
      Sets the y property.
      @param newValue the new y position
   */
  public void setY(int newValue)
  {
    y = newValue;
    repaint();
  }

  /**
      Gets the y property.
      @return the y position
   */
  public int getY()
  {
    return y;
  }

  public void paintComponent(Graphics g)
  {
    Graphics2D g2 = (Graphics2D) g;
    Rectangle2D.Double body
    = new Rectangle2D.Double(x, y + height / 3,
        width - 1, height / 3);
    Ellipse2D.Double frontTire
    = new Ellipse2D.Double(x + width / 6,
        y + height * 2 / 3, height / 3, height / 3);
    Ellipse2D.Double rearTire
    = new Ellipse2D.Double(x + width * 2 / 3,
        y + height * 2 / 3, height / 3, height / 3);

    // The bottom of the front windshield
    Point2D.Double r1
    = new Point2D.Double(x + width / 6, y + height / 3);
    // The front of the roof
    Point2D.Double r2
    = new Point2D.Double(x + width / 3, y);
    // The rear of the roof
    Point2D.Double r3
    = new Point2D.Double(x + width * 2 / 3, y);
    // The bottom of the rear windshield
    Point2D.Double r4
    = new Point2D.Double(x + width * 5 / 6, y + height / 3);

    Line2D.Double frontWindshield
    = new Line2D.Double(r1, r2);
    Line2D.Double roofTop
    = new Line2D.Double(r2, r3);
    Line2D.Double rearWindshield
    = new Line2D.Double(r3, r4);

    g2.draw(body);
    g2.draw(frontTire);
    g2.draw(rearTire);
    g2.draw(frontWindshield);
    g2.draw(roofTop);
    g2.draw(rearWindshield);
  }

  public Dimension getPreferredSize()
  {
    return new Dimension(DEFAULT_PANEL_WIDTH,
        DEFAULT_PANEL_HEIGHT);
  }

  private int x;
  private int y;
  private int width;
  private int height;

  private static final int DEFAULT_CAR_WIDTH = 60;
  private static final int DEFAULT_CAR_HEIGHT = 30;
  private static final int DEFAULT_PANEL_WIDTH = 160;
  private static final int DEFAULT_PANEL_HEIGHT = 130;
}
file:horstmann/ch07_carbean/CarBean.mf

Previous pageContentsNext page