package model.shapes; import javax.swing.*; import java.awt.*; /** * Arc of an ellipse shape * This shape requires four parameters, the radii, start angle and arc angle, for the regular constructor. * It can be instantiated using a parameterless constructor, but then only makeFactory() may be used to obtain * a factory that can create a fully initialized Ellipse object. * * @author Mathias Ricken */ public class EllipticalArc implements IShape { /** * Radius along the x axis of the oval. */ private int _radiusX; /** * Radius along the y axis of the oval. */ private int _radiusY; /** * Start angle. */ private int _startAngle; /** * Arc angle. */ private int _arcAngle; /** * Parameterless constructor. * Should just be used to call makeFactory() on the object. */ public EllipticalArc() { } /** * Regular constructor. * When this constructor is used, the object is fully initialized. * @param radiusX X readius * @param radiusY Y readius * @param startAngle start angle in degrees * @param arcAngle arc angle in degrees */ public EllipticalArc(int radiusX, int radiusY, int startAngle, int arcAngle) { _radiusX = radiusX; _radiusY = radiusY; _startAngle = startAngle; _arcAngle = arcAngle; } /** * Make the factory that can create this kind of IShape. * @return factory to make this IShape */ public AShapeFactory makeFactory() { // anonymously create factory that can be used as settings panel return new AShapeFactory(getClass().getName()) { /// text field for the X radius private JTextField _radiusXField; /// text field for the Y radius private JTextField _radiusYField; /// text field for the start degree private JTextField _startAngleField; /// text field for the end degree private JTextField _arcAngleField; // initializer block { setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); add(new JLabel("X Radius: ")); add(_radiusXField = new JTextField("50")); add(new JLabel("Y Radius: ")); add(_radiusYField = new JTextField("80")); add(new JLabel("Start Angle: ")); add(_startAngleField = new JTextField("45")); add(new JLabel("Arc Angle: ")); add(_arcAngleField = new JTextField("90")); } /** * Create the IShape that matches the current parameters. * @return IShape */ public IShape makeShape() { return new EllipticalArc(Integer.parseInt(_radiusXField.getText()), Integer.parseInt(_radiusYField.getText()), Integer.parseInt(_startAngleField.getText()), Integer.parseInt(_arcAngleField.getText())); }; }; } /** * Paint this shape. * @param g graphics object */ public void paint(Graphics g) { g.drawArc(-_radiusX,-_radiusY,2*_radiusX,2*_radiusY,_startAngle,_arcAngle); g.drawLine(0,0, (int)Math.round(_radiusX*Math.cos(Math.PI/180.f*_startAngle)), (int)Math.round(-_radiusY*Math.sin(Math.PI/180.f*_startAngle))); g.drawLine(0,0, (int)Math.round(_radiusX*Math.cos(Math.PI/180.f*(_startAngle+_arcAngle))), (int)Math.round(-_radiusY*Math.sin(Math.PI/180.f*(_startAngle+_arcAngle)))); } }