package uiintro;

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


public class PaintDemo extends JFrame {

  public PaintDemo() {
    this.setSize(400, 400);
    this.getContentPane().setBackground(Color.white);
    this.getContentPane().setLayout(null);
    this.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent me) {
        newThingy(me.getX(), me.getY());
      }
    });
    this.setVisible(true);
  }

  public void newThingy(int x, int y) {
    this.getContentPane().add(new Thingy(x, y));
    this.repaint();
  }
}

class Thingy extends JComponent {

  private int width = 50, height = 50;
  private Color foregroundColor;

  public Thingy () {
    super();
  }

  public Thingy(int x, int y) {
    this();
    foregroundColor = Color.red;
    this.setSize(width, height);
    this.setLocation(x, y);
  }

  public boolean contains(int x, int y) {
    return x >= 0 && x < width && y >= 0 && y < width;
  }


  public void paintComponent(Graphics g) {
    g.setColor(foregroundColor);
    g.fillOval(0, 0, width / 2, height);
    g.fillRect(20, 12, width, height/2);
  }
}
