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 Arrest{
      int wavelength = 150;
      int ytransdown = 25;
      int ytransup = 10;
      int xtransdown = 20;
      int xtrans;
      int ytrans;
      int xtransup = 25;
      int waverep = 15;
      int wavedepth = 30;
      int wavewidth = 65;
      int turnaround = 10;
      int linequantity = 25;
      int x, y;
      int w = 600;
      int h = 600;
      Rectangle area;
      BufferedImage displayImage;
      Graphics2D dispGc;


      public Arrest(){

      }

      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,w,h);
                dispGc.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
             }
      }

      public BufferedImage doDraw(int wavelengthField,
                       int ytransupField,
                       int xtransupField,
                       int ytransdownField,
                       int xtransdownField,
                       int waverepField,
                       int wavedepthField,
                       int lineQuantityField,
                       int wavewidthField){
       	wavelength = wavelengthField;
       	ytransup = ytransupField;
       	xtransup = xtransupField;
        ytransdown = ytransdownField;
       	xtransdown = xtransdownField;
       	waverep = waverepField;
       	wavedepth = wavedepthField;
       	linequantity = lineQuantityField;
        wavewidth = wavewidthField;
        displayImage = (imageDraw());
        return displayImage;
      }

      public BufferedImage imageDraw(){
             createDisplayImage();
             int i,j,k,offset1, offset2;
             for(j=0;j<waverep;j++){
                     int blkint = 250;
                     int xt = 0;
                     int yt = 0;
                     for(i=0;i<linequantity;i++){
	                        CubicCurve2D.Double arrest = new CubicCurve2D.Double();
                                Point2D.Double s, e, a, b;
                                s = new Point2D.Double();
		                a = new Point2D.Double();
		                b = new Point2D.Double();
		                e = new Point2D.Double();
                                offset1 = wavelength*(j-3);
                                offset2 = wavelength*(j-2);
                                if (i<turnaround){
                                   xt = xtransdown + xt;
                                   yt = ytransdown * i;
                                   s.setLocation(xt, offset1+yt);
		                   e.setLocation(xt, offset2+yt);
		                }
                                else{
                                     xt = xtransup + xt;
                                     yt = ytransup *i;
                                     s.setLocation(xt, offset1-yt);
		                     e.setLocation(xt, offset2-yt);
		                }
                                a.setLocation((int)(s.x)+wavedepth, (int)(s.y)+wavewidth);
		                b.setLocation((int)(e.x)-wavedepth, (int)(e.y)-wavewidth);
		                arrest.setCurve(s, a, b, e);
                                if(i<turnaround){
                                   blkint = blkint - 24;
                                   if (blkint <0) blkint = 0;
                                }
                                if(i>(turnaround+5)){
                                   blkint = blkint + 24;
                                   if (blkint >255) blkint = 255;
                                }
                                dispGc.setColor(new Color(blkint,blkint,blkint));
                                dispGc.setStroke(new BasicStroke(10.0f));
		                dispGc.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		                dispGc.draw(arrest);
                     }
             }
             return displayImage;
      }
}
