import java.awt.*;

class MMgraph 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;
    private String name; 
    // the maximum weight = used for scaling
    private final float maxw = 45;

    MMgraph (MMobject dad) {
	this.name = name;
	this.daddy = dad;
    }
    
    public void paint(Graphics g) {
	Rectangle bounds = g.getClipRect();
	Rectangle b = new Rectangle(bounds.x + 10, bounds.y + 20, bounds.width-20, bounds.height-40);
	g.setColor(Color.black);
	g.drawString(daddy.name+" Graph", 5, 12);
	drawAxis(g, b);
	// 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) { 
	    /*
	    float curw = daddy.data.normalAt(0).weight;
	    float maxw=curw;
	    for (int i = 1; i<daddy.data.size(); i++) {
		curw = daddy.data.normalAt(i).weight;
		if (curw>maxw) maxw = curw;
	    }
	    */
	    float da = (float)(b.width/(2*Math.PI));
	    float dw = b.height/maxw;
//	    System.out.println("Scaling everything so that max height = "+Integer.toString(b.height)+" and max weight = "+Float.toString(maxw));
	    for (int i = 0; i<daddy.data.size(); i++) {
		drawNormal(g, b, daddy.data.normalAt(i), da, dw);
	    }
	}
	
    }
    
/*    public void findMax () {
	if (daddy.data.size() > 1) { 
	    float curw = daddy.data.normalAt(0).getWeight();
	    this.maxw=curw;
	    for (int i = 1; i<daddy.data.size(); i++) {
		curw = daddy.data.normalAt(i).getWeight();
		if (curw>this.maxw) this.maxw = curw;
	    }
	    System.out.println("Max weight found is "+Float.toString(maxw));
	}
    }    
  */  
    public void drawAxis(Graphics g, Rectangle b) {
	g.drawString("2*pi",b.x+b.width-10, b.y+b.height+14);
	g.drawString("0",b.x, b.y+b.height+14);
	g.drawLine(b.x, b.y+b.height, b.x+b.width, b.y+b.height);
	g.drawLine(b.x, b.y, b.x, b.y+b.height);
    }

    public void drawNormal (Graphics g, Rectangle b, MMnormal v, float da, float dw) {
	/*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);
	*/
	drawBar(g, b.x, b.y+b.height, (float) v.getAngle(), (float)(.2*v.getWeight()), (float) da, (float) dw);	
    }
    
    // draws a ray in the specified direction from the center (x,y)
    public void drawBar (Graphics g, int x, int y, float angle, float weight, float da, float dw) {
	g.setColor(Color.red);
	g.drawLine((int)(x+angle*da), y, (int)(x+angle*da), (int)(y-weight*dw));
    }
    
    public void clear () {
	getGraphics().setColor(this.getBackground());
	getGraphics().fillRect(0,0,bounds().width,bounds().height);
    }
}