
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.JTextField;

/**
 *
 * @author 
 */
public class MandelbrotFrame extends JFrame {

	BufferedImage image;                     //
	int imageWidth = 500;
	int imageHeight = 500;
	JLabel label;
	JButton buttonChange;
	JTextField txt, textCompNum;
	JTextField textXMax, textXMin, textYMax, textYMin;
	JLabel labelXMin, labelXMax, labelYMax, labelYMin;
	int maxIterations = 100;
	double minRe = -2.0;                           // minimum real part of the complex numbers
	double maxRe = 2.0;                             // maximum real part of the complex numbers
	double minIm = -1.6;                            // minimum imaginary part of the complex numbers
	double maxIm = 1.6;                              // maximum imaginary part of the complex numbers

	public MandelbrotFrame() {

		super("Mandelbort Set ");
		this.setPreferredSize(new Dimension(850, 900));
		init();
	}

	public void init() {
		labelXMin = new JLabel("Xmin");
		textXMin = new JTextField("", 5);
		labelXMax = new JLabel("Xmax");
		textXMax = new JTextField("", 5);

		labelYMin = new JLabel("Ymin");
		textYMin = new JTextField("", 5);
		labelYMax = new JLabel("Ymax");
		textYMax = new JTextField("", 5);
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());

		txt = new JTextField(8);
		textCompNum = new JTextField(8);
		txt.setText("The portion of Complex pane is ");
		txt.setBackground(Color.gray);

		//those things
		JPanel component = new JPanel();
		component.setLayout(new FlowLayout());
		component.add(labelXMin);
		component.add(textXMin);
		component.add(labelXMax);
		component.add(textXMax);
		component.add(labelYMin);
		component.add(textYMin);
		component.add(labelYMax);
		component.add(textYMax);
		component.add(txt);
		component.add(textCompNum);
		buttonChange = new JButton("MyButton");
		component.add(buttonChange);
		buttonChange.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				maxIterations = Integer.parseInt(txt.getText());
				repaint();
			}
		});
		image = new BufferedImage(imageWidth, imageHeight, BufferedImage.OPAQUE);

		panel.addMouseListener(new MouseListener() {

			public void mouseClicked(MouseEvent e) {
				textCompNum.setText(e.getX() + " " + e.getY());
			}

			public void mousePressed(MouseEvent e) {
			}

			public void mouseReleased(MouseEvent e) {
			}

			public void mouseEntered(MouseEvent e) {
			}

			public void mouseExited(MouseEvent e) {
			}
		});
		this.add(component, BorderLayout.NORTH);
		this.add(panel);

		this.pack();

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	public void paint(Graphics g) {
		super.paint(g);
		transferCoordinate();
		g.drawImage(image, 30, 70, null);

	}

	/**
	 *First, we need to convert from screen coordinates to the
	 *coordinates on the complex plane they represent.  We want to store
	 *this result in our number "c".  We'll map x to the real part of c,
	 * and y to the imaginary part of c:
	 */
	public void transferCoordinate() {

		int r = 0;
		// calculate the complex values for the pixels in between the maximum and minimum parts of complex number
		double Re_middle = (maxRe - minRe) / (imageWidth - 1); // Make it fill the whole window
		double Im_middle = (maxIm - minIm) / (imageHeight - 1); // Make it fill the whole window
		for (int y = 0; y < imageHeight; ++y) {
			double c_im = maxIm - y * Im_middle; //calculates the equivalent imaginary part  for each pixel .
			for (int x = 0; x < imageWidth; ++x) {
				double c_re = minRe + x * Re_middle;   //calculates the equivalent real part  for each pixel.

				double Z_re = c_re, Z_im = c_im;
				boolean isInside = true;
				for (int n = 0; n < maxIterations; ++n) {
					double Z_re2 = Z_re * Z_re, Z_im2 = Z_im * Z_im;
					if (Z_re2 + Z_im2 > 4) {
						isInside = false;
						r = n;
						break;

					}
					Z_im = 2 * Z_re * Z_im + c_im;
					Z_re = Z_re2 - Z_im2 + c_re;

				}

				/*        int red = (count * 8) % 255;
				int green = (count * 9) % 255;
				int blue=(255-(count * 4))% 255;

				if(isInside){
				image.setRGB(x, y, Color.BLACK.getRGB());

				}else
				image.setRGB(x, y, new Color(red,green,blue).getRGB());

				 */
				if (isInside) {

					image.setRGB(x, y, Color.BLACK.getRGB());


				} else {

					if (r == 10) {
						image.setRGB(x, y, Color.red.getRGB());
					} else if (r >= 40) {
						image.setRGB(x, y, Color.ORANGE.getRGB());
					} else {
						image.setRGB(x, y, Color.BLACK.getRGB());
					}
				}
			}
		}

	}
}
