/** ShapeDemo.java -- a standalone application demonstrating polymorphism */

import java.awt.*;
import java.util.*;

abstract class Shape
{
	protected int x1, y1, x2, y2;
	
	public void setDimensions(int xpos1, int ypos1, int xpos2, int ypos2)
	{
		x1 = xpos1;
		y1 = ypos1;
		x2 = xpos2;
		y2 = ypos2;
	}
	
	abstract void draw(Graphics g);
}

class Rect extends Shape
{
	public void draw(Graphics g)
	{
		g.drawRect(x1, y1, x2-x1, y2-y1);
	}
}

class Circle extends Shape
{
	public void draw(Graphics g)
	{
		g.drawOval(x1, y1, x2-x1, y2-y1);
	}
}

class Line extends Shape
{
	public void draw(Graphics g)
	{
		g.drawLine(x1, y1, x2, y2);
	}
}

class ShapeDemo extends Frame
{
    private Canvas canvas;
    private Vector shapes;	// list of Shapes
	
    public ShapeDemo()
    { 
        // create the Frame with title.
        super("Shape Demo"); 
        
        // Add a menubar, with a File menu, with a Quit button.
        MenuBar menubar = new MenuBar();
        Menu file = new Menu("File", true);
        menubar.add(file);
        file.add("Quit");
        file.add("Draw");
        setMenuBar(menubar);
        
        //  make the Canvas and shape list
        canvas = new Canvas();
        shapes = new Vector();
        add("Center", canvas);
        resize(300, 300);
        
        // create the shapes
        Shape shape = new Rect();
        shape.setDimensions(100, 100, 200, 200);
        addShape(shape);
        shape = new Circle();
        shape.setDimensions(125, 125, 175, 175);
        addShape(shape);
        shape = new Line();
        shape.setDimensions(100, 200, 200, 100);
        addShape(shape);
   
        // pop up window
        show();
    }
        
    void draw()
    {
        for (int i=0; i<shapes.size(); i++)
        {
            Shape shape = (Shape)shapes.elementAt(i);
            shape.draw(canvas.getGraphics());
        }
    }

    void addShape(Shape shape)
    {
         shapes.addElement(shape);
    }
    
    // Handle the  menu buttons.
    public boolean action(Event e, Object arg)
    {
        if (e.target instanceof MenuItem)
        {
            String label = (String) arg;
            if (label.equals("Quit"))
                System.exit(0);
            else if (label.equals("Draw"))
                draw();
        }
        return false;
    }
    
    public static void main(String args[])
    {
    	new ShapeDemo();
    }
}
