package rpn;

import java.awt.*;
import rpn.graphics.CalculatorCanvas;

class GraphicsFrame extends Frame {
	public CalculatorCanvas canvas;
	CalculatorPanel vc;
	int w, h;
	public GraphicsFrame(CalculatorPanel vc, String t, int w, int h) {
		super(t);
		this.vc = vc;
		setLayout(new BorderLayout(0, 0));
		// System.out.println("Frame: w, h = " + w + ", " + h);
		canvas = new CalculatorCanvas(w, h);
		add("Center", canvas);
		pack();
		show();
	}

	void reset() {
		canvas.reset();
	}

	boolean initialized = false;
	public void paint(Graphics g) {
		super.paint(g);
		if (!initialized) {
			initialized = true;
		} else {
			Rectangle r = bounds();
			int W  = r.width, H = r.height;
			if (W != w || H != h) {
				w = W; h = H;
				canvas.matrix.setDimensions(w, h);
				canvas.setDimensions(w, h);
				canvas.mkImage();
			}
		}
	}

	public boolean handleEvent(Event e) {
		// System.err.println(EventDisplay.toString(e));
		if (e.id == Event.WINDOW_DESTROY) {
			// signal it to the calculator
			vc.gfxDestroy();
			dispose();
		}
		return super.handleEvent(e);
	}
}
