import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import com.borland.jbcl.layout.*;

/**
 * @author  Heinze, Pausch, Robl
 * @version 1.0
 */

public class VirtualLife extends Applet implements Runnable
{
  private static final int MOUSE_LEFTBUTTON=1;
  private static final int MOUSE_RIGHTBUTTON=2;

  PlayField playfield;
  Thread thread;
  Image img, LifeChartImg, MicChartImg;
  Graphics gfx, LifeChartgfx, MicChartgfx;
  int paintmode, brushsize, mousebutton=0;

  private int LifeChartWidth=190;
  private int LifeChartHeight=100;
  private int LifeChartLeft=10;
  private int LifeChartTop=470;
  private int LifeChartScale=2;
  private int LifeChartScaleCounter=LifeChartScale;
  Vector LifeChart = new Vector(LifeChartWidth+20);

  private int MicChartWidth=190;
  private int MicChartHeight=100;
  private int MicChartLeft=10;
  private int MicChartTop=580;
  private int MicChartScale=1;
  private int MicChartScaleCounter=MicChartScale;
  Vector MicChart = new Vector(MicChartWidth+20);

  int sleeptime, drawx, drawy, draww, drawh, pixelsize=6;
  boolean pausemode=false;

  boolean isStandalone = false;
  Panel panel1 = new Panel();
  Label lblDrawModeText = new Label();
  Label lblSpeedText = new Label();
  Label lblSpeed = new Label();
  Label lblBrushSizeText = new Label();
  Panel panel2 = new Panel();
  Label lblLifesText = new Label();
  Label lblLifes = new Label();
  Panel panel7 = new Panel();
  Button btnsize1 = new Button();
  Button btnsize17 = new Button();
  Button btnsize3 = new Button();
  Button btnsize35 = new Button();
  Button btnsize9 = new Button();
  Button btnsize70 = new Button();

  Panel panel8 = new Panel();
  Button btnrandom = new Button();
  Button btnverybad = new Button();
  Button btnnothing = new Button();
  Button btnverygood = new Button();
  Button btnbad = new Button();
  Button btngood = new Button();
  GridLayout gridLayout1 = new GridLayout();
  Panel panel4 = new Panel();
  VerticalFlowLayout verticalFlowLayout3 = new VerticalFlowLayout();

  Panel panel9 = new Panel();
  Label lblEnergyLossText = new Label();
  Scrollbar scbEnergyLoss = new Scrollbar();
  Label lblEnergyLoss = new Label();
  VerticalFlowLayout verticalFlowLayout4 = new VerticalFlowLayout();
  Panel panel3 = new Panel();
  GridLayout gridLayout2 = new GridLayout();
  Panel panel10 = new Panel();
  Panel panel11 = new Panel();
  Scrollbar scbSpeed = new Scrollbar();
  VerticalFlowLayout verticalFlowLayout5 = new VerticalFlowLayout();
  GridLayout gridLayout3 = new GridLayout();
  Panel panel5 = new Panel();
  Panel panel6 = new Panel();
  Panel panel13 = new Panel();
  Panel panel12 = new Panel();
  Button btndeath = new Button();
  Button btnnewlife = new Button();
  VerticalFlowLayout verticalFlowLayout2 = new VerticalFlowLayout();
  GridLayout gridLayout5 = new GridLayout();
  GridLayout gridLayout6 = new GridLayout();
  GridLayout gridLayout7 = new GridLayout();
  GridLayout gridLayout8 = new GridLayout();
  VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout();
  Panel panel14 = new Panel();
  Panel panel15 = new Panel();
  GridLayout gridLayout9 = new GridLayout();
  VerticalFlowLayout verticalFlowLayout6 = new VerticalFlowLayout();
  Panel panel16 = new Panel();
  Button btnpause = new Button();
  Button btnkillall = new Button();
  Button btnfill = new Button();
  GridLayout gridLayout4 = new GridLayout();
  Panel panel17 = new Panel();
  VerticalFlowLayout verticalFlowLayout7 = new VerticalFlowLayout();


  /** applet constructor */
  public VirtualLife()
  {
  }

