SE450: Template Method [35/65] Previous pageContentsNext page

file:horstmann/ch06_scene2/SelectableShape.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package horstmann.ch06_scene2;
import java.awt.Graphics2D;

/**
   A shape that manages its selection state.
 */
public abstract class SelectableShape implements SceneShape
{
  public void setSelected(boolean b)
  {
    selected = b;
  }

  public boolean isSelected()
  {
    return selected;
  }

  public void drawSelection(Graphics2D g2)
  {
    translate(1, 1);
    draw(g2);
    translate(1, 1);
    draw(g2);
    translate(-2, -2);
  }

  private boolean selected;
}

file:horstmann/ch06_scene2/HouseShape.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package horstmann.ch06_scene2;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/**
   A house shape.
 */
public class HouseShape extends SelectableShape
{
  /**
      Constructs a house shape.
      @param x the left of the bounding rectangle
      @param y the top of the bounding rectangle
      @param width the width of the bounding rectangle
   */
  public HouseShape(int x, int y, int width)
  {
    this.x = x;
    this.y = y;
    this.width = width;
  }

  public void draw(Graphics2D g2)
  {
    Rectangle2D.Double base
    = new Rectangle2D.Double(x, y + width, width, width);

    // The left bottom of the roof
    Point2D.Double r1
    = new Point2D.Double(x, y + width);
    // The top of the roof
    Point2D.Double r2
    = new Point2D.Double(x + width / 2, y);
    // The right bottom of the roof
    Point2D.Double r3
    = new Point2D.Double(x + width, y + width);

    Line2D.Double roofLeft
    = new Line2D.Double(r1, r2);
    Line2D.Double roofRight
    = new Line2D.Double(r2, r3);

    g2.draw(base);
    g2.draw(roofLeft);
    g2.draw(roofRight);
  }

  public boolean contains(Point2D p)
  {
    return x <= p.getX() && p.getX() <= x + width
        && y <= p.getY() && p.getY() <= y + 2 * width;
  }

  public void translate(int dx, int dy)
  {
    x += dx;
    y += dy;
  }

  private int x;
  private int y;
  private int width;
}

Previous pageContentsNext page