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

abstract public class LaplaceGUI extends PhysicsApplet{

  double[][] phi;
  double[][] e;
  int[][] State;
  ArrayDisplay D;
  TitleCanvas Legend;
  double CathodeV = 100.0;
     // mouse modes:
  static final int NormalMode = 0;
  static final int CathodeMode = 1;
  static final int GroundMode = 2;
  static final int EraseMode = 3;
  static final int MeasureMode = 4;
     // button codes:
  static final int FieldResetCode = 1;
  static final int AllResetCode = 2;
  static final int MeasureECode = 3;
  static final int StartCode = 4;
  static final int StopCode = 5;
    // array states:
  static final int NormalState = 0;
  static final int CathodeState = 1;
  static final int GroundState = 2;
  

  LaplaceGUI(){
    super("Laplace Applet",100,100,4);
    phi = new double[Nx+1][Ny+1];
    e = new double[Nx+1][Ny+1];
    State = new int[Nx+1][Ny+1];
    for (int i = 0; i <= Nx; i++){
      for (int j = 0; j <= Ny; j++){
         phi[i][j] = 0.0;
         e[i][j] = 0.0;
         State[i][j] = 0;
       }
    }
  }

  void resetArrays(){
      for (int mx = 0; mx <= Nx; mx++){
        for (int my = 0; my <= Ny; my++){
          if (State[mx][my] == 0)  phi[mx][my] = 0.0;
        }
      }
      refreshPicture();
    }

    void resetAll(){
      for (int mx = 0; mx <= Nx; mx++){
        for (int my = 0; my <= Ny; my++){
         phi[mx][my] = 0.0;
         e[mx][my] = 0.0;
         State[mx][my] = 0;
        }
      }
      refreshPicture();
    }

  void buildPicture(){
    D = new ArrayDisplay(phi,e,State,Color.black,Color.magenta,
                                                 Color.red,Color.black);
    Picture.add(D,"Center");
    Legend = new TitleCanvas(" ", 16);
    Picture.add(Legend,"South");
  }

  void refreshPicture(){
    D.refresh();
  }
  
  void buildControls(){
    Controls.setLayout(new GridLayout(0,4,10,10));
    ModeButton B1 = new ModeButton("Cathode", CathodeMode);
    Controls.add(B1);
    ModeButton B2 = new ModeButton("Ground", GroundMode);
    Controls.add(B2);   
    ModeButton B3 = new ModeButton("Erase", EraseMode);
    Controls.add(B3);   
    CommandButton B4 = new CommandButton("Reset All", AllResetCode);
    Controls.add(B4);
    CommandButton B5 = new CommandButton("Solve Laplace", StartCode);
    Controls.add(B5);
   CommandButton B6 = new CommandButton("Stop",StopCode);
    Controls.add(B6);
    Label B7 = new Label(" ");
    Controls.add(B7);
    CommandButton B8 = new CommandButton("Reset Fields", FieldResetCode);
    Controls.add(B8);
    Label B9 = new Label(" ");
    Controls.add(B9);
    ModeButton B10 = new ModeButton("Measure Voltage", MeasureMode);
    Controls.add(B10);
    CommandButton B11 = new CommandButton("Compute Energy",MeasureECode);
    Controls.add(B11);
    Label B12 = new Label(" ");
    Controls.add(B12);
  }

  Color findColor(double A, double C, int S){
    if (S == CathodeState)  return D.Color2;
    if (S == GroundState)   return D.Color1;
    int colorm =  (int) ( 10.0 * A/CathodeV);
    return D.DisplayColors[colorm];
  }

  void writeFieldValue(double Val, int mode){
    if (mode == MeasureMode) Legend.write(" Voltage = " + Val);
  }

  void setArrays(int i, int j, int mode){
    if (mode == CathodeMode){
      phi[i][j] = CathodeV;
      State[i][j] = CathodeState;
    } else if (mode == GroundMode){
      phi[i][j] = 0.0;
      State[i][j] = GroundState;
    } else if (mode == EraseMode){
      phi[i][j] = 0.0;
      State[i][j] = 0;
    }
  }

  void doAction(int Code){
    switch(Code){
      case FieldResetCode:
        resetArrays();
        break;;
     case AllResetCode:
        resetAll();
        break;
     case MeasureECode:
        double E = Energy();
        Legend.write("Energy = "+E);
        break;
     case StartCode:
        startThread();
        break;
     case StopCode:
        stopThread();
        break;
     default:
        break;
    } 
  }

  abstract double Energy();
}
      
    