  /** initializes the applet (GUI!) */
  public void init()
  {
    try
    {
      jbInit();

      draww=(draww/pixelsize)*pixelsize;
      drawh=(drawh/pixelsize)*pixelsize;

      img=createImage(draww, drawh);
      gfx=img.getGraphics();
      gfx.setColor(new Color(0,0,0));
      gfx.fillRect(0, 0, draww, drawh);

      playfield=new PlayField(draww/pixelsize, drawh/pixelsize, pixelsize);
      playfield.set_graphics(gfx);
      playfield.draw_all();

      scbEnergyLoss_adjustmentValueChanged(null);
      scbSpeed_adjustmentValueChanged(null);

      paintmode = Food.TYPE_GOOD;
      setbutton_drawmode(paintmode);

      brushsize = 9;
      setbutton_brushsize(brushsize);

      LifeChartImg=createImage(LifeChartWidth, LifeChartHeight);
      LifeChartgfx=LifeChartImg.getGraphics();

      MicChartImg=createImage(MicChartWidth, MicChartHeight);
      MicChartgfx=MicChartImg.getGraphics();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }

    // initialize LifeChart vector
    for (int i=0;i<=LifeChartWidth;i++)
      LifeChart.addElement(new Integer(0));

    playfield.manualdraw(playfield.playfield_width/2,playfield.playfield_height/2, PlayField.TYPE_NEWLIFE, 1);
    selectmicrobe(0, 0);
  }

