/*
	Joseph Bergin 12.97
*/

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

public class PaintDrawApplet extends Applet
{	TestCanvas drawArea = new TestCanvas();
	Toolkit myToolkit = Toolkit.getDefaultToolkit();
	Frame myFrame;
	
	public PaintDrawApplet(Frame f){myFrame = f; }
	
	public void init() 
	{	add (drawArea);
		drawArea.setBackground(Color.white);
		add(moveButton);
		add(printButton);
		moveButton.addActionListener(new MoveListener());
		printButton.addActionListener(new PrintListener());
	}
	
	RectangleFigure a = new RectangleFigure("A", 30, 50, 50, 50);
	RectangleFigure b = new RectangleFigure("B", 200, 50, 50, 50);
	Button moveButton = new Button("Move");
	Button printButton = new Button("Print");
	
	public void print(){drawArea.print();}
	
	public Dimension getPreferredSize()
	{	return new Dimension(500, 300);
	}
	
	class TestCanvas extends Canvas
	{	public Dimension getPreferredSize()
		{	return new Dimension(400, 300);
		}
 
		public void paint( Graphics g ) 
		{	a.drawConnect(b, g);
			a.draw(g);
			b.draw(g);
		}
		
		public void print()
		{	PrintJob printing = myToolkit.getPrintJob(myFrame, "Testing", null);
			if(printing == null)
				System.out.println("No print job");
			else
			{	Graphics drawer = printing.getGraphics();
				if(drawer == null) 
					System.out.println("No Graphics.");
				else
				{	paint(drawer);
					drawer.dispose();
				}
				printing.end();
			}
		}

	}
	
	class MoveListener implements ActionListener
	{	public void actionPerformed(ActionEvent e)
		{	b.move(b.location().x, b.location().y + 20);
			drawArea.repaint();
		}
	}

	class PrintListener implements ActionListener
	{	public void actionPerformed(ActionEvent e)
		{	print();
		}
	}
}

	class RectangleFigure implements java.io.Serializable
	{	private Rectangle bounds;
		private String label;
		
		public RectangleFigure(String label, int x, int y, int w, int h)
		{	bounds = new Rectangle(x, y, w, h);
			this.label = label;
		}
		
		public void draw(Graphics g)
		{	int x = bounds.x, y = bounds.y, h = bounds.height, w = bounds.width;
			g.drawRect(x, y, w, h);
			Color c = g.getColor();
			g.setColor(Color.white);
			g.fillRect(x+1, y+1, w - 1, h - 1);
			g.setColor(Color.red);
			g.drawString(label, x + w/2, y + h/2);
			g.setColor(c);
		}
		
		public Rectangle rectangle(){ return bounds; }
		public Point location(){ return new Point(bounds.x, bounds.y); }
		public Point center(){ return new Point(bounds.x+bounds.width/2, bounds.y+bounds.height/2); }
		public Dimension dimension(){ return new Dimension(bounds.width, bounds.height);}
		
		public void move(int x, int y)
		{	bounds.x = x; bounds.y = y;
		}
		
		public void drawConnect(RectangleFigure b, Graphics g)
		{	g.drawLine(center().x, center().y, b.center().x, b.center().y);
		}
	}
	
/* Improvements.  Center the label. Size the box according to the size of the label.
	Get/set rectangle colors.  Get/set rectangle label. 
	More getters/setters for Rectangle Figure.  Figure root class and other subclasses. 
	Line as a figure (Label on line?).  PaintVector in panel holding all figures to be
	painted.  Mouse select and rubberbanding moves.  Connection objects (not lines but
	show as lines). Save a figure to a file. (Object serialization).  
	
*/
