setShape(Shape)   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 6
c 0
b 0
f 0
cc 2
rs 10
1
package org.gannacademy.cdf.graphics.geom;
2
3
import org.gannacademy.cdf.graphics.Drawable2D;
4
import org.gannacademy.cdf.graphics.DrawableException;
5
import org.gannacademy.cdf.graphics.ui.DrawingPanel;
6
7
import java.awt.*;
8
import java.awt.geom.RoundRectangle2D;
9
10
/**
11
 * <p>Draw a round rectangle (i.e. a rectangle with round corners)</p>
12
 *
13
 * <p><img src="doc-files/RoundRectangle.png" alt="Round rectangle diagram"></p>
14
 *
15
 * @author <a href="https://github.com/gann-cdf/graphics/issues" target="_blank">Seth Battis</a>
16
 */
17
public class RoundRectangle extends Drawable2D {
18
    /**
19
     * <p>Construct a new round rectangle</p>
20
     *
21
     * <p><img src="doc-files/RoundRectangle.png" alt="Diagram of RoundRectangle parameters"></p>
22
     *
23
     * <p>All window coordinates are measured in pixels, with the X-axis increasing from left to right and the Y-axis
24
     * increasing from top to bottom. All window coordinates exist in the first quadrant.</p>
25
     *
26
     * <p><img src="../doc-files/window-coordinates.png" alt="Diagram of window coordinates"></p>
27
     *
28
     * @param x            coordinate of origin
29
     * @param y            coordinate of origin
30
     * @param width        in pixels
31
     * @param height       in pixels
32
     * @param arcWidth     in pixels, width of the ellipse that defines the round corners
33
     * @param arcHeight    in pixels, height of the ellipse that defines the round corners
34
     * @param drawingPanel on which to draw
35
     */
36
    public RoundRectangle(double x, double y, double width, double height, double arcWidth, double arcHeight, DrawingPanel drawingPanel) {
37
        try {
38
            setShape(new RoundRectangle2D.Double(x, y, width, height, arcWidth, arcHeight));
39
            setDrawingPanel(drawingPanel);
40
        } catch (DrawableException e) {
41
            System.err.println(e.getMessage());
42
            e.printStackTrace();
0 ignored issues
show
Best Practice introduced by
Throwable.printStackTrace writes to the console which might not be available at runtime. Using a logger is preferred.
Loading history...
43
        }
44
    }
45
46
    protected RoundRectangle2D getShapeAsRoundRectangle() {
47
        return (RoundRectangle2D) getShape();
48
    }
49
50
    @Override
51
    public void setShape(Shape shape) throws DrawableException {
52
        if (shape instanceof RoundRectangle2D) {
53
            super.setShape(shape);
54
        } else {
55
            throw new DrawableException("Attempt to set RoundRectangle's underlying shape to a non-RoundRectangle2D instance");
56
        }
57
    }
58
59
    /**
60
     * Width of ellipse defining corners
61
     *
62
     * @return Arc width
63
     * @see RoundRectangle#getArcWidth()
64
     */
65
    public double getArcWidth() {
66
        return getShapeAsRoundRectangle().getArcWidth();
67
    }
68
69
    /**
70
     * Height of ellipse defining corners
71
     *
72
     * @return Arc height
73
     * @see RoundRectangle#getArcHeight()
74
     */
75
    public double getArcHeight() {
76
        return getShapeAsRoundRectangle().getArcHeight();
77
    }
78
}
79