import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;

/**
 * A canvas that can display cirles.
 * @author Jan-Philipp Kappmeier
 */
public class CircleCanvas extends Canvas {

//	private static final long serialVersionUID = 1L;
	/** The {@link CircleApplet} in which this canvas is contained. */
	public CircleApplet applet;

	/**
	 * Creates a new canvas that has the ability to draw circles.
	 * @param applet the {@link CircleApplet} in which this canvas is contained
	 */
	public CircleCanvas( CircleApplet applet ) {
		super();
		this.setBackground( Color.white );
		this.applet = applet;
		repaint();
		this.setSize( 800, 166 );
	}

	/**
	 * method which is called if the frame wants to repaint itself,
	 * therefore it calls 'repaint' from its components
	 */
	@Override
	public void repaint() {
		if( getGraphics() != null )
			paint( getGraphics() );
	}

	/**
	 * Draws a circle with a specified color in a given graphics area.
	 * @param g the graphics area
	 * @param circle the circle to be drawn
	 * @param c the color of the circle
	 */
	public void drawCircle( Graphics g, Circle circle, Color c ) {
		g.setColor( c );

		if( circle == null )
			return;
		g.fillOval( (int)(circle.getCenter().getX() - circle.getRadius()), (int)(circle.getCenter().getY() - circle.getRadius()), (int)circle.getDiameter(), (int)circle.getDiameter() );
	}

	/**
	 * Method which is automatically called when the canves needs to be repainted.
	 * Retrieves the circles from the applet and draws the first one in blue, the
	 * second one in green.
	 */
	@Override
	public void paint( Graphics g ) {
		super.paint( g );

		Circle circle1 = applet.getCircle1();
		Circle circle2 = applet.getCircle2();

		drawCircle( g, circle1, Color.BLUE );
		drawCircle( g, circle2, Color.GREEN );
	}
}
