import java.applet.*;
import java.awt.*;
import java.awt.event.*;

import Geod_2_Cart;
import Transform_7p;
import Cart_2_Geod;
import Cart_2_Proj;
import survey;
import hk80_pattern;

public class ToHk80 extends Applet implements ActionListener
{
   Label title = new Label("Transformation of Co-ordinates from WGS84 to HK1980.");
   Label label01 = new Label("WGS84 :");
   Label label02 = new Label("Latitude N(DD.MMSS)");
   Label label03 = new Label("Longitude S");
   Label label04 = new Label("ellipsoid height");
   Label label05 = new Label("HK80 :");
   Label label_N = new Label();
   Label label_E = new Label();

   TextField T_lat = new TextField();
   TextField T_long = new TextField();
   TextField T_ell = new TextField();

   Button conver = new Button("Convert");
   Button clear = new Button("Clear");

   public void init()
   {
      setLayout(null);
      title.setBounds(10,10,320,30);
      label01.setBounds(10,40,50,25);
      label02.setBounds(70,40,120,25);
      label03.setBounds(200,40,80,25);
      label04.setBounds(330,40,120,25);

      T_lat.setBounds(70,70,120,20);
      T_long.setBounds(200,70,120,20);
      T_ell.setBounds(330,70,120,20);

      conver.setBounds(200,100,120,20);
      conver.addActionListener( this);

      clear.setBounds(330,100,120,20);
      clear.addActionListener( this);

      label05.setBounds(10,130,50,25);
      label_N.setBounds(70,130,100,25);
      label_E.setBounds(200,130,100,25);

      add(title);
      add(label01);
      add(label02);
      add(label03);
      add(label04);
      add(T_lat);
      add(T_long);
      add(T_ell);
      add(conver);
      add(clear);
      add(label05);
      add(label_N);
      add(label_E);
   }

      public void actionPerformed( ActionEvent ev)
      {
         String s = ev.getActionCommand();
         if(s.equals("Convert"))
         {
         // WGS84 datum
         Geod_2_Cart WGS84_G2C = new Geod_2_Cart(6378137, 6.69437999015e-3);

         // assign the information of a point
         WGS84_G2C.Lat = survey.hms2hr(Double.valueOf( T_lat.getText()).doubleValue())*1.74532925199e-2;
         WGS84_G2C.Long = survey.hms2hr(Double.valueOf( T_long.getText()).doubleValue())*1.74532925199e-2;
         WGS84_G2C.h = Double.valueOf( T_ell.getText()).doubleValue();
      
         // perform conversion
         WGS84_G2C.compute();
      
         // 7-parameter transformation
         Transform_7p WGS84_2_HK80 = new Transform_7p( -356.6098, 178.7987, -127.0985,
                                                       -3.20193e-5, -5.90817e-5, 6.89496e-5,
                                                        1.77866e-7);
         WGS84_2_HK80.X1 = WGS84_G2C.X;
         WGS84_2_HK80.Y1 = WGS84_G2C.Y;
         WGS84_2_HK80.Z1 = WGS84_G2C.Z;
         WGS84_2_HK80.compute();
      
         // HK80 datum
         Cart_2_Proj HK80_C2P = new Cart_2_Proj(6378388, 0.00672267, 0.38942018981,
                                             1.99279172962, 819069.8, 836694.05, 1.0);
         // assign the information of a point
         HK80_C2P.X = WGS84_2_HK80.X2;
         HK80_C2P.Y = WGS84_2_HK80.Y2;
         HK80_C2P.Z = WGS84_2_HK80.Z2;
      
         // perform conversion
         HK80_C2P.compute();
         HK80_C2P.Long += 3.14159265359;
         HK80_C2P.compute_grid();

         // output the result
         label_N.setText( hk80_pattern.generic.format(HK80_C2P.northing)+"N");
         label_E.setText( hk80_pattern.generic.format(HK80_C2P.easting)+"E");
      }else
      {
         T_lat.setText("");
         T_long.setText("");
         T_ell.setText("");
         label_N.setText("");
         label_E.setText("");
      }
      }
}
