/* Resource simulation  -  Knut Ekker,  knut@neti.no  January 1996 */
import java.awt.*;

public class resource extends java.applet.Applet implements Runnable {
  int wx, wy, yoff,xpts[],ypts[],nx,harvest,origo, OtherHarv, FertRes;
  int OrigResource, Resource, Growth, MaxResource, PctFert, count, pause1;
  double x0,xmin,xmax,dx,ymin,ymax,dy,xscale,yscale,tmin,t,dt;
  double width, vwidth, vx;
  Thread kicker = null;
  Button Restart = new Button("Restart");
  Button Pause = new Button("Pause");
  Button Stop  = new Button("Stop Simulation");
  Choice C     = new Choice();

public void init() {
  pause1 = 10000;
  wx = size().width;
  wy = size().height;
  resize(wx,wy);
  setBackground(Color.white);
  nx = wx / 2;
  xpts = new int[nx];
  ypts = new int[nx];
  yoff = 50;
  count = 0;
  origo = yoff + 250;
  OrigResource = 50000;
  MaxResource  = 2*OrigResource;
  Growth = 12;           /*  % growth of resource */
  PctFert = 22;      /*  % of resource that may reproduce */
  OtherHarv = 2;  /* % of total resource harvested by other nations */
  harvest = 350;
  wy -= yoff;
  InitSimulation();
  Panel p = new Panel();
  p.setLayout(new FlowLayout());
  p.add(Pause);
  p.add(Restart);
  p.add(Stop);
  C.addItem("Harvest 250 tons");
  C.addItem("Harvest 300 tons");
  C.addItem("Harvest 350 tons");
  C.addItem("Harvest 400 tons");
  C.addItem("Harvest 450 tons");
  C.select ("Harvest 350 tons");
  p.add(C);
  add("North",p);
  Restart.disable();
  C.disable();
}

public void pause(int t) {
  for (int x=0;x<t;x++) {
  }
} 
public void InitSimulation() {
  x0 = 1;
  xmin = 0;
  xmax = 360;
  dx = (xmax-xmin)/(nx-1);
  xscale = (wx-0.5)/(xmax-xmin);
  for(int x=0;x<nx;x++){
    xpts[x] = x + yoff;
   }
}

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

public void MakeGraph(Graphics g) {
  int ix, iy;
  int jx, jy;

  //g.setColor(Color.black);
  //g.drawRect(0,yoff,wx-1,wy-1);

  g.setColor(Color.red);
  ix = 1+yoff;
  jx = 360+yoff;
  iy = 1+yoff;
  jy = 250+yoff;
  g.drawRect(ix,iy,jx,jy);

  g.setColor(Color.blue);
  
  Resource = OrigResource; 
  for(int x=0;x<nx;x++) {
    ypts[x] = origo - (int)(Resource - harvest)/500;
    FertRes = (int)(Resource * PctFert/100);
    if (Resource < MaxResource)  
       Resource = Resource + (int)(FertRes*Growth/100)-
             ((int)(Resource*OtherHarv/100)+harvest);
     else Resource = Resource - 5*OtherHarv;
    if (Resource < 0) Resource = 0;
  }
  for (int x = 0 ; x < nx-1 ; x++ ) 
    g.drawLine(xpts[x], ypts[x], xpts [x+1], ypts[x+1]);

  g.setColor(Color.black);
  
  Resource = OrigResource;
  for(int x=0;x<nx;x++) {
    FertRes = (int)(Resource*PctFert/100);
    ypts[x] = origo - (int)( FertRes*Growth/(100*500) );
    if (Resource < MaxResource) 
       Resource = Resource + (int)(FertRes*Growth/100)-
             ((int)(Resource*OtherHarv/100)+harvest);
     else Resource = Resource - 5*OtherHarv;
    if (Resource < 0) Resource = 0;
  }
  for (int x = 0; x < nx-1 ; x++ ) 
    g.drawLine(xpts[x], ypts[x], xpts[x+1], ypts[x+1]);
}

public void run () {
  count = 0;
  while ( kicker != null ){
    /* step(); step(); step(); */
    t += dt;
    count += 1;
    if (count < 2) repaint();
    try {
      Thread.sleep(60);
    } catch (InterruptedException e) {
      break;
    }
  }
}
/*  Start the applet by forking an animation thread.  */

public void start() {
  if (kicker == null) {
    kicker = new Thread(this);
    kicker.start();
  }
}

/* Stop the applet. The thread will exit because kicker is set to null. */

public void stop() {
  if (kicker != null) {
    kicker.stop();
    kicker = null;
  }
}


public boolean mouseDown(java.awt.Event evt, int x, int y) {
 /* 
  requestFocus();
  if (null != kicker ) stop();
  else start();
  */
  return true;
}

public boolean action(Event evt, Object arg) {
  if (evt.target instanceof Choice) {
   if("Harvest 250 tons".equals(arg)) {
      harvest = 250;
    } else if("Harvest 300 tons".equals(arg)) {
        harvest = 300;
    } else if("Harvest 350 tons".equals(arg)) {
        harvest = 350;
    } else if("Harvest 400 tons".equals(arg)) {
        harvest = 400;
    } else if("Harvest 450 tons".equals(arg)) {
        harvest = 450;
    }
   } else {
     if ("Stop Simulation".equals(arg)) {
        stop();
        Stop.disable();
        Restart.enable();
        C.enable();
        Pause.disable();
      } else if ("Restart".equals(arg)) {
        Stop.enable();
        Restart.disable();
        C.disable();
        Pause.enable();
        Pause.setLabel("Pause");
        InitSimulation();
        start();
      } else if ("Pause".equals(arg)) {
        stop();
        Pause.setLabel("Resume");
      } else if ("Resume".equals(arg)) {
        start();
        Pause.setLabel("Pause");
      }
   }
   return true;
}
}
