package edu.cornell.lassp.houle.HysSym;

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

/**
*
* <b>hys</b> is an applet that displays a <b>HysLattice</b> using a 
* <b>HysCanvas</b>,  plots the magnetic field and magnetization with a
* <b>XyPlot</b> and uses a <b>HysControlBar</b> to control the
* <b>HysLattice</b>.  For an example of usage,  see
* <A HREF="http://www.msc.cornell.edu/~houle/hysteresis"
* TARGET="edu.cornell.lassp.houle.docsym">
* a page that uses it.</A>
*
* <PRE>
* Parameters:
*
*     rfield      random field value -- a floating point number
* </PRE>
*
* <P>
* <A HREF="../src/edu/cornell/lassp/houle/HysSym/hys.java" TARGET="edu.cornell.lassp.houle.source">
* Source code </A> is available.
*
* <UL>
* <LI> Version 0.91a:  Changed "run" method to make applet run the field to
*      zero before handing control to user.  This makes the applet fail more
*      gracefully if,  for some reason,  the code to let the user set the
*      target B doesn't work.
* </UL>
*
* @author <A HREF="http://www.msc.cornell.edu/~houle" TARGET="edu.cornell.lassp.houle.author"> Paul Houle </A> (E-mail: <A HREF="mailto:ph18@cornell.edu">ph18@cornell.edu</A>)
* @version 0.9a
*/

public class hys extends Applet implements Runnable {

  float R;
  GridBagLayout layout;
  GridBagConstraints gbc;
  TextField tf;
  public HysCanvas hc;
  public HysLattice hl;
  public HysControlBar hcb;
  XyPlot xy;

  Thread me;

  public void init() {

  R=Float.valueOf(getParameter("rfield")).floatValue();
  hl = new HysLattice(64,64,R);
  hc = new HysCanvas(hl);
  xy = new XyPlot(260,260);
  hcb = new HysControlBar(260,25);
  
  tf = new TextField("starting applet",30);
  tf.setEditable(false);
  tf.setFont(new Font("courier",Font.PLAIN,14));
  layout=new GridBagLayout();
  gbc= new GridBagConstraints();

  setLayout(layout);

  gbc.weightx=1.0;
  layout.setConstraints(hc,gbc);
  add(hc);

  gbc.gridwidth = GridBagConstraints.REMAINDER;
  layout.setConstraints(xy,gbc);
  add(xy);

  gbc.gridwidth= GridBagConstraints.RELATIVE;
  layout.setConstraints(tf,gbc);
  add(tf);

  gbc.gridwidth= GridBagConstraints.REMAINDER;
  layout.setConstraints(xy,gbc);
  add(hcb);


  hl.registerConsumer(xy);
  hl.registerConsumer(hcb);
  hcb.attachModel(hl);

  xy.setWorld(hl.bmin()+2,+1.0,hl.bmax()-2,-1.0);
  hcb.setRange(hl.bmin()+2,hl.bmax()-2);
  tf.repaint();
  hc.repaint();
  xy.repaint();
  hcb.repaint();

  }

  public void run() {
     tf.setText("Generating hysteresis loop...");
     hl.runTo(100);
     while(hl.isRunning())
	sleep(500);

     hl.runTo(-100);
     while(hl.isRunning())
	sleep(500);

     hl.runTo(0);
     while(hl.isRunning())
	sleep(500);

     tf.setText("Set goal B with control bar ====>");
     hcb.takeControl(true);
  };

  void sleep(int yieldTime) {

      try
      {
        Thread.currentThread().sleep(yieldTime);
      } catch(InterruptedException e) {};
  };

  public void start() {
      me = new Thread(this);
      me.setPriority(Thread.MIN_PRIORITY);
      me.start();
  };
  

  public void stop() {
      me.stop();
      hl.stop();
  };

};










