
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.*;
import java.lang.*;
import java.util.*;
import java.net.*;

public class AMSImage extends Applet {
  Image img, backimg;
  int[] pixels;
  int width, height;
  String stemp, slat, slong;
  int curX, curY;
  FontMetrics fm;
  Font myFont, boldFont;

  public void init() {
    //img = getImage(this.getDocumentBase(), "ed.gif");
    String fn = getParameter("filename");
    if (fn == null) fn = "ed.gif";
    try {
      URL u = new URL(getDocumentBase(), fn);
      URLConnection uc = u.openConnection();
      uc.setUseCaches(false);
      img = getImage(uc.getURL() );
      slat = " ";
      slong = " ";
      stemp = " ";
      curX = 0;
      curY = 0;
      MediaTracker track = new MediaTracker(this);
      track.addImage(img,0);
      track.checkAll(true);
      track.waitForID(0); 
    }
    catch (Exception ie) {return ;}
    width = img.getWidth(this);
    height = img.getHeight(this);

    pixels = new int[width * height];
    backimg = createImage( width, height);
    PixelGrabber pg = new 
             PixelGrabber(img,0,0,width,height,pixels,0,width);
    try { pg.grabPixels(); }
    catch (Exception ep) {System.out.println("grabPixels: "+ep);}
    int stat = pg.status();
    System.out.println("pg status = "+stat);
    Font myFont = getFont();
    boldFont = new Font(myFont.getName(), Font.BOLD, myFont.getSize());

  }

  public void update(Graphics g) {
    paint(g);
  }

  public void paint(Graphics g) {
    Graphics gb = backimg.getGraphics();
    gb.drawImage(img,0,0,this);

    gb.setFont(boldFont);
    fm = gb.getFontMetrics();
    String te = "Temp = -99.9xC";
    int w = fm.stringWidth(te);
    int h = fm.getHeight();

    int xPos = curX+10;
    if ( (curX + 25 + w) > width) {
      xPos = curX - w - 10;
    }

    int yPos = curY;
    if ( (yPos - h) < 5) {
      yPos = h+5;
    } else if ( (yPos + 2*h+5) > height) {
      yPos = height - 2*h - 5;
    }

    drawBold(gb,"Temp = "+stemp,xPos,yPos);
    drawBold(gb,"Lat = "+slat,xPos+4,yPos+h);
    drawBold(gb,"Long = "+slong,xPos+8,yPos+2*h);


    /*  this code does the black box...
    int xPos = curX+30;
    if ( (curX + 45 + w) > width) {
      xPos = curX - w - 30;
    }

    int yPos = curY+h;
    if ( (yPos - h) < 5) {
      yPos = h+5;
    } else if ( (yPos + 2*h+5) > height) {
      yPos = height - 2*h - 5;
    }

    

    gb.setColor(Color.black);
    gb.fillRect(xPos-5, yPos-h, w+10, 3*h+5);
    gb.setColor(Color.white);
    gb.drawString("Temp = "+stemp,xPos, yPos);
    gb.drawString("Lat = "+slat,xPos, yPos+h);
    gb.drawString("Long = "+slong,xPos, yPos+2*h);

     */

    g.drawImage(backimg,0,0,this);
  }

  void drawBold(Graphics g, String s, int x, int y) {
    g.setColor(Color.black);
    g.drawString(s,x-1,y-1);
    g.drawString(s,x+1,y-1);
    g.drawString(s,x-1,y+1);
    g.drawString(s,x+1,y+1);
    g.setColor(Color.yellow);
    g.drawString(s,x,y);
    return;
  }

  public boolean mouseMove(Event e, int x, int y) {
    if (x > width-1 || y > height-1) return true;
    int value = pixels[x + y*width];
    value = value & 0xff;

    // in the noise
    if (value < 4) return true;

    // well-known formulae for converting between brightness and temperature
    double temp;
    if (value > 176) {
      temp =  418. - value;
    } else {
      temp = (660. - value) / 2.;
    }

    // approximate degC to keep it in whole and half degrees.
    double tempC = temp - 273;
    stemp = tempC+"\u00b0"+"C";

    // lat/lon
    double DEGPER = .107974;
    double lat = 32.0 + (480/2 - y) * DEGPER;
    double lon = 95.0 + (640/2 - x) * DEGPER;
    slat = c2s(lat)+"N";
    slong = c2s(lon)+"W";

    curX = x;
    curY = y;
//    System.out.println("value = "+value+ " temperature ="+temp+" / "+tempC+ "  lat/lon=" + slat + "  "+slon);

    repaint();
    return true;
  }

  // return String to two decimal places for value v
  String c2s(double v) {
    String s = v+".000";
    int i = s.indexOf(".");
    return (s.substring(0,i+3));
  }
}
