import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JPanel;


/* Draw a Julia Fractal using Complex numbers */
public class Fractal {
  
    /**
     * Repeatedly ask the user for the real and imaginary parts of c 
     * (the fractal shape parameter) and draw the fractal.  
     */
    public static void main(String[] args) {
      
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Draw a fractal!");
        createWindow();
       
        // -------- Modify the code below to draw fractals ---------
        // ------- repeatedly until the user wishes to stop -------- 
          
            System.out.println("Enter real part of c: ");
            double x= keyboard.nextDouble();
            System.out.println("Enter imaginary part of c: ");
            double y= keyboard.nextDouble();
            Complex c= new Complex(x, y);

            System.out.println("Drawing fractal!\n");
            drawFractal(c);
            
        // ----------------- Modify the code above -----------------
        
        System.out.println("Bye!");
    }

    /**
     * ={The series  f(0) = z
     *               f(n+1) = f(n)^2 + c  does NOT diverge}, TRUE or FALSE?
     *
     * This method determines if a point z in the complex plane
     * is part of the fractal.
     * 
     * @param z: The point of interest.
     * @param c: Constant parameter determining the fractal shape.
     * @return True if point z is inside fractal.
     */
    private static boolean isInsideFractal(Complex z, Complex c) {

        // ---------- Implement this method ---------
        
        return false;
    }
    
    private static DrawingPanel dp;
    
    /**
     * Draw the Fractal given the shape parameter c.
     */   
    private static void drawFractal(Complex c) {
        dp.clear();
        double tmp = 1.5 / 256;
        
        for (int i=0; i<512; i++)
            for (int j=0; j<512; j++) {
                Complex z = new Complex((j-256)*tmp, -(i-256)*tmp);
                if (isInsideFractal(z, c)) dp.setPixel(j, i, 0xffffff);
            }
        
        dp.repaint();
    }
    
    /**
     * Create a JFrame for the graphics
     */
    private static void createWindow() {
        JFrame frame = new JFrame("Julia Fractal");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        dp = new DrawingPanel(512, 512);
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(dp, BorderLayout.CENTER);
        frame.pack();
        frame.setLocation(100, 100);
        frame.setResizable(false);
        frame.setVisible(true);
    }
}//class Fractal


/* Set up a DrawingPanel */
class DrawingPanel extends JPanel
{
    private BufferedImage bi;

    public DrawingPanel(int w, int h)
    {
        bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        setSize(w, h);
        setPreferredSize(new Dimension(w, h));
    }

    public void setPixel(int x, int y, int rgb)
    {
        bi.setRGB(x, y, rgb);
    }

    public void paintComponent(Graphics g)
    {
        g.drawImage(bi, 0, 0, null);
    }
    
    public void clear()
    {
        for (int i=0; i<bi.getHeight(); i++)
            for (int j=0; j<bi.getWidth(); j++)
                bi.setRGB(j, i, 0);
    }
}
