package jbgui;
/*
	Joseph Bergin 12.97
	Ported to JFC December 2000
*/

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Point;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.PrintJob;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;

public class PaintDrawApplet extends JApplet
{	private TestCanvas drawArea = new TestCanvas();
	private Toolkit myToolkit = Toolkit.getDefaultToolkit();
	private JFrame myFrame;
	
	public PaintDrawApplet(JFrame f){myFrame = f; }
	
	public void init() 
	{	Container contentPane = getContentPane();
		contentPane.setLayout(new FlowLayout());
		contentPane.setBackground(Color.white);	
		contentPane.add (drawArea);
		contentPane.add(moveButton);
		contentPane.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);
	JButton moveButton = new JButton("Move");
	JButton printButton = new JButton("Print");
	
	public void print(){drawArea.print();}
	
	public Dimension getPreferredSize()
	{	return new Dimension(600, 340);
	}
	
	class TestCanvas extends JPanel
	{	
		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);
			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).  
	
*/
