// ShowImageComponent.java
//
//
// Copyright Henrik Björkman 1999
//
// History:
//
// Created by Henrik Björkman 1999-08-13 using code from 
// DrawCircleDemo.java    
//

package chartplotter_package;

import java.awt.*;
import java.awt.event.*;
import java.io.*;


public class ShowImageComponent extends Component /*implements ActionListener*/
{
  final int lineLen=30;
  protected int width,height;
  protected Frame frame;
  Image img=null;          // The chart being show on screen
  Point point;



  static void debug(String s)
  {
    System.out.println("ShowImageComponent: " + s);
  }

  synchronized public Point getPoint()
  {
    return(point);
  }

  synchronized public void movePoint(int x, int y)
  {
    if (point!=null) {drawCross(point);}
    point.x+=x;
    point.y+=y;
    drawCross(point);
    System.out.println("x="+point.x+" y="+point.y);
  }


  synchronized public void loadChart(String name)
  {

    debug("loadChart "+name);

    img = getToolkit().getImage(name);



    // Wait until the image is loaded so we know the size
    int i=0;
    while ((i<30) && (img.getWidth(this)<0))
    {
      try {this.wait(100);}  catch (InterruptedException e) {;}     
      i++;
    }



    this.setSize(this.getPreferredSize());

  }



  public ShowImageComponent(Frame frame, int width, int height)
  {
     this.frame=frame;
     this.width=width;
     this.height=height;


     this.enableEvents(AWTEvent.MOUSE_EVENT_MASK);
     /*this.enableEvents(AWTEvent.KEY_EVENT_MASK);*/


  }

  public ShowImageComponent(Frame frame, boolean trace, int line_width)
  {
     this.frame=frame;
     this.width=160;
     this.height=100;

     this.enableEvents(AWTEvent.MOUSE_EVENT_MASK);

  }

  synchronized public Dimension getPreferredSize() 
  {
    if (img!=null)
    {
      debug("width "+img.getWidth(this)+ " height "+img.getHeight(this));
      return new Dimension(img.getWidth(this), img.getHeight(this));
    }
    else
    {
      return new Dimension(width, height);
    }
  }

  void drawCross(Point p)
  {
      Graphics g=getGraphics();
      g.setXORMode(Color.white);
      g.drawLine(p.x-lineLen, p.y, p.x+lineLen, p.y);
      g.drawLine(p.x, p.y-lineLen, p.x, p.y+lineLen);
      g.dispose();
  }


  synchronized public void processMouseEvent(MouseEvent e)
  {
    if (e.getID()==MouseEvent.MOUSE_PRESSED)
    {
      if (point!=null) {drawCross(point);}
      point=new Point(e.getX(),e.getY());
      drawCross(point);
      System.out.println("x="+e.getX()+" y="+e.getY());
    }
    else 
    {
      super.processMouseEvent(e);
    }
  }



  public void update(Graphics g)
  {
    paint(g);
  }  

  public synchronized void paint(Graphics g) 
  {
    Point p=null;


    if (img!=null) {g.drawImage(img, 0, 0, this);}

    g.setPaintMode();
    g.setColor(Color.blue);


    if (point!=null) {drawCross(point);}

    g.dispose();
  }


}


