package opartgui;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.beans.*;
import javax.swing.*;
import java.awt.geom.*;
import com.sun.image.codec.jpeg.*;

public class Cataract{

protected BufferedImage displayImage;
protected Graphics2D dispGc;
int wavelength = 150;
int ytrans = 6;
int xtrans = 15;
int waverep = 17;
int wavedepth = 10;
int wavewidth = 65;
boolean invert = false;
boolean firstDraw = true;
int w = 600;
int h = 600;
int x, y, redint, grnint, bluint,xt,yt;
Rectangle area;
Color colorOne = new Color(0,158,0);


public Cataract(){
    
}

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

protected void setOffScrGc(){
        if (displayImage != null){
             dispGc = displayImage.createGraphics();
             dispGc.setColor(Color.white);
             dispGc.fillRect(0,0,w,h);
             dispGc.setColor(Color.black);
             dispGc.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        }
}


public BufferedImage doDraw(int wavelengthField,
                       int ytransField,
                       int xtransField,
                       int waverepField,
                       int wavedepthField,
                       int wavewidthField){
       	wavelength = wavelengthField;
       	ytrans = ytransField;
       	xtrans = xtransField;
       	waverep = waverepField;
       	wavedepth = wavedepthField;
       	wavewidth = wavewidthField;
        displayImage = (imageDraw());
        return displayImage;
}


public BufferedImage imageDraw(){
        createDisplayImage();
        int i,j,k,offset1, offset2;
        for(j=0;j<waverep;j++){
                for(i=1;i<100;i++){
	                CubicCurve2D.Double cataract = 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);
                        offset2 = wavelength*(j+1);
                        xt = xtrans*i;
                        yt = (h-(ytrans*i));
                        s.setLocation(offset1-xt, yt);
		        e.setLocation(offset2-xt, yt);
		        a.setLocation((int)(s.x)+wavewidth, (int)(s.y)-wavedepth);
		        b.setLocation((int)(e.x)-wavewidth, (int)(e.y)+wavedepth);
		        cataract.setCurve(s, a, b, e);
        	        dispGc.setColor(colorOne);
		        dispGc.setStroke(new BasicStroke(3.0f));
		        dispGc.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		        dispGc.draw(cataract);
                }
        }
        for(j=0;j<waverep;j++){
                for(i=1;i<100;i++){
		        CubicCurve2D.Double cataract = 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);
                        offset2 = wavelength*(j+1);
                        xt = xtrans*i;
                        yt = (h-(ytrans*i));
                        s.setLocation(offset1-xt,yt-.75);
		        e.setLocation(offset2-xt,yt-.75);
		        a.setLocation((int)(s.x)+wavewidth, (int)(s.y)-wavedepth);
		        b.setLocation((int)(e.x)-wavewidth, (int)(e.y)+wavedepth);
		        cataract.setCurve(s, a, b, e);
        	        if (i<25){redint = (9*i);}
                        if (i>34){redint = (255-(9*(i-35)));}
                        if(i>24 && i<35){redint = 255;}
                        if (i>60){redint = 0;}
                        grnint = 158 - redint;
                        if (grnint< 1){grnint = 0;}
                        bluint = 0 - redint;
                        if (bluint< 1){bluint = 0;}
                        dispGc.setColor(new Color(redint, grnint, bluint));
		        dispGc.setStroke(new BasicStroke(1.5f));
		        dispGc.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		        dispGc.draw(cataract);
                }
    	}
        return displayImage;
}
      

}
