/**
 * CirclesApplet1.java
 * Click on the applet to draw the circles.
 * This version just uses the system provided AWT GUI thread.
 * See CircleApplet2.java for example using its own thread.
 * See CircleApplet1a.java  for a failed attempt of use
 * repaint() in a monothreaded applet.
 */

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class CirclesApplet1 extends Applet {
    Canvas canvas;

    public void init() {
   setLayout(new GridLayout(1,1));
   canvas = new MyCanvas();
   add(canvas);
    }

}

class MyCanvas extends Canvas {

    Point currentCenter;
    Dimension canvasSize;
    final int RADIUS = 20;

    public MyCanvas() {
   setBackground(Color.yellow);


   currentCenter =  new Point(0,0);
   addMouseListener(new HandleMouse());
    }

    public class HandleMouse extends MouseAdapter {
   public void mouseClicked(MouseEvent e) {

       canvasSize = getSize();   // size determined by GridLayout manager and applet
       int x0 = canvasSize.width / 4;
       int y0 = canvasSize.height / 4;
       for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++) {
          int x = x0 * (i+1);
          int y = y0 * (j+1);
          currentCenter = new Point(x,y);

          // in this loop, repaint() clogs the AWT thread with
          // requests to update and paint. Requests are combined
          // but the result is that parts of the drawing may be missing.
          repaint();
      }
       }
    }

    private void drawFillCircle(Graphics g, Color c, Point center, int radius) {
   g.setColor(c);
   int leftX = center.x - radius;
   int leftY = center.y - radius;
   g.fillOval(leftX, leftY, 2*radius, 2*radius);
    }

    public void paint(Graphics g) {
   drawFillCircle(g, Color.red, currentCenter, RADIUS);
    }

    public void update(Graphics g) {
   paint(g);
    }

}