import java.util.*;
import java.awt.*;
import java.applet.*;


public class Cal extends Applet implements Runnable {

//	GridBagLayout theLayout;
	Canvas hereCanvas;
	Button touchMe;
	int w, h;

	Color white, black;
	int posx, posy;

//	Image offscreen;
//	Dimension offscreensize;
//	Graphics offgraphics;

	Thread design;
	int fnum = -1;
	long time1, time2, timeDiff;
	int rad;
	int timeStep, temp1, temp2;


	public String getAppletInfo() {
		return "Thales seriEs: carpetPoetry, by joey beRzowski";
     } // getAppletInfo

      public void start() {
		repaint();
		if (design == null) {
			design = new Thread(this);
			design.start();
		}
      } // start


      public void stop() {
		if ((design != null) && design.isAlive())
			design.stop();
		design = null;
      } // stop


      
	  public void destroy() {
		if ((design != null) && design.isAlive())
			design.stop();
		design = null;
      } // destroy


    
	  public void run() {
       
    //  Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

        long startTime = System.currentTimeMillis();

        while (Thread.currentThread() == design) {
            fnum++;

            repaint();
			try {Thread.sleep(10);}
			catch (InterruptedException e) {}
           
        }
    }


    public void init() {
		white =  Color.white;
		black =  Color.black;

        setBackground(white);
        setForeground(black);

		GridBagLayout theLayout	= new GridBagLayout();
		this.setLayout(theLayout);

		hereCanvas = new Canvas();
		touchMe = new Button("[]");
		touchMe.setBackground(white);
		
		theLayout.setConstraints(hereCanvas, GBconstraints.constrain(0,  0, 1, 1, 1.0, 0.99, 1, 10, 0,0,0,0));
		theLayout.setConstraints(touchMe,   GBconstraints.constrain(0,  1, 1, 1, 1.0, 0.01, 1, 10, 0,0,0,0));

		add(hereCanvas);
		add(touchMe);
		
		repaint();
	}

   
	public void paint(Graphics g) { 
        Dimension d = size();
		w=d.width;
		h=d.height;

              
	/*	if ((offscreen == null) || (d.width != offscreensize.width) || (d.height != offscreensize.height)) {

			offscreen = createImage(d.width, d.height);
			offscreensize = d;
			offgraphics = offscreen.getGraphics();
		}
		offgraphics.setColor(white);
		offgraphics.fillRect(0,0,d.width,d.height);

   		offgraphics.setColor(black);

		g.drawImage(offscreen, 0, 0, null);
	 */
	}
  
        
   public void update(Graphics g) { 
        
        
   }

        
   public boolean action(Event e, Object arg) {
               
	   if (e.target == touchMe) {        
		   Graphics g = hereCanvas.getGraphics();
		   g.setColor(white);			
		   g.fillRect(0, 0, w, h);			
		   return true; 
	   } else return false;
        
   }

        
   public boolean mouseDown(Event e, int x, int y) {
        
	   this.posx = x; 		
	   this.posy = y;  
	   time1 = e.when;
//	   temp1 = fnum;
	   return true;
       
   }

       
   public boolean mouseDrag(Event e, int x, int y) {
        Graphics g = hereCanvas.getGraphics();
		g.setColor(black);
		g.drawLine(posx,posy,x,y);
		g.drawLine(posx+1,posy,x-1,y);
		g.drawLine(posx,posy-1,x,y+1);
	

		time2 = e.when;
		timeDiff = time2-time1;

		int t = (int)(timeDiff);

		int sx = Math.abs(Math.abs(x)-Math.abs(posx));

		int sy = Math.abs(Math.abs(y)-Math.abs(posy));

		int radx = t/(5*(sx+1)) + 1;

		int rady = t/(5*(sy+1)) + 1;


		g.fillOval(posx-radx/2,posy-rady/2,radx,rady);

	//	temp2 = fnum;
	//	timeStep=temp1-temp2;
	//	g.fillOval(posx,posy,timeStep,timeStep);

	//	int s = Math.abs(Math.abs(x)-Math.abs(posx))+Math.abs(Math.abs(y)-Math.abs(posy));
	//	g.fillOval(posx,posy,s,s);

		posx=x;
		posy=y;

		time1=time2;
	//	temp1=temp2;
		return true;
   }

}

class GBconstraints {

  	public static GridBagConstraints constrain(int x, int y, int w, int h, 
		double wtx, double wty, int fill, int anchor, int top, int left, 
		int bottom, int right) {
		GridBagConstraints c = new GridBagConstraints();
		c.gridx=x;
		c.gridy=y;
		c.gridwidth=w;
		c.gridheight=h;
		c.weightx=wtx;
		c.weighty=wty;
		c.fill=fill;
		c.anchor=anchor;
		if (top+left+bottom+right>0) 
			c.insets = new Insets (top, left, bottom, right);

		return c;
	}

}
