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


public class BresenhamCircleMain extends Applet implements MouseInput {

    Grid view;
    int mode = 0;
    public static Color red = new Color(255, 0, 0);
    int xorig=0, yorig=0;

    public BresenhamCircleMain() {}

    public void init() {
	view = new Grid(400,400, 41,41);
	view.addMouseInput(this);
	view.drawGrid = true;
	view.drawAxes = true;

	setLayout(new GridLayout(1,1));
	add(view);

	view.init();
	setVisible(true);
    }

    static Color darkGreen = new Color(0, 200, 0);
    
    public void mouseUp(int x, int y) {
	//do nothing
	//if in mode 1 draw the line
	if (mode==1) {
	    view.clear();
	    //view.line(xorig, yorig, x, y, red);
	    PointInt p1 = new PointInt(xorig,yorig);
	    PointInt p2 = new PointInt(x,y);
	    System.out.println("origin = (" + p1.x() + "," + p1.y() + ")");
	    System.out.println("radius = " + 
			       Math.sqrt((p2.x()-p1.x())*(p2.x()-p1.x())
					 + (p2.y()-p1.y())*(p2.y()-p1.y()) ));
	    Bresenham.BresenhamCircleAlgorithm(p1, p2, view);
	    view.gbuf.setXORMode(darkGreen);
	    
	    //	    view.line(xorig, yorig, x, y);
	    view.gbuf.setPaintMode();
	    view.updateDisplay();
	}
	mode = 0;
    }

    public void mouseDown(int x, int y, int bnum) {
	mode = bnum;
	view.clear();
	xorig = x;
	yorig = y;
    }

    public void mouseDrag(int x, int y) {
	if (mode!=1) {
	    //stretch a line from (xorig, yorig) to (x, y)
	    view.clear();
	    Bresenham.BresenhamAlgorithm(new PointInt(xorig,yorig), 
					 new PointInt(x,y), 
					 view);
	    if (mode==2) {
		view.gbuf.setXORMode(darkGreen);
		view.line(xorig, yorig, x, y);
		view.gbuf.setPaintMode();
	    }
	    view.updateDisplay();
	}
	else {
	    //draw a simple line
	    view.clear();
	    view.drawThinLine();
	    view.updateDisplay();
	}
    }

    public final static void main(String[] s) {
	BresenhamCircleMain bcmain = new BresenhamCircleMain();
    }
}






