import java.awt.*;
import java.applet.Applet;

/**
 * Original solution to the inheritance exercise of the Java Basics course. 
 * @author Kary Fr&auml;mling
 */
public class InheritanceExc extends Applet
{
    Figure[]	figArray;

    public static void main(String argv[])
    {
	Frame	f;
	
	f = new Frame();
	f.setLayout(new GridLayout());
	f.add(new InheritanceExc());
	f.setSize(250, 250);
	f.show();
    }

    InheritanceExc()
    {
	figArray = new Figure[2];
	figArray[0] = new Circle(50, 50, 30);
	figArray[1] = new Rectangle(100, 140, 50, 70);
    }

    public void paint(Graphics g)
    {
	for ( int i = 0 ; i < figArray.length ; i++ )
	    figArray[i].drawIt(g);
    }
}

class Figure
{
    protected Point	pos;
    
    Figure(int x, int y) { pos = new Point(x, y); }

    public void drawIt(Graphics g) {}
    
    public double getArea() { return 0; }

    public void writeFigureArea(Graphics g)
    {
	if ( pos != null )
	    g.drawString("Area: " + getArea(), pos.x, pos.y);
    }
}

class Circle extends Figure
{
    private int	radius = 5;

    Circle(int x, int y, int r) { super(x, y); radius = r; }
    
    public double getArea() { return Math.PI*radius*radius; }

    public void drawIt(Graphics g)
    {
	if ( pos != null ) {
	    g.fillOval(pos.x, pos.y, 2*radius, 2*radius);
	    writeFigureArea(g);
	}
    }
}

class Rectangle extends Figure
{
    Dimension	mySize;

    Rectangle(int x, int y, int width, int height)
    {
	super(x, y);
	mySize = new Dimension(width, height);
    }

    public double getArea()
    {
	if ( mySize != null )
	    return mySize.width*mySize.height;
	else
	    return 0;
    }

    public void drawIt(Graphics g)
    {
	if ( pos != null && mySize != null ) {
	    g.fillRect(pos.x, pos.y, mySize.width, mySize.height);
	    writeFigureArea(g);
	}
    }
}

