import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CircleView extends JFrame implements ActionListener {
  private CircleModel model;

  public CircleView(CircleModel model) {
    // Set the model to be used.
    this.model = model;
    
    setTitle("View");
    setSize(500, 200);
    setLocation(200, 200);

    ViewPanel panel = new ViewPanel(model);
    add(panel);

    if (model != null)
      // Register the view as listener for the model
      model.addActionListener(this);

    repaint();
  }

  // Model notifications (events) trigger a redraw.
  public void actionPerformed(ActionEvent actionEvent) {
    repaint();
  }
}


class ViewPanel extends JPanel {
  private CircleModel model;

  public ViewPanel(CircleModel model) {
      this.model = model;
  }

  // Draw a circle in the middle of the passed graphics object
  // (roughly speaking, the "canvas" for the window).
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    if (model == null) return;

    g.setColor(model.getColor());

    int xCenter = getWidth() / 2;
    int yCenter = getHeight() / 2;
    int radius = (int)model.getRadius();

    if (model.isFilled()) {
      g.fillOval(xCenter - radius, yCenter - radius,
        2 * radius, 2 * radius);
    }
    else {
      g.drawOval(xCenter - radius, yCenter - radius,
        2 * radius, 2 * radius);
    }
  }
}
