package model.shapes; import javax.swing.*; import java.awt.*; /** * Ellipse shape. * This shape requires two parameters, the radii, 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 Ellipse implements IShape { /** * Radius along the x axis of the oval. */ private int _radiusX; /** * Radius along the y axis of the oval. */ private int _radiusY; /** * Parameterless constructor. * Should just be used to call makeFactory() on the object. */ public Ellipse() { } /** * Regular constructor. * When this constructor is used, the object is fully initialized. * @param radiusX X readius * @param radiusY Y readius */ public Ellipse(int radiusX, int radiusY) { _radiusX = radiusX; _radiusY = radiusY; } /** * Make the factory that can create this kind of IShape. * @return factory to makethis 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; // 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")); } /** * Make the IShape that matches the current parameters. * @return IShape */ public IShape makeShape() { return new Ellipse(Integer.parseInt(_radiusXField.getText()),Integer.parseInt(_radiusYField.getText())); }; }; } /** * Paint this shape. * @param g graphics object */ public void paint(Graphics g) { g.drawOval(-_radiusX,-_radiusY,2*_radiusX,2*_radiusY); } }