package seisinfo;

import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class USGSFrame extends JFrame {
  String usgspath1;
  String usgspath2;
  String usgstitle;
  int refresh;
  int xm;
  int ym;

  int scrheight;                   // screen height
  int scrwidth;                    // screen width
  int xmarg=10;                    // x-margin
  int ymarg=10;                    // y-margin
  int xx;                          // picture width
  int yy;                          // picture height
  int ysubtitle=70;                // height subtitle
  int ytitle=30;                   // height frame title

  double XX;
  double YY;
  double MaxX;
  double MinX;
  double MaxY;
  double MinY;


  private Image offScreenImg;
  private Graphics offScreenG;
  Toolkit toolkit = Toolkit.getDefaultToolkit();
  Image image1;
  Image image2;
  Image scaled1;
  Image scaled2;

  MediaTracker tracker1 = new MediaTracker(this);
  MediaTracker tracker2 = new MediaTracker(this);
  public static usgs1 t1;
  boolean done=false;
  boolean offline=false;
  public USGSFrame(String usgspath1,String usgspath2,String usgstitle, int ref,int xm,int ym) {
    this.usgspath1=usgspath1;
    this.usgspath2=usgspath2;
    this.usgstitle=usgstitle;
    this.refresh=ref;
    this.xm=xm;
    this.ym=ym;

    try {
      jbInit();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    scrheight=screenSize.height;  // get screen height
    scrwidth=screenSize.width;    // get screen width
    System.out.println("USGS "+scrheight+"  "+scrwidth);

    this.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        done=true;
        offline=true;
        System.out.println("CLOSE");
        Close();
      }
    });

    addComponentListener(new java.awt.event.ComponentAdapter()
    {
      public void componentMoved(java.awt.event.ComponentEvent evt)
      {
        formComponentMoved(evt);
      }
       public void componentResized(java.awt.event.ComponentEvent evt)
       {
         formComponentResized(evt);
       }
    });

  }
  private void formComponentMoved(java.awt.event.ComponentEvent evt) {
      // Add your handling code here:
//        System.out.println("I was moved to "+this.getLocation());
      XX=this.getLocation().getX();
      YY=this.getLocation().getY();
      System.out.println("USGS is here: "+XX+","+YY);
  }
  private void formComponentResized(java.awt.event.ComponentEvent evt)
  {
      // Add your handling code here:
//      System.out.println("TU "+this.getBounds());
      XX=this.getBounds().getMinX();
      YY=this.getBounds().getMinY();
//      System.out.println("USGS is here: "+XX+","+YY);
      MaxX=this.getBounds().getMaxX();
      MinX=this.getBounds().getMinX();
      MaxY=this.getBounds().getMaxY();
      MinY=this.getBounds().getMinY();
      System.out.println((int)MinX+","+(int)MinY+"  "+(int)MaxX+","+(int)MaxY);
  }

  public int LocX()
  {
      return((int)XX);
  }
  public int LocY()
  {
      return((int)YY);
  }

  public void Close()
  {
    this.setVisible(false);
    done=true;
    SeisInfoFrame.USGS=false;
  }

  void jbInit() throws Exception {
    this.getContentPane().setLayout(null);
  }
  public void newpic()
  {
    try
    {
      image1 = toolkit.getImage(new URL(usgspath1));
    }
    catch (MalformedURLException e)
    {
    }
/*
    tracker1.addImage(image1,1);
    try
    {
        if(!tracker1.waitForID(1,6000))
        {
            System.out.println("USGSFrame: Error Loading Image 1");
            offline=true;
        }
    }catch(InterruptedException e){System.out.println(e);}
*/
//    xx=scrwidth/2 - 2*xmarg;
//    yy=scrheight/2 - 2*ymarg - ysubtitle - ytitle - ymarg;
//    scaled1=image1.getScaledInstance(xx,yy,Image.SCALE_AREA_AVERAGING);

    xx=(int)MaxX - 2*xmarg;
    yy=(int)MaxY - 2*ymarg - ysubtitle - ytitle - ymarg;
//    System.out.println("XX,YY :"+(int)MaxX+" "+(int)MaxY);
    scaled1=image1.getScaledInstance(xx,yy,Image.SCALE_AREA_AVERAGING);

    try
    {
      image2 = toolkit.getImage(new URL(usgspath2));
    }
    catch (MalformedURLException e)
    {
    }
/*
    tracker2.addImage(image2,2);
    try
    {
        if(!tracker2.waitForID(2,6000))
        {
            System.out.println("USGSFrame: Error Loading Image 2");
            offline=true;
        }
    }catch(InterruptedException e){System.out.println(e);}
*/
//    scaled2=image2.getScaledInstance(470,60,Image.SCALE_AREA_AVERAGING);
    scaled2=image2.getScaledInstance(scrwidth/2-2*xmarg,ysubtitle,Image.SCALE_AREA_AVERAGING);
  }

  public void CrtImg()
  {
    offScreenImg = createImage(getSize().width, getSize().height);
    offScreenG = offScreenImg.getGraphics();
  }
  public void update(Graphics g)
  {
    offScreenG = offScreenImg.getGraphics();
    paint(offScreenG);
    g.drawImage(offScreenImg,0,0,this);
  }
  public void paint(Graphics g)
  {
    g.drawString("Source: USGS",10,50);
    if(scaled1 !=null)
    {
      g.drawImage(scaled1, 8, ytitle+ymarg, this);
      image1.flush();
    }
    if(scaled2 !=null)
    {
      g.drawImage(scaled2, 8, scrheight/2 - ymarg - ysubtitle, this);
      image2.flush();
    }
  }

  public void StartUSGS()
  {
    t1=new usgs1();
    t1.start();
  }
  class usgs1 extends Thread
  {
    public void run()
    {
      while(!done)
      {
        while(!offline)
        {
          newpic();
          repaint();
          try
          {
            sleep(refresh*1000);
          }catch(InterruptedException e){}
          System.out.println("MESSAGE: USGSFrame thread active.");
        }
        try
        {
          sleep(20000);
        }catch(InterruptedException e){}
        offline=false;
      }
      System.out.println("MESSAGE: USGSFrame thread stopped.");
    }
  }

}


