summaryrefslogtreecommitdiffstats
path: root/docs/teaching/exercises/figures/Box.java
blob: b792e0de9e4b33ecab5ca9e86b7769f0d66af496 (plain)
1
2
3
4
5
6
7
8
9
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
/* *******************************************************************
 * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
 * All rights reserved.
 * This program and the accompanying materials are made available
 * under the terms of the Common Public License v1.0
 * which accompanies this distribution and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 *
 * Contributors:
 *     PARC     initial implementation
 * ******************************************************************/


package figures;

import java.awt.*;
import java.awt.geom.*;

public class Box extends ShapeFigureElement {
    private Point _p0;
    private Point _p1;
    private Point _p2;
    private Point _p3;

    public Box(int x0, int y0, int width, int height) {
        _p0 = new Point(x0, y0);
        _p1 = new Point(x0+width, y0);
        _p2 = new Point(x0+width, y0+height);
        _p3 = new Point(x0, y0+height);
    }

    public Point getP0() { return _p0; }
    public Point getP1() { return _p1; }
    public Point getP2() { return _p2; }
    public Point getP3() { return _p3; }

    public void move(int dx, int dy) {
        _p0.move(dx, dy);
        _p1.move(dx, dy);
        _p2.move(dx, dy);
        _p3.move(dx, dy);
    }

    public void checkBoxness() {
        if ((_p0.getX() == _p3.getX()) &&
            (_p1.getX() == _p2.getX()) &&
            (_p0.getY() == _p1.getY()) &&
            (_p2.getY() == _p3.getY()))
          return;
        throw new IllegalStateException("This is not a square.");
    }

    public String toString() {
        return "Box(" + _p0 + ", " + _p1 + ", " + _p2 + ", " + _p3 + ")";
    }

    public Shape getShape() {
        return new Rectangle(getP1().getX(),
                             getP1().getY(),
                             getP3().getX() - getP1().getX(),
                             getP3().getY() - getP1().getY());
    }
}