/*
Programa para construir un caracol con forma, pero cun multiples funciones

*/

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Vector;



public class caracolModificado5 extends Applet
{  

   miCanvas mc;
   Panel pnl_1;
   Panel pnl_2 = new Panel ();
   Panel pnl_3 = new Panel ();
   Panel pnl_4 = new Panel ();
   Panel pnl_5 = new Panel ();
   Panel pnl_6 = new Panel ();
  
   Label lbl_1 = new Label ("色");
   Label lbl_2 = new Label ("Zoom");
   Label lbl_3 = new Label ("貝殻の線の数");
   Label lbl_4 = new Label ("回転の数");
   Label lbl_5 = new Label ("回転90度");
   
   TextField tf_zoom = new TextField ("10");
   TextField tf_lineas = new TextField ("40");
   TextField tf_vueltas = new TextField ("3");

   Choice color = new Choice ();
   //Choice grado = new Choice ();

   Button boton_zoom = new Button ("ok");
   Button boton_lineas = new Button ("ok");
   Button boton_vueltas = new Button ("ok");
   Button boton_giros = new Button ("ok");
   
   double radio = Integer.parseInt(tf_zoom.getText());  // tam es igual a radio
   int nk = Integer.parseInt(tf_lineas.getText());  // En realidad aqui si cambio los valores, toma distintas figuras 
   int nn = Integer.parseInt(tf_vueltas.getText()); // aqui defino la cantidad de vueltas que tiene el caracol
   //int k = 0; 

public void init ()
{
      
   setBackground (Color.white);  
   setLayout (new BorderLayout ());

   add ("Center", mc = new miCanvas ());
   add ("North", pnl_1 = new Panel ());
   pnl_1.setBackground (Color.pink);
   pnl_1.setLayout (new GridLayout (2,3)); 
   
   pnl_2.add (lbl_1);
   color.add ("黒");
   color.add ("赤");
   color.add ("青");
   color.add ("緑");
   color.add ("黄色");
   color.add ("マグネンタ");
   color.add ("水色");
   color.add ("ピンク");
   color.add ("オレンジ色");
   pnl_2.add (color);
   pnl_1.add (pnl_2);
   pnl_3.add (lbl_2);
   pnl_3.add (tf_zoom);
   pnl_3.add (boton_zoom);
   pnl_1.add (pnl_3);
   pnl_4.add (lbl_3);
   pnl_4.add (tf_lineas);
   pnl_4.add (boton_lineas); 
   pnl_1.add (pnl_4);
   pnl_5.add (lbl_4);
   pnl_5.add (tf_vueltas);
   pnl_5.add (boton_vueltas);
   pnl_1.add (pnl_5);
   pnl_6.add (lbl_5);
   pnl_6.add (boton_giros);
   //pnl_6.add (grado);
   pnl_1.add (pnl_6);

// oidores de los colores
    color.addItemListener ( new java.awt.event.ItemListener ()
      {
        public void itemStateChanged (ItemEvent e)
        {
          mc.cambiaColor (color.getSelectedIndex (), Integer.parseInt(tf_zoom.getText()));
         
        }
      });
    boton_zoom.addActionListener ( new java.awt.event.ActionListener ()
      {
        public void actionPerformed (ActionEvent e)
        {
          mc.cambia_zoom( Integer.parseInt(tf_zoom.getText()));
          
        }
      });
    boton_lineas.addActionListener ( new java.awt.event.ActionListener ()
      {
        public void actionPerformed (ActionEvent e)
        {
          mc.cambia_lineas( Integer.parseInt(tf_lineas.getText()), Integer.parseInt(tf_zoom.getText()));
          
        }
      }); 
    boton_vueltas.addActionListener ( new java.awt.event.ActionListener ()
      {
        public void actionPerformed (ActionEvent e)
        {
          mc.cambia_vueltas( Integer.parseInt(tf_vueltas.getText()), Integer.parseInt(tf_zoom.getText()));
          
        }
      }); 
    boton_giros.addActionListener ( new java.awt.event.ActionListener ()
      {
        public void actionPerformed (ActionEvent e)
        {
          mc.cambiaGrado (Integer.parseInt(tf_zoom.getText()));
         
        }
      });

  }
}


class miCanvas extends Canvas {
  

