package opartgui;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.image.BufferedImage;

public class Ellipse{
      int x, y;
      int w = 600;
      int h = 600;
      int turnaround = 16;
      int circleQuantity = 28;
      int circleSize = 40;
      Rectangle area;
      BufferedImage displayImage;
      Graphics2D dispGc;
      boolean invert = false;
      int changes[] = {6,4,2,1};
      BufferedImage[] animate = new BufferedImage [21];
      Color colorOne = new Color(254,254,254);
      Color colorTwo = new Color(1,1,1);

      public Ellipse(){

      }

      public BufferedImage doDraw(int changeOneField,
                       int changeTwoField,
                       int changeThreeField,
                       int changeFourField,
                       int circleSizeField,
                       int circleQuantityField,
                       int turnaroundField){
       	changes[0] = changeOneField;
       	changes[1] = changeTwoField;
       	changes[2] = changeThreeField;
        changes[3] = changeFourField;
        circleSize = circleSizeField;
       	circleQuantity = (circleQuantityField*2);
       	turnaround = turnaroundField;
        displayImage = (imageDraw());
        return displayImage;
      }

      protected void createDisplayImage(){
        displayImage = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
        setOffScrGc();
      }

      protected void setOffScrGc(){
        if (displayImage != null){
             dispGc = displayImage.createGraphics();
             dispGc.fillRect(0,0,600,600);
             dispGc.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        }
      }

//       public void turnaroundAnimation(boolean anim){
//              boolean flag = true;
//              turnaround = 10;
//               int i = -1;
//               if(anim==true){
//                  System.out.println("Animation activated");
//                  while(flag == true){
//                          while (turnaround<20){
//                                System.out.println("Animating forward");
//                                turnaround++;
//                                System.out.println("Turnaround = "+turnaround);
//                                i++;
//                                System.out.println("ArrayLocation = "+i);
//                                animate[i] = imageDraw();
// 
//                          }
//                          while (turnaround>10){
//                                System.out.println("Animating backward");
//                                turnaround--;
//                                System.out.println("Turnaround = "+turnaround);
//                                i++;
//                                System.out.println("ArrayLocation = "+i);
//                                animate[i] = imageDraw();
// 
//                          }
//                          flag = false;
//                  }
// 
//               }
//       }
// 

       /* public void turnaroundAnimation(boolean anim){
             boolean flag = true;
             turnaround = 10;
             int i = -1;
             if(anim==true){
                System.out.println("Animation activated");
                while(flag == true){
                        while (turnaround<20){
                              System.out.println("Animating forward");
                              turnaround++;
                              System.out.println("Turnaround = "+turnaround);
                              i++;
                              System.out.println("ArrayLocation = "+i);
                              animate[i] = imageDraw();

                        }
                        while (turnaround>10){
                              System.out.println("Animating backward");
                              turnaround--;
                              System.out.println("Turnaround = "+turnaround);
                              i++;
                              System.out.println("ArrayLocation = "+i);
                              animate[i] = imageDraw();

                        }
                        flag = false;
                }

             }
             opart1.doAnimPanelShow();
             opart1.animPanel.loadAnimate(animate, anim);
      } */

      public BufferedImage imageDraw(){
             createDisplayImage();
             int i,j;
             int xcurrent=20;
             boolean pt = false;
             for(j=1;j<(circleQuantity/2);j++){
                     int circletmp = circleSize;
                     for(i=1;i<circleQuantity;i++){
	                        if(i>=(turnaround-11) && i<(turnaround-7)){circleSize = circleSize-changes[0];}
                                if(i>=(turnaround-7) && i<(turnaround-5)){circleSize = circleSize-changes[1];}
                                if(i>=(turnaround-5) && i<(turnaround-3)){circleSize = circleSize-changes[2];}
                                if(i>=(turnaround-3) && i<(turnaround-1)){circleSize = circleSize-changes[3];}
                                if(i>=turnaround && i<(turnaround+2)){circleSize = circleSize+changes[3];}
                                if(i>=(turnaround+2) && i<(turnaround+4)){circleSize = circleSize+changes[2];}
                                if(i>=(turnaround+4) && i<(turnaround+6)){circleSize = circleSize+changes[1];}
                                if(i>=(turnaround+6) && i<(turnaround+10)){circleSize = circleSize+changes[0];}
                                if(circleSize<0)circleSize=0;
                                dispGc.setColor(colorOne);
                                if (pt==false){
                                   dispGc.setColor(colorTwo);
                                }
		                dispGc.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                                dispGc.fill(new Ellipse2D.Double(xcurrent,20+(circletmp*j),circleSize,circletmp));
                                if (pt==false){
                                              	pt = true;
                                }
                                else{
                                    	pt=false;
                                }
                                xcurrent = xcurrent + circleSize;
                     }
                     circleSize = circletmp;
                     xcurrent = 20;
             }
             return displayImage;
        }

}
