import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.List;
import java.awt.image.BufferedImage;
import java.awt.geom.GeneralPath;
import javax.swing.*;

public class HuseyinNazli extends Applet 
implements Runnable, ActionListener, ItemListener
{
 //Image sekil=null;
 //Image applet=null;
 //Graphics appG,sekilG;
 int x,y,x1,y1;
 Lumatron l;
 Color renk;
 int treadtime;
 private Thread t1;
 private boolean s1,flag;
 int radius;
 private List renkler;
 private List sekiller;
 private Panel P;
 private TextField te1;
 int graphCode;
 int startAngle=0;
 int arcAngle=90;
 private String toSekil[]={"grafik 1 : tam  daire",
                           "grafik 2 : iki  daire dilimi",
                           "grafik 3 : dört daire dilimi",
                           "grafik 4 : sekiz daire dilimi",
                           "grafik 5 : üçken yıldız",
                           "grafik 6 : dörtgen yıldız",
                           "grafik 7 : beşgen yıldız",
                           "grafik 8 : altıgen yıldız",
                           "grafik 9 : üçken",
                           "grafik 10: dörtgen",
                           "grafik 11: beşgen",
                           "grafik 12: altıgen" };

 public void init()
 {
 x=500; //başlangıç noktası x ekseni
 y=340; //başlangıç noktası y ekseni

 graphCode=1;
 P=new Panel();
 te1=new TextField("Lumatron. Copyright :Hüseyin Nazlı; Dr.M. Turhan Çoban");
 te1.setBackground(Color.black);
 P.setLayout(new GridLayout(1,5));
 P.setBackground(Color.black);
 P.setForeground(Color.yellow);
 setBackground(Color.black);
 s1=true;
 l=new Lumatron();
 renk=l.getColor();
 treadtime=l.flash;
 radius=10;
 renkler=new List(1,false);
 renkler.setBackground(Color.black);
 renkler.addActionListener(this);
 renkler.addItemListener(this);
 sekiller=new List(1,false);
 sekiller.setBackground(Color.black);
 sekiller.addActionListener(this);
 sekiller.addItemListener(this);
 for(int i=0;i<17;i++)
 {
   renkler.add(l.toString(i));
 }
 for(int i=0;i<12;i++)
 {
   sekiller.add(toSekil[i]);
 }
 P.add(renkler);
 P.add(sekiller);
 P.add(te1);
 setLayout(new BorderLayout());
 add(P,BorderLayout.NORTH);
 showStatus(l.toString1());
}

 public void start()
 {
 t1=new Thread(this,"T1");
 t1.start();
  showStatus(l.toString1());
 }

 public void stop()
 {
 t1=null;
 }

 public void actionPerformed(ActionEvent e)
 {
   for(int i=0;i<17;i++)
   {
     if(renkler.getSelectedIndex()==i)
     {
     t1.suspend();
     s1=true;
     l.setLumatron(i);
     renk=l.getColor();
     t1.resume();
     s1=false;
     showStatus(l.toString1(i));
     }
   }
   for(int i=0;i<12;i++)
   {
     if(sekiller.getSelectedIndex()==i)
     {
     t1.suspend();
     s1=true;
     graphCode=i+1;
     t1.resume();
     s1=false;
     }
   }
/*
 if(e.getSource()==button1)
   if(s1)
   {
   showStatus("baslatildi");
   t1.resume();
   s1=false;
   }
   else
   {
   showStatus("durduruldu");
   t1.suspend();
   s1=true;
   }
*/
 }

 public void run()
 {
 while(true)
 {
 try{ Thread.sleep(l.flash);}
 catch(InterruptedException e){e.printStackTrace();}
 if(flag)
    radius+=10;
 else
    radius-=10;
 startAngle+=2%360;
 repaint();
 if(radius>600 ) flag=false;
 else if(radius<0) flag=true;
 }
 }//end of run

 public void itemStateChanged(ItemEvent e)
 {
 showStatus(l.toString1());
 }//end of itemStateChange

 public void paint(Graphics g)
 {
  //Graphics2D g=(Graphics2D)g1;
  //g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
  //setBackground(Color.black);
/*
  if(sekil==null)
  {
  x1=getBounds().width;
  y1=getBounds().height;
  applet=new BufferedImage(x1,y1,BufferedImage.TYPE_INT_RGB);
  sekil=new BufferedImage(x1,y1,BufferedImage.TYPE_INT_RGB);
  appG=applet.getGraphics();
  sekilG=sekil.getGraphics();
  sekilG.setColor(Color.black);
  }
*/
  if(graphCode==1)
  {
   g.setColor(renk);
   g.fillOval(x-radius/2,y-radius/2,radius,radius);
  }
  else if(graphCode==2)
  {
   arcAngle=160;
   //g.setColor(Color.black);
   //g.fillOval(x-radius/2,y-radius/2,radius,radius);
   g.setColor(renk);
   g.fillArc(x-radius/2,y-radius/2,radius,radius,startAngle,arcAngle);
   g.fillArc(x-radius/2,y-radius/2,radius,radius,startAngle+180,arcAngle);
  }
  else if(graphCode==3)
  {
   arcAngle=80;
   //g.setColor(Color.black);
   //g.fillOval(y-radius/2,y-radius/2,radius,radius);
   g.setColor(renk);
   g.fillArc(x-radius/2,y-radius/2,radius,radius,startAngle,arcAngle);
   g.fillArc(x-radius/2,y-radius/2,radius,radius,startAngle+90,arcAngle);
   g.fillArc(x-radius/2,y-radius/2,radius,radius,startAngle+180,arcAngle);
   g.fillArc(x-radius/2,y-radius/2,radius,radius,startAngle+270,arcAngle);
  }
  else if(graphCode==4)
  {
   arcAngle=40;
   //g.setColor(Color.black);
   //g.fillOval(y-radius/2,y-radius/2,radius,radius);
   g.setColor(renk);
   g.fillArc(x-radius/2,y-radius/2,radius,radius,startAngle,arcAngle);
   g.fillArc(x-radius/2,y-radius/2,radius,radius,startAngle+45,arcAngle);
   g.fillArc(x-radius/2,y-radius/2,radius,radius,startAngle+90,arcAngle);
   g.fillArc(x-radius/2,y-radius/2,radius,radius,startAngle+135,arcAngle);
   g.fillArc(x-radius/2,y-radius/2,radius,radius,startAngle+180,arcAngle);
   g.fillArc(x-radius/2,y-radius/2,radius,radius,startAngle+225,arcAngle);
   g.fillArc(x-radius/2,y-radius/2,radius,radius,startAngle+270,arcAngle);
   g.fillArc(x-radius/2,y-radius/2,radius,radius,startAngle+315,arcAngle);
  }
  else if(graphCode==5)
  {
   g.setColor(renk);
   fillYildiz(g,x,y,3,radius/2,startAngle);
  }
  else if(graphCode==6)
  {
   g.setColor(renk);
   fillYildiz(g,x,y,4,radius/2,startAngle);
  }
  else if(graphCode==7)
  {
   g.setColor(renk);
   fillYildiz(g,x,y,5,radius/2,startAngle);
  }
  else if(graphCode==8)
  {
   g.setColor(renk);
   fillYildiz(g,x,y,6,radius/2,startAngle);
  }
    else if(graphCode==9)
  {
   g.setColor(renk);
   fillEskenarcokgen(g,x,y,3,radius/2,startAngle);
  }
  else if(graphCode==10)
  {
   g.setColor(renk);
   fillEskenarcokgen(g,x,y,4,radius/2,startAngle);
  }
  else if(graphCode==11)
  {
   g.setColor(renk);
   fillEskenarcokgen(g,x,y,5,radius/2,startAngle);
  }
  else if(graphCode==12)
  {
   g.setColor(renk);
   fillEskenarcokgen(g,x,y,6,radius/2,startAngle);
  }


 }//end of paint
 private void delay(int millis)
 {
 try{Thread.sleep(millis);}catch(Exception inored){}
 }
 public static void fillYildiz(Graphics g,int xi,int yi, int n,int yildizboyu,double aci )
{
   // açı derece cinsinden verilmiştir
   double teta=2.0*Math.PI/n;
   double R=yildizboyu;
   double r=yildizboyu*0.25;
   polar P1=new polar();
   polar P2=new polar();
   polar P3=new polar();
   int x[]=new int[2*n+2];
   int y[]=new int[2*n+2];
   for(int i=0;i<=n;i++)
   {
     double teta1=teta*i+Math.PI/2.0+aci*Math.PI/180.0;
     double teta2=teta/2.0+teta1;
     P1.polarGir(R,teta1);
     P2.polarGir(r,teta2);
     x[2*i]=xi+(int)P1.xi();
     y[2*i]=yi-(int)P1.xj();
     x[2*i+1]=xi+(int)P2.xi();
     y[2*i+1]=yi-(int)P2.xj();
     if(i==n)
     {
     x[2*i]=x[0];
     y[2*i]=y[0];
     }
   }
   g.fillPolygon(x,y,(2*n+2));

}  //fillYildiz metodu sonu}
public static void fillEskenarcokgen(Graphics g,int xi,int yi, int n,int eskenarcokgenboyu,double aci )
{
   // açı derece cinsinden verilmişti
   double teta=2.0*Math.PI/n;
   double R=eskenarcokgenboyu;
   polar P1=new polar();
   int x[]=new int[2*n+2];
   int y[]=new int[2*n+2];
   for(int i=0;i<=n;i++)
   {
     double teta1=teta*i+Math.PI/2.0+aci*Math.PI/180.0;;
     P1.polarGir(R,teta1);
     x[i]=xi+(int)P1.xi();
     y[i]=yi-(int)P1.xj();
     if(i==n)
     {
     x[i]=x[0];
     y[i]=y[0];
     }
   }
   g.fillPolygon(x,y,(n+1));
}  //filleskenarcokgen metodu sonu

}
