import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 * Title:        WaterMixApplet
 * Description:  Mixing water with Fuzzy Logic
 * Copyright:    Copyright (c) 2001
 * @author S. Robl <stefan@qdev.de>, A. Heinze <mail@aheinze.de>
 * @version 1.0
 */

public class panWaterMixAnim extends JPanel
{
  myDebug debug = new myDebug(5);
  public WaterContainer CupCold = new WaterContainer(20,17,50,100,3);
  public WaterContainer CupHot = new WaterContainer(190,17,50,100,3);
  public WaterContainer WaterCont = new WaterContainer(80,130,100,200,5);
  private WaterDrops wd=new WaterDrops();
  private Image dbImage;
  private Graphics dbGraphics;
  private boolean renderingRequired=true;

  private double TCold, VCold, THot, VHot, TCont, VCont;

  public panWaterMixAnim()
  {
    try 
    {
      jbInit();
    }
    catch(Exception ex) 
    {
      ex.printStackTrace();
    }
    
    CupCold.setBackColor(this.getBackground());
    CupHot.setBackColor(this.getBackground());
    WaterCont.setBackColor(this.getBackground());
  }

  void jbInit() throws Exception 
  {
    this.setLayout(null);
  }

  public void paint(Graphics g) 
  {
	debug.Write("panWaterMixAnim.paint(..)",0);

	render(false);
	
	if (dbImage!=null)
	{
      g.drawImage(dbImage,0,0,this);
    }
  }

  private String format(double value) 
  {
    return new Double(((int)(value*10))/10.0).toString();
  }

  public void setCupLabelValues(double TCold, double VCold, double THot, double VHot) 
  {
    this.TCold=TCold;
    this.VCold=VCold;
    this.THot=THot;
    this.VHot=VHot;
	renderingRequired = true;
  }

  public void setContainerLabelValues(double TCont, double VCont) 
  {
    this.TCont=TCont;
    this.VCont=VCont;
	renderingRequired = true;
  }
  
  public void initRender(boolean cold, Color coldColor, Color hotColor, int steps)
  {
    if (cold) 
    {
      wd.initValues(true, 67, 110, 80, 175, WaterCont.getWaterLevelPixel(true), 5, (float) 0.25, steps*2);
      wd.initAnimation(50, coldColor);
    } 
    else 
    {
      wd.initValues(false, 190, 110, 80, 175, WaterCont.getWaterLevelPixel(true), 5, (float) 0.25, steps*2);
      wd.initAnimation(100, hotColor);
    }
  }
  
  public void renderStep()
  {
    renderingRequired = true;
	render(true);
  }
  
  private void render(boolean doWaterDrops)
  {
	if (!renderingRequired)
		return;
	
	// initialize double-buffer
    if (dbImage == null)
    {
      dbImage = createImage(this.getSize().width, this.getSize().height);
      dbGraphics = dbImage.getGraphics();
    }
	
	// clear background
    dbGraphics.setColor(Color.lightGray);
    dbGraphics.fillRect(0, 0, this.getSize().width, this.getSize().height);

    dbGraphics.setColor(Color.black);
    dbGraphics.drawRect(0,0,this.getWidth()-1,this.getHeight()-1);

    WaterCont.paint(dbGraphics);
    CupCold.paint(dbGraphics);
    CupHot.paint(dbGraphics);
	
	if (doWaterDrops)
	{
		wd.animate(dbGraphics);
	}

    dbGraphics.setColor(Color.black);
    dbGraphics.drawString("T: " + format(TCold) +" ¡C",12,150);
    dbGraphics.drawString("V: " + format(VCold) +" l",20,170);

    dbGraphics.drawString("T: " + format(TCont) + " ¡C",105,90);
    dbGraphics.drawString("V: " + format(VCont) + " l",110,110);

    dbGraphics.drawString("T: " + format(THot) +" ¡C",190,150);
    dbGraphics.drawString("V: " + format(VHot) +" l",198,170);
	
	renderingRequired = false;
  }

  public void update(Graphics g)
  {
	if (dbImage!=null)
	{
      g.drawImage(dbImage,0,0,this);
    }
  }
}
