import java.awt.*;

class MMeci extends Panel {

    private final double pi_2 = Math.PI/2, pi = Math.PI;
    // a pointer to the parent object that created it
    public MMobject daddy;

    MMeci (MMobject dad) {
	this.daddy = dad;
    }
    
    public void paint(Graphics g) {
	Rectangle bounds = g.getClipRect();
	Rectangle square = new Rectangle();
	if (bounds.height>bounds.width) {
	    square.resize(bounds.width, bounds.width);
	    square.move(bounds.x, bounds.y + (bounds.height-bounds.width)>>1);
	} else { 
	    square.resize(bounds.height, bounds.height);
	    square.move(bounds.x + (bounds.width-bounds.height)>>1, bounds.y);
	}

	g.setColor(Color.black);
	g.drawString("ECI", 5, 12);
	drawCircle(g, square, 15);
	// System.out.println("About to call g.drawLine (lastx, lasty, echs, wai) = ("+Integer.toString(lastx)+", "+Integer.toString(lasty)+", "+Integer.toString(echs)+", "+Integer.toString(wai)+")");
	if (daddy.data.size() > 1) { 
	    for (int i = 0; i<daddy.data.size(); i++) {
		drawNormal (g, square, daddy.data.normalAt(i));
	    }
	}
	
    }
    
    public void drawCircle(Graphics g, Rectangle b, int radius) {
	g.drawOval(b.x, b.y, b.width-1, b.height-1);
	g.drawOval(b.x+(b.width>>1)-radius, b.y+(b.height>>1)-radius, radius<<1, radius<<1);
    }

    public void drawNormal (Graphics g, Rectangle b, MMnormal v) {
	int x0 = b.x + (b.width>>1);
	int y0 = b.y + (b.height>>1); 
	
	/*g.setColor(Color.magenta);
	g.drawRect(b.x,b.y,b.height,b.width);
	g.drawLine(b.x, b.y, b.x + b.width, b.y + b.height);
	g.drawLine(b.x + b.width, b.y, b.x, b.y + b.height);
	g.setColor(Color.blue);
	g.drawLine(x0,y0-5,x0,y0+5);
	g.drawLine(x0-5,y0,x0+5,y0);
	*/
	drawRay(g, x0, y0, (float) v.getCosa(), (float) v.getSina(), 15, (float)(.1*v.getWeight()));	
    }
    
    // draws a ray in the specified direction from the center (x,y)
    public void drawRay (Graphics g, int x, int y, float cosa, float sina, float offset, float length) {
	g.setColor(Color.red);
	g.drawLine((int)(x+offset*cosa), (int)(y+offset*sina), (int)(x+(offset+length)*cosa), (int)(y+(offset+length)*sina));
    }
    
    public void clear () {
	getGraphics().setColor(this.getBackground());
	getGraphics().fillRect(0,0,bounds().width,bounds().height);
    }
}
