import java.applet.*;
import java.awt.*;
import java.lang.*;
import java.io.*;
import java.awt.event.*;

/*
<APPLET CODE="grafChoice.class" CODEBASE="." WIDTH=480 HEIGHT=300></APPLET>
*/

public class grafChoice extends Applet implements ActionListener, ItemListener
{
   TextField tf, caixa01;
   Label     ac, rotul01, rotul02;
   Choice    ch;
   Button    botao;
   double    hora, A, Y;
   Color corAzul = new Color(121,139,176);
   Color corVerdeEscuro = new Color(80,110,110);
   Color corVerdeClaro = new Color(221,245,200);
   Color corTextoClaro = new Color(226,246,231);
   Font fonte = new Font("Arial", Font.BOLD, 14);
   drawCanvas tela = new drawCanvas();

   public void init()
   {
	  resize(480,300);// área de exibição

      setLayout(new BorderLayout());
      setBackground(corAzul);

      tf = new TextField("",8);
      add("North",tf);

      add("Center", tela);

      Panel ps = new Panel();  add("South",ps);
      ps.setBackground(corVerdeClaro);
	  ac = new Label("Escolha:",Label.RIGHT);   ps.add(ac);
	  ch=new Choice();
	  ch.add("Primaveira");
	  ch.add("Verão");
	  ch.add("Outono");
	  ch.add("Inverno");
      ps.add(ch);ch.addItemListener(this);

      rotul01  = new Label("Hora:",Label.RIGHT);      ps.add(rotul01);
	  caixa01  = new TextField("12",2);               ps.add(caixa01);
      botao = new Button("Calcular!");  ps.add(botao);botao.addActionListener(this);

    }

    public void itemStateChanged(ItemEvent e)
    {
      if (e.getItem()=="Primaveira")
      {	 A=2.5;
         hora =  Double.valueOf(caixa01.getText()).doubleValue();
		 Y=A*Math.sin((hora-6)*(2*3.1416/24));    if (Y<0.00001) Y=0;
		 tf.setText(String.valueOf(Y));
		 tela.repaint();
	  }
      else
	  if (e.getItem() == "Verão")
	  {	 A=3.0;
	     hora =  Double.valueOf(caixa01.getText()).doubleValue();
		 Y=A*Math.sin((hora-6)*(2*3.1416/24));    if (Y<0.00001) Y=0;
		 tf.setText(String.valueOf(Y));
		 tela.repaint();
	  }
      else
      if (e.getItem() == "Outono")
      {	 tf.setText(String.valueOf(e.getItem()));
		 A=2.6;
		 hora =  Double.valueOf(caixa01.getText()).doubleValue();
		 Y=A*Math.sin((hora-6)*(2*3.1416/24));    if (Y<0.00001) Y=0;
		 tf.setText(String.valueOf(Y));
		 tela.repaint();
	  }
      else
      if (e.getItem() == "Inverno")
      {	 A=2.0;
         hora =  Double.valueOf(caixa01.getText()).doubleValue();
		 Y=A*Math.sin((hora-6)*(2*3.1416/24));    if (Y<0.00001) Y=0;
		 tf.setText(String.valueOf(Y));
		 tela.repaint();
	  }
    }

    public String getChoice()	{return ch.getSelectedItem();}

    public void actionPerformed(ActionEvent e)
    {
		if (e.getSource() == botao)
    	{   hora =  Double.valueOf(caixa01.getText()).doubleValue();
    	    Y=A*Math.sin((hora-6)*(2*3.1416/24));   if (Y<0.00001) Y=0;
    	    tf.setText(String.valueOf(Y));
    	    tela.repaint();
	    }
    }

    public class drawCanvas extends java.awt.Canvas
    {
    	int x,y,width,height;
        double  T, DT, Y0, T0;

        public synchronized void paint (Graphics g)
        {
           String s = getChoice();
           Dimension dm = size();
           x = dm.width / 4;
           y = dm.height / 4;
           width = dm.width / 2;
           height = dm.height / 2;

           T=0; DT=0.1; Y0=30; T0=width/24;

    	   g.setColor(Color.blue);
           g.drawRect(x,y,width,height);
           g.setColor(corVerdeClaro);
           g.fillRect( x+1,y+1,width-1,height-1);

           g.setFont(fonte);
           g.setColor(corTextoClaro);
           g.drawString("Para conhecer a radiação solar numa certa hora do dia",      40, 20);
           g.drawString("Mude a estação, mude a hora e clique o botao de cálculo",    40, 220);

    	   if (s.compareTo("Primaveira") == 0)	   {  g.setColor(corVerdeEscuro);  }
           else if (s.compareTo("Verão") == 0)	   {  g.setColor(Color.red);	   }
           else if (s.compareTo("Outono") == 0)	   {  g.setColor(Color.magenta);   }
    	   else if (s.compareTo("Inverno") == 0)   {  g.setColor(Color.blue);      }

    	   g.drawLine((int)((12)*T0),(int)((height*3/2)-Y*Y0),(int)((36)*T0),(int)((height*3/2)-Y*Y0));

    	   for (T=0;T<width/10;T=T+DT)
    	   {	g.drawOval((int)((T+12)*T0),   (int)((height*3/2)-Y*Y0),1,1);
    			g.drawLine((int)((hora+12)*T0),(int)(height*1/2),(int)((hora+12)*T0),(int)(height*3/2));
    			Y=A*Math.sin((T-6)*(2*3.1416/24));
    			if (Y<0) Y=0;
           }
        }
     }
}
