gann-cdf /
graphics
| 1 | package org.gannacademy.cdf.graphics.geom; |
||
| 2 | |||
| 3 | import org.gannacademy.cdf.graphics.Drawable; |
||
| 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.Line2D; |
||
| 9 | import java.awt.geom.Point2D; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * <p>Draw a line</p> |
||
| 13 | * |
||
| 14 | * <p><img src="doc-files/Line.png" alt="Line diagram"></p> |
||
| 15 | * |
||
| 16 | * @author <a href="https://github.com/gann-cdf/graphics/issues" target="_blank">Seth Battis</a> |
||
| 17 | */ |
||
| 18 | public class Line extends Drawable { |
||
| 19 | /** |
||
| 20 | * <p>Construct a new line</p> |
||
| 21 | * |
||
| 22 | * <p><img src="doc-files/Line.png" alt="Diagram of Line parameters"></p> |
||
| 23 | * |
||
| 24 | * <p>All window coordinates are measured in pixels, with the X-axis increasing from left to right and the Y-axis |
||
| 25 | * increasing from top to bottom. All window coordinates exist in the first quadrant.</p> |
||
| 26 | * |
||
| 27 | * <p><img src="../doc-files/window-coordinates.png" alt="Diagram of window coordinates"></p> |
||
| 28 | * |
||
| 29 | * @param x1 coordinate of starting point |
||
| 30 | * @param y1 coordinate of starting point |
||
| 31 | * @param x2 coordinate of ending point |
||
| 32 | * @param y2 coordinate of ending point |
||
| 33 | * @param drawingPanel on which to draw |
||
| 34 | */ |
||
| 35 | public Line(double x1, double y1, double x2, double y2, DrawingPanel drawingPanel) { |
||
| 36 | try { |
||
| 37 | setShape(new Line2D.Double(x1, y1, x2, y2)); |
||
| 38 | setDrawingPanel(drawingPanel); |
||
| 39 | } catch (DrawableException e) { |
||
| 40 | System.err.println(e.getMessage()); |
||
| 41 | e.printStackTrace(); |
||
|
0 ignored issues
–
show
Best Practice
introduced
by
Loading history...
|
|||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * <p>Construct a new line</p> |
||
| 47 | * |
||
| 48 | * <p><img src="doc-files/Line.png" alt="Diagram of Line parameters"></p> |
||
| 49 | * |
||
| 50 | * @param p1 Starting point |
||
| 51 | * @param p2 Ending point |
||
| 52 | * @param drawingPanel on which to draw |
||
| 53 | */ |
||
| 54 | public Line(Point2D p1, Point2D p2, DrawingPanel drawingPanel) { |
||
| 55 | try { |
||
| 56 | setShape(new Line2D.Double(p1, p2)); |
||
| 57 | setDrawingPanel(drawingPanel); |
||
| 58 | } catch (DrawableException e) { |
||
| 59 | e.printStackTrace(); |
||
|
0 ignored issues
–
show
|
|||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return Underlying {@link Line2D} geometry |
||
| 65 | */ |
||
| 66 | protected Line2D getShapeAsLine() { |
||
| 67 | return (Line2D) getShape(); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * <p>Replace the underlying {@link Line2D} geometry</p> |
||
| 72 | * |
||
| 73 | * <p>Changing the geometry leaves other characteristics (stroke color, style) unchanged.</p> |
||
| 74 | * |
||
| 75 | * @param shape of new geometry (must be and instance of {@link Line2D}) |
||
| 76 | * @throws DrawableException if {@code shape} is not an instance of {@link Line2D} |
||
| 77 | */ |
||
| 78 | @Override |
||
| 79 | public void setShape(Shape shape) throws DrawableException { |
||
| 80 | if (shape instanceof Line2D) { |
||
| 81 | super.setShape(shape); |
||
| 82 | } else { |
||
| 83 | throw new DrawableException("Attempt to set Line's underlying shape to a non-Line2D instance"); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | @Override |
||
| 88 | public void setWidth(double width) { |
||
| 89 | // FIXME this feels hacktacular |
||
|
0 ignored issues
–
show
|
|||
| 90 | double scale = width / getWidth(); |
||
| 91 | setLine( |
||
| 92 | getX() + (getX1() - getX()) * scale, getY1(), |
||
| 93 | getX() + (getX2() - getX()) * scale, getY2() |
||
| 94 | ); |
||
| 95 | } |
||
| 96 | |||
| 97 | @Override |
||
| 98 | public void setHeight(double height) { |
||
| 99 | // FIXME this feels hacktacular |
||
|
0 ignored issues
–
show
|
|||
| 100 | double scale = height / getHeight(); |
||
| 101 | setLine( |
||
| 102 | getX1(), getY() + (getY1() - getY()) * scale, |
||
| 103 | getX2(), getY() + (getY2() - getY()) * scale |
||
| 104 | ); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Starting point |
||
| 109 | * |
||
| 110 | * @return Starting point |
||
| 111 | * @see Line2D#getP1() |
||
| 112 | */ |
||
| 113 | public Point2D getP1() { |
||
| 114 | return getShapeAsLine().getP1(); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Ending point |
||
| 119 | * |
||
| 120 | * @return Ending point |
||
| 121 | * @see Line2D#getP2() |
||
| 122 | */ |
||
| 123 | public Point2D getP2() { |
||
| 124 | return getShapeAsLine().getP2(); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Replace the underlying geometry |
||
| 129 | * |
||
| 130 | * @param x1 coordinate of the starting point |
||
| 131 | * @param y1 coordinate of the starting point |
||
| 132 | * @param x2 coordinate of the ending point |
||
| 133 | * @param y2 coordinate of the ending point |
||
| 134 | */ |
||
| 135 | public void setLine(double x1, double y1, double x2, double y2) { |
||
| 136 | getShapeAsLine().setLine(x1, y1, x2, y2); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * X-coordinate of starting point |
||
| 141 | * |
||
| 142 | * @return X-coordinate of starting point |
||
| 143 | * @see Line2D#getX1() |
||
| 144 | */ |
||
| 145 | public double getX1() { |
||
| 146 | return getShapeAsLine().getX1(); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Y-coordinate of starting point |
||
| 151 | * |
||
| 152 | * @return Y-coordinate of starting point |
||
| 153 | * @see Line2D#getY1() |
||
| 154 | */ |
||
| 155 | public double getY1() { |
||
| 156 | return getShapeAsLine().getY1(); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * X-coordinate of ending point |
||
| 161 | * |
||
| 162 | * @return X-coordinate of ending point |
||
| 163 | * @see Line2D#getX2() |
||
| 164 | */ |
||
| 165 | public double getX2() { |
||
| 166 | return getShapeAsLine().getX2(); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Y-coordinate of ending point |
||
| 171 | * |
||
| 172 | * @return Y-coordinate of ending point |
||
| 173 | * @see Line2D#getY2() |
||
| 174 | */ |
||
| 175 | public double getY2() { |
||
| 176 | return getShapeAsLine().getY2(); |
||
| 177 | } |
||
| 178 | |||
| 179 | @Override |
||
| 180 | public void translate(double dx, double dy) { |
||
| 181 | getShapeAsLine().setLine(getX1() + dx, getY1() + dy, getX2() + dx, getY2() + dy); |
||
| 182 | } |
||
| 183 | |||
| 184 | @Override |
||
| 185 | public void setLocation(double x, double y) { |
||
| 186 | getShapeAsLine().setLine(x, y, x + getX1() - getX2(), y + getY1() - getY2()); |
||
| 187 | } |
||
| 188 | } |
||
| 189 |