// Java (Applet) Fractal Explorer
// (c) 1997 by Nils Pipenbrinck
// fixed in 2005 by doj after decompilation of .class files with jad

import java.awt.Color;
import java.awt.Graphics;
import java.util.Calendar;
import java.util.Date;

class MandelRenderer
{
    private int height;
    private int width;
    private Graphics Painter;
    private double delta_x;
    private double delta_y;
    private double left;
    private double top;
    private double other_r;
    private double other_i;

    public  long milliseconds;
    public  int iteration_depth;

    public MandelRenderer(Graphics g, int i, int j, MandelState mandelstate)
    {
        iteration_depth = 255;
        Painter = g;
        width = i;
        height = j;
        left = mandelstate.left;
        top = mandelstate.top;
        delta_x = (4D * (mandelstate.right - mandelstate.left)) / (double)(float)width;
        delta_y = (4D * (mandelstate.bottom - mandelstate.top)) / (double)(float)height;
        other_r = mandelstate.other_r;
        other_i = mandelstate.other_i;
	milliseconds = 0;

        if(Painter != null)
        {
            if(mandelstate.isMandel)
		{
		    mandel();
		}
	    else
		{
		    julia();
		}
        }
    }

    private void mandel()
    {
        milliseconds = Calendar.getInstance().getTime().getTime();
        for(int j = 0; j < 16; j++)
        {
            double d5 = top + (delta_y * (double)taby[j]) / 4D;
            for(int k = taby[j]; k < height; k += 4)
            {
                double d6 = left + ((double)tabx[j] * delta_x) / 4D;
                for(int l = tabx[j]; l < width; l += 4)
                {
                    int i = 0;
                    double d1 = other_i;
                    double d = other_r;
                    double d4 = d6;
                    double d3 = d5;
                    do
                    {
                        double d2 = (d * d - d1 * d1) + d4;
                        d1 = 2D * (d * d1) + d3;
                        d = d2;
                    } while(++i != iteration_depth && d * d + d1 * d1 < 4D);
                    if(i < iteration_depth)
                    {
                        float f = (float)(1.0D - (double)((float)i / (float)iteration_depth));
                        float f1 = (float)(Math.atan2(d1, d) / 6.2831853071800001D);
                        Painter.setColor(Color.getHSBColor(f1, 1.0F, f));
                    } else
                    {
                        Painter.setColor(Color.black);
                    }
                    Painter.fillRect(l, k, widx[j], widy[j]);
                    d6 += delta_x;
                }

                d5 += delta_y;
            }

        }

        milliseconds = Calendar.getInstance().getTime().getTime() - milliseconds;
    }

    private void julia()
    {
        milliseconds = Calendar.getInstance().getTime().getTime();
        for(int j = 0; j < 16; j++)
        {
            double d5 = top + (delta_y * (double)taby[j]) / 4D;
            for(int k = taby[j]; k < height; k += 4)
            {
                double d6 = left + ((double)tabx[j] * delta_x) / 4D;
                for(int l = tabx[j]; l < width; l += 4)
                {
                    int i = 0;
                    double d1 = d6;
                    double d = d5;
                    double d4 = other_r;
                    double d3 = other_i;
                    do
                    {
                        double d2 = (d * d - d1 * d1) + d4;
                        d1 = 2D * (d * d1) + d3;
                        d = d2;
                    } while(++i != iteration_depth && d * d + d1 * d1 < 4D);
                    if(i < iteration_depth)
                    {
                        float f = (float)(1.0D - (double)((float)i / (float)iteration_depth));
                        float f1 = (float)(Math.atan2(d1, d) / 6.2831853071800001D);
                        Painter.setColor(Color.getHSBColor(f1, 1.0F, f));
                    } else
                    {
                        Painter.setColor(Color.black);
                    }
                    Painter.fillRect(l, k, widx[j], widy[j]);
                    d6 += delta_x;
                }

                d5 += delta_y;
            }

        }

        milliseconds = Calendar.getInstance().getTime().getTime() - milliseconds;
    }

    private static final int tabx[] = {
        0, 2, 2, 0, 1, 3, 1, 3,
	0, 2, 0, 2, 1, 3, 1, 3
    };
    private static final int taby[] = {
        0, 2, 0, 2, 0, 0, 2, 2,
	1, 1, 3, 3, 1, 1, 3, 3
    };
    private static final int widx[] = {
        4, 2, 2, 2, 1, 1, 1, 1,
	1, 1, 1, 1, 1, 1, 1, 1
    };
    private static final int widy[] = {
        4, 2, 2, 2, 2, 2, 2, 2,
	1, 1, 1, 1, 1, 1, 1, 1
    };

}

