package psplot;

import java.awt.*;
import real.*;

public class controlNode {
	// puts the little squares at the control points
	public int x, y;
	public Color colour = Color.lightGray;
	PlotCanvas canvas;
	Image img;

	public static int wd = 3, ht = 3;
	// the point is that by calling the nodes a class we can
	// change shapes easily 

	public controlNode(PlotCanvas c, int xx, int yy) {
		canvas = c; x = xx; y = yy;
		mkImage();
	}
	
	public controlNode(PlotCanvas c, Point p) {
		canvas = c; x = p.x; y = p.y;
		mkImage();
	}

	public controlNode(PlotCanvas c, RealPoint P) {
		canvas = c; 
		locate(P);
		mkImage();
	}

	public controlNode(PlotCanvas c, RealPoint P, Color col) {
		canvas = c;
		locate(P);
		colour = col;
		mkImage();
	}

	void mkImage() {
		img = canvas.parent.createImage(2*wd+1, 2*ht+1);
		Graphics g = img.getGraphics();
		g.setColor(colour);
		g.fillRect(0, 0, 2*wd, 2*ht);
		g.setColor(Color.black);
		g.drawRect(0, 0, 2*wd, 2*ht);
	}

	public boolean contains(int a, int b) {
		if (x - wd <= a && a <= x + wd &&
			y - ht <= b && b <= y + ht) {
			return(true);
		}
		else return(false);
	}
	
	public void locate(int xx, int yy) {
		x = xx; y = yy;
	}

	public RealPoint realLocation() {
		return(canvas.toRealPoint(x, y));
	}

	public void locate(RealPoint P) {
		Point Q = canvas.toPoint(P);
		x = Q.x; y = Q.y;
	}

	public void draw() {
		Graphics g = canvas.gfx;
		Color c = g.getColor();
		g.setColor(colour);
		g.fillRect(x - wd, y - ht, 2*wd, 2*ht);
		g.setColor(Color.black);
		g.drawRect(x - wd, y - ht, 2*wd, 2*ht);
		g.setColor(c);
	}
	
	
	public void paint(Graphics g) {
		// System.out.println("img = " + img);
		// g.drawImage(img, x-wd, y-ht, null);
		Color c = g.getColor();
		g.setColor(colour);
		g.fillRect(x - wd, y - ht, 2*wd, 2*ht);
		g.setColor(Color.black);
		g.drawRect(x - wd, y - ht, 2*wd, 2*ht);
		g.setColor(c);
	}
}

