gann-cdf /
graphics
| 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.Ellipse2D; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * <p>Draw an ellipse</p> |
||
| 12 | * |
||
| 13 | * <p><img src="doc-files/Ellipse.png" alt="Ellipse diagram"></p> |
||
| 14 | * |
||
| 15 | * <p>Ellipses are drawn inscribed within their rectangular bounding box.</p> |
||
| 16 | * |
||
| 17 | * @author <a href="https://github.com/gann-cdf/graphics/issues" target="_blank">Seth Battis</a> |
||
| 18 | */ |
||
| 19 | public class Ellipse extends Drawable2D { |
||
| 20 | /** |
||
| 21 | * <p>Construct a new ellipse</p> |
||
| 22 | * |
||
| 23 | * <p><img src="doc-files/Ellipse.png" alt="Diagram of Ellipse parameters"></p> |
||
| 24 | * |
||
| 25 | * <p>All window coordinates are measured in pixels, with the X-axis increasing from left to right and the Y-axis |
||
| 26 | * increasing from top to bottom. All window coordinates exist in the first quadrant.</p> |
||
| 27 | * |
||
| 28 | * <p><img src="../doc-files/window-coordinates.png" alt="Diagram of window coordinates"></p> |
||
| 29 | * |
||
| 30 | * @param x coordinate of origin |
||
| 31 | * @param y coordinate of origin |
||
| 32 | * @param width in pixels |
||
| 33 | * @param height in pixels |
||
| 34 | * @param drawingPanel on which to draw |
||
| 35 | */ |
||
| 36 | public Ellipse(double x, double y, double width, double height, DrawingPanel drawingPanel) { |
||
| 37 | try { |
||
| 38 | setShape(new Ellipse2D.Double(x, y, width, height)); |
||
| 39 | setDrawingPanel(drawingPanel); |
||
| 40 | } catch (DrawableException e) { |
||
| 41 | System.err.println(e.getMessage()); |
||
| 42 | e.printStackTrace(); |
||
|
0 ignored issues
–
show
Best Practice
introduced
by
Loading history...
|
|||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | @Override |
||
| 47 | public void setShape(Shape shape) throws DrawableException { |
||
| 48 | if (shape instanceof Ellipse2D) { |
||
| 49 | super.setShape(shape); |
||
| 50 | } else { |
||
| 51 | throw new DrawableException("Attempt to set Ellipse's underlying shape to a non-Ellipse2D instance"); |
||
| 52 | } |
||
| 53 | } |
||
| 54 | } |
||
| 55 |