package model.shapes; import javax.swing.*; import java.awt.*; /** * Rectangle shape. * * @author Mathias Ricken */ public class Rectangle implements IShape { /** * Width of the circle. */ private int _width; /** * Height of the circle. */ private int _height; /** * Parameterless constructor. * Should just be used to call makeFactory() on the object. */ public Rectangle() { } /** * Regular constructor. * When this constructor is used, the object is fully initialized. * @param w width * @param h height */ public Rectangle(int w, int h) { _width = w; _height = h; } /** * 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 width private JTextField _widthField; /// text field for the height private JTextField _heightField; // initializer block { setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); add(new JLabel("Width: ")); add(_widthField = new JTextField("100")); add(new JLabel("Height: ")); add(_heightField = new JTextField("50")); } /** * Create the IShape that matches the current parameters. * @return IShape */ public IShape makeShape() { return new Rectangle(Integer.parseInt(_widthField.getText()),Integer.parseInt(_heightField.getText())); }; }; } /** * Paint this shape. * @param g graphics object */ public void paint(Graphics g) { g.drawRect(-_width/2,-_height/2,_width,_height); } }