package figure;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;

public class FigurePanel extends JPanel {
    AffineTransform transform;
    int width = 0, height = 0;
    public BoundingBox bbox;
    Vector plotables;
    
    public FigurePanel(double llx, double lly, double urx, double ury) {
        setBackground(Color.white);
        bbox = new BoundingBox(llx, lly, urx, ury);
        plotables = new Vector();
    }

    public void paintComponent(Graphics gfx) {
        super.paintComponent(gfx);
        Rectangle size = getBounds();
        if (size.width != width || size.height != height) {
            width = size.width;  height = size.height;
            float ratioX = (float) ((width-insets.left-insets.right) / 
				    (bbox.urx - bbox.llx));
            float ratioY = (float) (-(height-insets.top-insets.bottom) /
				    (bbox.ury - bbox.lly));
            transform = new AffineTransform();
	    transform.translate(insets.left, insets.top);
            transform.scale(ratioX, ratioY);
            transform.translate(-bbox.llx, -bbox.ury);
	}	
        Graphics2D g = (Graphics2D) gfx;
	g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
			   RenderingHints.VALUE_ANTIALIAS_ON);
        for (int i = 0; i < plotables.size(); i++)
            ((Plotable) plotables.elementAt(i)).plot(g);
    }

    public BoundingBox getBoundingBox() {
        return bbox;
    }

    public AffineTransform getTransform() {
        return transform;
    }

    public void add(Plotable p) {
        plotables.addElement(p);
        p.setFigurePanel(this);
    }

}