  /** GUI initialisation */
  private void jbInit() throws Exception
  {
    this.setLayout(null);

    drawy=2;
    MicChartTop = this.getBounds().height- MicChartHeight;
    MicChartTop -= MicChartTop%pixelsize + drawy;
    LifeChartTop = MicChartTop - LifeChartHeight -5;

    panel1.setBackground(new Color(18, 6, 64));
    panel1.setBounds(new Rectangle(2, 2, 200, 450));
    panel1.setLayout(verticalFlowLayout1);

    MicChartLeft=LifeChartLeft=panel1.getBounds().x;
    drawx=(panel1.getBounds().x*2)+panel1.getBounds().width;
    draww=this.getSize().width-drawx;
    drawh=this.getSize().height-drawy;
    MicChartWidth=LifeChartWidth=panel1.getBounds().width;

    lblDrawModeText.setText("Draw Mode:");
    lblSpeedText.setText("Speed:");
    lblSpeed.setAlignment(2);
    lblSpeed.setText("        0");
    this.setBackground(new Color(18, 6, 149));

    this.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        this_mousePressed(e);
      }
    });

    this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
      public void mouseDragged(MouseEvent e) {
        MouseDrag(e);
      }
    });

    lblBrushSizeText.setBackground(Color.orange);
    lblBrushSizeText.setText("BrushSize:");

    lblLifesText.setText("Lifes:");
    lblLifes.setAlignment(2);
    lblLifes.setBackground(Color.orange);
    lblLifes.setText("0");
    panel2.setLayout(verticalFlowLayout6);
    panel7.setLayout(gridLayout1);

    btnsize1.setLabel("1");
    btnsize1.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        btnsize1_actionPerformed(e);
      }
    });

    btnsize17.setLabel("17");
    btnsize17.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        btnsize17_actionPerformed(e);
      }
    });

    btnsize3.setLabel("3");
    btnsize3.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        btnsize3_actionPerformed(e);
      }
    });

    btnsize35.setLabel("35");
    btnsize35.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        btnsize35_actionPerformed(e);
      }
    });

    btnsize9.setLabel("9");
    btnsize9.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        btnsize9_actionPerformed(e);
      }
    });
    btnsize70.setLabel("70");
    btnsize70.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        btnsize70_actionPerformed(e);
      }
    });
    btnrandom.setFont(new java.awt.Font("Dialog", 0, 11));
    btnrandom.setLabel("Random");
    btnrandom.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        btnrandom_actionPerformed(e);
      }
    });
    panel8.setLayout(verticalFlowLayout2);
    btnverybad.setFont(new java.awt.Font("Dialog", 0, 11));
    btnverybad.setLabel("Very Bad");
    btnverybad.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        btnverybad_actionPerformed(e);
      }
    });
    btnnothing.setFont(new java.awt.Font("Dialog", 0, 11));
    btnnothing.setLabel("Nothing");
    btnnothing.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        btnnothing_actionPerformed(e);
      }
    });
    btnverygood.setFont(new java.awt.Font("Dialog", 0, 11));
    btnverygood.setLabel("Very Good");
    btnverygood.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        btnverygood_actionPerformed(e);
      }
    });
    btnbad.setFont(new java.awt.Font("Dialog", 0, 11));
    btnbad.setLabel("Bad");
    btnbad.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        btnbad_actionPerformed(e);
      }
    });
    btngood.setFont(new java.awt.Font("Dialog", 0, 11));
    btngood.setLabel("Good");
    btngood.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        btngood_actionPerformed(e);
      }
    });


    panel8.setBackground(Color.orange);
    panel7.setBackground(Color.orange);
    panel4.setLayout(verticalFlowLayout3);
    panel4.setBackground(Color.orange);
    lblEnergyLossText.setText("Energy Loss per step:");
    scbEnergyLoss.setMaximum(60);
    scbEnergyLoss.setOrientation(0);
    scbEnergyLoss.setValue(2);
    scbEnergyLoss.addAdjustmentListener(new java.awt.event.AdjustmentListener() {
      public void adjustmentValueChanged(AdjustmentEvent e) {
        scbEnergyLoss_adjustmentValueChanged(e);
      }
    });
    lblEnergyLoss.setAlignment(2);
    lblEnergyLoss.setText("        0");
    panel9.setLayout(verticalFlowLayout4);
    panel3.setLayout(gridLayout2);
    panel9.setBackground(Color.orange);
    scbSpeed.setOrientation(0);
    scbSpeed.setValue(40);
    scbSpeed.addAdjustmentListener(new java.awt.event.AdjustmentListener() {
      public void adjustmentValueChanged(AdjustmentEvent e) {
        scbSpeed_adjustmentValueChanged(e);
      }
    });
    panel10.setLayout(verticalFlowLayout5);
    panel11.setLayout(gridLayout3);
    panel10.setBackground(Color.orange);
    panel2.setBackground(Color.orange);
    panel5.setLayout(gridLayout8);
    panel13.setLayout(gridLayout7);
    btndeath.setFont(new java.awt.Font("Dialog", 0, 11));
    btndeath.setLabel("Death");
    btndeath.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        btndeath_actionPerformed(e);
      }
    });

    btnnewlife.setFont(new java.awt.Font("Dialog", 0, 11));
    btnnewlife.setLabel("New Life");
    btnnewlife.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        btnnewlife_actionPerformed(e);
      }
    });

    panel6.setLayout(gridLayout5);
    panel12.setLayout(gridLayout6);
    panel15.setLayout(gridLayout9);
    panel15.setBackground(Color.orange);
    btnpause.setLabel("Pause");
    btnpause.addActionListener(new java.awt.event.ActionListener()
    {

      public void actionPerformed(ActionEvent e)
      {
        btnpause_actionPerformed(e);
      }
    });
    btnkillall.setLabel("Kill All");
    btnkillall.addActionListener(new java.awt.event.ActionListener()
    {

      public void actionPerformed(ActionEvent e)
      {
        btnkillall_actionPerformed(e);
      }
    });
    btnfill.setActionCommand("Fill");
    btnfill.setLabel("Fill");
    btnfill.addActionListener(new java.awt.event.ActionListener()
    {

      public void actionPerformed(ActionEvent e)
      {
        btnfill_actionPerformed(e);
      }
    });
    panel16.setBackground(Color.orange);
    panel16.setLayout(gridLayout4);
    panel17.setLayout(verticalFlowLayout7);
    panel17.setBackground(Color.orange);
    this.add(panel1, null);
    panel1.add(panel2, null);
    panel2.add(panel15, null);
    panel15.add(lblLifesText, null);
    panel15.add(lblLifes, null);
    panel1.add(panel10, null);
    panel10.add(lblSpeedText, null);
    panel10.add(panel11, null);
    panel11.add(scbSpeed, null);
    panel11.add(lblSpeed, null);
    panel1.add(panel9, null);
    panel9.add(lblEnergyLossText, null);
    panel9.add(panel3, null);

    panel3.add(scbEnergyLoss, null);
    panel3.add(lblEnergyLoss, null);
    panel1.add(panel4, null);
    panel4.add(lblBrushSizeText, null);
    panel4.add(panel7, null);
    panel7.add(btnsize1, null);
    panel7.add(btnsize3, null);
    panel7.add(btnsize9, null);
    panel7.add(btnsize17, null);
    panel7.add(btnsize35, null);
    panel7.add(btnsize70, null);
    panel1.add(panel8, null);
    panel8.add(lblDrawModeText, null);

    panel8.add(panel5, null);
    panel5.add(btnverygood, null);
    panel5.add(btngood, null);
    panel8.add(panel6, null);
    panel6.add(btnverybad, null);
    panel6.add(btnbad, null);
    panel8.add(panel13, null);

    panel13.add(btnrandom, null);
    panel13.add(btnnothing, null);
    panel8.add(panel14, null);
    panel8.add(panel12, null);
    panel12.add(btnnewlife, null);
    panel12.add(btndeath, null);
    panel1.add(panel17, null);
    panel17.add(panel16, null);
    panel16.add(btnpause, null);
    panel16.add(btnkillall, null);
    panel16.add(btnfill, null);
  }

  /** starts the thread for the graphical display */
  public void start()
  {
    if (thread==null)
    {
      thread=new Thread(this);
      thread.start();
    }
  }

  /** stops the thread when quitting the applet */
  public void stop()
  {
    if (thread!=null)
    {
      thread.stop();
      thread=null;
    }
  }

  /** the display-thread draws endless every "sleeptime" milliseconds the playfield */
  public void run()
  {
    while (true)
    {
      repaint();
      try { Thread.sleep(sleeptime); }
      catch (InterruptedException e) {}
    }
  }

  /** draws the completely applet */
  public void paint(Graphics g)
  {
    if (!pausemode)
    {
      playfield.refresh();

      refresh_lbllifes();

      int miccount=playfield.get_microbe_count();

      if (miccount==0)
        sleeptime=250;

      paintLifeChart(miccount);

      Microbe selmic=playfield.get_selectedmic();
      if (selmic!=null)
        paintMicChart(selmic.getenergy());
    }

    g.drawImage(img, drawx, drawy, this);
    g.drawImage(LifeChartImg, LifeChartLeft, LifeChartTop, this);
    g.drawImage(MicChartImg, MicChartLeft, MicChartTop, this);
  }

  /**
  * selects a microbe
  * @param x x position
  * @param y y position
  */
  void selectmicrobe(int x, int y)
  {
    // reinitialize MicChart vector
    MicChart.removeAllElements();
    for (int i=0;i<=MicChartWidth;i++)
      MicChart.addElement(new Integer(0));
    playfield.set_selectedmic(x, y);
  }

  /** refreshes the label lbllife */
  private void refresh_lbllifes()
  {
    lblLifes.setText(new Integer(playfield.get_microbe_count()).toString());
  }

  /** every update has a redraw as consequence */
  public void update(Graphics g)
  {
    paint(g);
  }

  /**
  * draws the microbe chart
  * @param energy current selected microbe's energy level
  */
  private void paintMicChart(int energy)
  {
    if (MicChartScaleCounter++>MicChartScale)
    {
      MicChartScaleCounter=0;

      if (playfield.get_selectedmic()!=null)
      {
        MicChart.removeElementAt(0);
        MicChart.addElement(new Integer(energy));
      }

      MicChartgfx.setColor(panel1.getBackground());
      MicChartgfx.fillRect(0, 0, MicChartWidth, MicChartHeight);

      int max=1100; // maximum scale value

      MicChartgfx.setColor(panel2.getBackground());
      MicChartgfx.setFont(new Font("Arial",0,10));
      MicChartgfx.drawString(new Integer(max).toString(),2,10);
      MicChartgfx.drawString("0",2,MicChartHeight-2);

      MicChartgfx.setColor(new Color(120,150,120));
      MicChartgfx.setFont(new Font("Serif",0,24));
      String str = "Blue MicPower";
      MicChartgfx.drawString(str,(MicChartWidth-MicChartgfx.getFontMetrics().stringWidth(str))/2,(MicChartHeight+(MicChartgfx.getFontMetrics().getHeight()/2))/2);

      MicChartgfx.setColor(new Color(0,255,0));
      for (int i=1;i<MicChartWidth;i++)
      {
        MicChartgfx.drawLine(i-1,
                              MicChartHeight-(int)((double)((Integer)MicChart.elementAt(i-1)).intValue()/max*MicChartHeight),
                              i,
                              MicChartHeight-(int)((double)((Integer)MicChart.elementAt(i)).intValue()/max*MicChartHeight));
      }
    }
  }

  /**
  * draws the life chart
  * @param miccount current number of microbes
  */
  private void paintLifeChart(int miccount)
  {
    if (LifeChartScaleCounter++>LifeChartScale)
    {
      LifeChartScaleCounter=0;
      LifeChart.removeElementAt(0);
      LifeChart.addElement(new Integer(miccount));

      LifeChartgfx.setColor(panel1.getBackground());
      LifeChartgfx.fillRect(0, 0, LifeChartWidth, LifeChartHeight);

      // search for the biggest value
      int max=0;
      for (int i=0;i<LifeChartWidth;i++)
        if(((Integer)LifeChart.elementAt(i)).intValue()>max)
          max=((Integer)LifeChart.elementAt(i)).intValue();

      max=(int)((double)max*1.10);

      LifeChartgfx.setColor(panel2.getBackground());
      LifeChartgfx.setFont(new Font("Arial",0,10));
      LifeChartgfx.drawString(new Integer(max).toString(),2,10);
      LifeChartgfx.drawString("0",2,LifeChartHeight-2);

      LifeChartgfx.setColor(new Color(120,150,120));
      LifeChartgfx.setFont(new Font("Serif",0,24));
      String str = "Lifes";
      LifeChartgfx.drawString(str,(LifeChartWidth-LifeChartgfx.getFontMetrics().stringWidth(str))/2,(LifeChartHeight+(LifeChartgfx.getFontMetrics().getHeight()/2))/2);


      LifeChartgfx.setColor(new Color(255,255,0));
      for (int i=1;i<LifeChartWidth;i++)
      {
        LifeChartgfx.drawLine(i-1,
                              LifeChartHeight-(int)((double)((Integer)LifeChart.elementAt(i-1)).intValue()/max*LifeChartHeight),
                              i,
                              LifeChartHeight-(int)((double)((Integer)LifeChart.elementAt(i)).intValue()/max*LifeChartHeight));
      }
    }
  }

  void scbEnergyLoss_adjustmentValueChanged(AdjustmentEvent e)
  {
    lblEnergyLoss.setText(new Integer(scbEnergyLoss.getValue()).toString());
    playfield.setenergyloss(new Integer(scbEnergyLoss.getValue()).intValue());
  }

  void scbSpeed_adjustmentValueChanged(AdjustmentEvent e)
  {
    lblSpeed.setText(new Integer(sleeptime=scbSpeed.getValue()).toString());
    sleeptime=100-sleeptime;
  }

  void MouseDrag(MouseEvent e)
  {
    if (mousebutton==MOUSE_LEFTBUTTON) // only draw if the left mouse button is pressed
    {
      int x=e.getX()-drawx, y=e.getY()-drawy;
      x/=pixelsize;
      y/=pixelsize;
      playfield.manualdraw(x, y, paintmode, brushsize);

      if (paintmode==PlayField.TYPE_DEATH || paintmode==PlayField.TYPE_NEWLIFE)
        refresh_lbllifes();

      // reset sleeptime
      if (paintmode == PlayField.TYPE_NEWLIFE)
        scbSpeed_adjustmentValueChanged(null);

      if (playfield.get_microbe_count()==0)
        paint(getGraphics());
    }
  }

  void this_mousePressed(MouseEvent e)
  {
    if (e.isMetaDown()) // right mouse button: select microbe
    {
      mousebutton=MOUSE_RIGHTBUTTON;
      selectmicrobe((e.getX()-drawx)/pixelsize,(e.getY()-drawy)/pixelsize);
    }
    else // left mouse button: draw mode
    {
      mousebutton=MOUSE_LEFTBUTTON;
      MouseDrag(e);
    }
  }

  /**
  * changes the caption of the brushsize buttons to bold depending of the selected mode
  * @param brsize current brush size
  */
  void setbutton_brushsize(int brsize)
  {
    Font set_font = new Font("Dialog",1,12);
    Font unset_font = new Font("Dialog",0,12);

    if (brsize == 1)
      this.btnsize1.setFont(set_font);
    else
      this.btnsize1.setFont(unset_font);
    if (brsize == 3)
      this.btnsize3.setFont(set_font);
    else
      this.btnsize3.setFont(unset_font);
    if (brsize == 9)
      this.btnsize9.setFont(set_font);
    else
      this.btnsize9.setFont(unset_font);
    if (brsize == 17)
      this.btnsize17.setFont(set_font);
    else
      this.btnsize17.setFont(unset_font);
    if (brsize == 35)
      this.btnsize35.setFont(set_font);
    else
      this.btnsize35.setFont(unset_font);
    if (brsize == 70)
      this.btnsize70.setFont(set_font);
    else
      this.btnsize70.setFont(unset_font);
  }

  /**
  * changes the caption of the drawmode buttons to bold depending of the selected mode
  * @param drmode current draw mode
  */
  void setbutton_drawmode(int drmode)
  {
    Font set_font = new Font("Dialog",1,12);
    Font unset_font = new Font("Dialog",0,12);

    // draw modes for food and life
    if (drmode == PlayField.TYPE_RANDOMFOOD)
      this.btnrandom.setFont(set_font);
    else
      this.btnrandom.setFont(unset_font);
    if (drmode == Food.TYPE_VERYBAD)
      this.btnverybad.setFont(set_font);
    else
      this.btnverybad.setFont(unset_font);
    if (drmode == Food.TYPE_BAD)
      this.btnbad.setFont(set_font);
    else
      this.btnbad.setFont(unset_font);
    if (drmode == Food.TYPE_NOTHING)
      this.btnnothing.setFont(set_font);
    else
      this.btnnothing.setFont(unset_font);
    if (drmode == Food.TYPE_GOOD)
      this.btngood.setFont(set_font);
    else
      this.btngood.setFont(unset_font);
    if (drmode == Food.TYPE_VERYGOOD)
      this.btnverygood.setFont(set_font);
    else
      this.btnverygood.setFont(unset_font);

    if (drmode == PlayField.TYPE_NEWLIFE)
      this.btnnewlife.setFont(set_font);
    else
      this.btnnewlife.setFont(unset_font);
    if (drmode == PlayField.TYPE_DEATH)
      this.btndeath.setFont(set_font);
    else
      this.btndeath.setFont(unset_font);
  }

  void btnsize1_actionPerformed(ActionEvent e)
  {
    brushsize = 1;
    setbutton_brushsize(brushsize);
  }
  void btnsize3_actionPerformed(ActionEvent e)
  {
    brushsize = 3;
    setbutton_brushsize(brushsize);
  }
  void btnsize9_actionPerformed(ActionEvent e)
  {
    brushsize = 9;
    setbutton_brushsize(brushsize);
  }
  void btnsize17_actionPerformed(ActionEvent e)
  {
    brushsize = 17;
    setbutton_brushsize(brushsize);
  }
  void btnsize35_actionPerformed(ActionEvent e)
  {
    brushsize = 35;
    setbutton_brushsize(brushsize);
  }
  void btnsize70_actionPerformed(ActionEvent e)
  {
    brushsize = 70;
    setbutton_brushsize(brushsize);
  }

  void btnrandom_actionPerformed(ActionEvent e)
  {
    paintmode = PlayField.TYPE_RANDOMFOOD;
    setbutton_drawmode(paintmode);
  }

  void btnverybad_actionPerformed(ActionEvent e)
  {
    paintmode = Food.TYPE_VERYBAD;
    setbutton_drawmode(paintmode);
  }

  void btnbad_actionPerformed(ActionEvent e)
  {
    paintmode = Food.TYPE_BAD;
    setbutton_drawmode(paintmode);
  }

  void btnnothing_actionPerformed(ActionEvent e)
  {
    paintmode = Food.TYPE_NOTHING;
    setbutton_drawmode(paintmode);
  }

  void btngood_actionPerformed(ActionEvent e)
  {
    paintmode = Food.TYPE_GOOD;
    setbutton_drawmode(paintmode);
  }

  void btnverygood_actionPerformed(ActionEvent e)
  {
    paintmode = Food.TYPE_VERYGOOD;
    setbutton_drawmode(paintmode);
  }

  void btnnewlife_actionPerformed(ActionEvent e)
  {
    paintmode = PlayField.TYPE_NEWLIFE; // new life
    setbutton_drawmode(paintmode);
    brushsize = 1; // it is helpful to have only a one pixel sized brush as default for drawing new microbes
    setbutton_brushsize(brushsize);
  }

  void btndeath_actionPerformed(ActionEvent e)
  {
    paintmode = PlayField.TYPE_DEATH;  // kill microbe
    setbutton_drawmode(paintmode);
  }

  void btnpause_actionPerformed(ActionEvent e)
  {
    Font set_font = new Font("Dialog",1,12);
    Font unset_font = new Font("Dialog",0,12);

    pausemode=!pausemode;

    if (pausemode)
    {
      this.btnpause.setFont(set_font);
      sleeptime=250;
    }
    else
    {
      this.btnpause.setFont(unset_font);
      scbSpeed_adjustmentValueChanged(null);
    }
  }

  void btnkillall_actionPerformed(ActionEvent e)
  {
    playfield.remove_allmicrobes();
  }

  void btnfill_actionPerformed(ActionEvent e)
  {
    if (paintmode!=PlayField.TYPE_DEATH && paintmode!=PlayField.TYPE_NEWLIFE)
    {
      playfield.fill_all(paintmode);
      playfield.draw_all();
    }
  }
}