  int color = 0;
  int grado = 0;
  double valor;
  int nk = 40;  // cantidad de lineas en el caracol y tambien define la curvatura 
  int nn = 3;   // cantidad de vueltas
  double radio = 10;
  double radio2 = radio / 2.2; // Aqui se define el valor y la forma del caracol.. mas o menos diferenciados   
  double k = 0.0;

 miCanvas ()
  {
    super ();
  }


public void paint(Graphics g){

   int i, j;

   double t = 6.28 /nk;
   double x,y,x1,y1; 
   double x2,y2,x3,y3;

   
   /*  --- Defino el Color del caracol  ---  */

    switch (color) {
      case 0 : g.setColor (Color.black);
               break;
      case 1 : g.setColor (Color.red);
               break;
      case 2 : g.setColor (Color.blue);
               break;
      case 3 : g.setColor (Color.green);
               break;
      case 4 : g.setColor (Color.yellow);
               break;
      case 5 : g.setColor (Color.magenta);
               break;
      case 6 : g.setColor (Color.cyan);
               break;
      case 7 : g.setColor (Color.pink);
               break;
      case 8 : g.setColor (Color.orange);
               break;
    }

/*  --  Defino el valor del radio  --  */

   if (nk <=3) {
      radio2 = radio / 1.4;
   } 
   else if (nk >= 4 & nk <=5){
      radio2 = radio / 1.5;   
   }
   else if (nk >= 6 & nk <=7){
      radio2 = radio / 1.8;   
   }
   else{
      radio2 = radio / 2;   
   }
/*  --  Fin valor nk  --   */
       
   x = radio*Math.cos(0+k);
   y = radio*Math.sin(0+k);

   x2 = radio2*Math.cos(0+k);
   y2 = radio2*Math.sin(0+k);

   
   for (i=1; i<=nn; i++){
   
     for (j=1; j<nk; j++){
       double th = t*j;    
        
/* --  Caracol parte externa -- */

       x1 = radio*Math.cos(th+k);
       y1 = radio*Math.sin(th+k);      
       //g.setColor(Color.blue); 
       g.drawLine( (int) x + 300, (int) y + 300, (int) x1 + 300, (int) y1 + 300);
       radio = radio + (radio/(nk+1));
       

/*- Caracol la parte interna  -*/

       x3 = radio2*Math.cos(th+k);
       y3 = radio2*Math.sin(th+k);      
       //g.setColor(Color.red); 
       g.drawLine( (int) x2 + 300, (int) y2 + 300, (int) x3 + 300, (int) y3 + 300);
       radio2 = radio2 + (radio2/(nk));  //+1
       

/*  --  lineas separadora del caracol  ---  */

       //g.setColor(Color.black);
       g.drawLine((int) x + 300, (int) y + 300, (int) x2 + 300, (int) y2 + 300);

/*  --  Cambio de variables  --  */
       x = x1;
       y = y1; 
       x2 = x3;
       y2 = y3; 
     
     }

       double th = t*nk;    
       x1 = radio*Math.cos(th+k);
       y1 = radio*Math.sin(th+k);      
       //g.setColor(Color.blue); 
       g.drawLine( (int) x + 300, (int) y + 300, (int) x1 + 300, (int) y1 + 300);
       radio = radio + (radio/(nk)); //+1
       

       x3 = radio2*Math.cos(th+k);
       y3 = radio2*Math.sin(th+k);      
       //g.setColor(Color.red); 
       g.drawLine( (int) x2 + 300, (int) y2 + 300, (int) x3 + 300, (int) y3 + 300);
       radio2 = radio2 + (radio2/(nk));  //+1
       

       //g.setColor(Color.black);
       g.drawLine((int) x + 300, (int) y + 300, (int) x2 + 300, (int) y2 + 300);

       x = x1;
       y = y1; 
       x2 = x3;
       y2 = y3; 

     
   } 
   g.drawLine((int) x + 300, (int) y + 300, (int) x2 + 300, (int) y2 + 300);
 
}

public void cambiaColor (int c, int r)
{
    color = c;
    radio = r;
    repaint ();
}

void cambia_zoom (int d)
{
 
    radio = d;
    repaint ();
}

void cambia_lineas (int e, int r)
{
    nk = e;
    radio = r;
    repaint ();
}

void cambia_vueltas (int d, int r)
{
 
    nn = d;
    radio = r;
    repaint ();
}

public void cambiaGrado (int r)
{
    k = k + (Math.PI/2);
    radio = r;
    repaint ();
}

}

