package seisinfo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import java.io.*;


/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class USGS24Frame extends JFrame {
  String usgs24path1;
  String usgs24title;
  int refresh;
  int nr;

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


  boolean done=false;
  boolean offline=false;
  private Image offScreenImg;
  private Graphics offScreenG;
  Toolkit toolkit = Toolkit.getDefaultToolkit();
  Image image1;
  Image scaled1;
  MediaTracker tracker1 = new MediaTracker(this);;
  public static usgs24 t1;

  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


  public USGS24Frame(int nr,String usgs24path1,String usgs24title,int ref) {
    this.nr=nr;
    this.usgs24path1=usgs24path1;
    this.usgs24title=usgs24title;
    this.refresh=ref;
    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    scrheight=screenSize.height;  // get screen height
    scrwidth=screenSize.width;    // get screen width
    System.out.println(scrheight+"  "+scrwidth);

    this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        done=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);
         }
      });

    try {
      jbInit();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }

  }
  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("USGS24 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("USGS24 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;
    offline=true;
    switch(nr)
    {
      case 1:
      SeisInfoFrame.USGS24_1 = false;
      break;
      case 2:
      SeisInfoFrame.USGS24_2 = false;
      break;
      case 3:
      SeisInfoFrame.USGS24_3 = false;
      break;
      case 4:
      SeisInfoFrame.USGS24_4 = false;
      break;
    }
  }

  public void CrtImg()
  {
          offScreenImg = createImage(getSize().width, getSize().height);
          offScreenG = offScreenImg.getGraphics();
  }
  public void StartUSGS24()
  {
    t1=new usgs24();
    t1.start();
  }
  public void newpic()
  {
    try
    {
      image1 = toolkit.getImage(new URL(usgs24path1));
    }
    catch (MalformedURLException e)
    {
    }
/*
    tracker1.addImage(image1,1);
    try
    {
        if(!tracker1.waitForID(1,6000))
        {
            System.out.println("USGS24Frame: Error Loading Image 1");
            offline=true;
        }
    }catch(InterruptedException e){System.out.println(e);}
*/
//    scaled1=image1.getScaledInstance(470,400,Image.SCALE_AREA_AVERAGING);
    xx=scrwidth/2 - 2*xmarg;
    yy=scrheight/2 - 2*ymarg  - 3*ymarg;
    scaled1=image1.getScaledInstance(xx,yy,Image.SCALE_AREA_AVERAGING);

  }
  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,40);
    if(scaled1 != null)
    {
      g.drawImage(scaled1, xmarg, ytitle+10, this);
      image1.flush();
    }
  }


  class usgs24 extends Thread
  {

    public void run()
    {
      while(!done)
      {
        while(!offline)
        {
          newpic();
          repaint();
          try
          {
            sleep(refresh*1000);
          }catch(InterruptedException e){}
          System.out.println("MESSAGE: USGS24Frame tread active.");
        }
        try
        {
          sleep(20000);
        }catch(InterruptedException e){}
        offline=false;
      }
      System.out.println("MESSAGE: USGS24Frame thread stopped");
    }
  }

  void jbInit() throws Exception {
    this.getContentPane().setLayout(null);
  }
}
