package fractal;

import de.erichseifert.gral.data.DataTable;
import de.erichseifert.gral.plots.XYPlot;
import de.erichseifert.gral.plots.lines.DefaultLineRenderer2D;
import de.erichseifert.gral.plots.lines.LineRenderer;
import de.erichseifert.gral.plots.points.PointRenderer;
import de.erichseifert.gral.ui.InteractivePanel;

import org.apache.commons.math3.complex.Complex;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;

/**
 * Main frame for fractal task
 *
 * @author Danil Kolikov
 */
public class MainFrame extends JFrame {
    private static final Color red = new Color(1.0f, 0.0f, 0.0f);
    private static final Color green = new Color(0.0f, 1.0f, 0.0f);
    private static final Color blue = new Color(0.0f, 0.0f, 1.0f);

    private DataTable firstRootData = new DataTable(Double.class, Double.class);
    private DataTable secondRootData = new DataTable(Double.class, Double.class);
    private DataTable thirdRootData = new DataTable(Double.class, Double.class);
    private XYPlot plot = new XYPlot();
    private InteractivePanel interactivePanel;

    LineRenderer lineRenderer = new DefaultLineRenderer2D();

    private void drawCircle() {
        DataTable circleData = new DataTable(Double.class, Double.class);
        for (int i = 0; i <= 720; i++) {
            double x = Math.cos(i * Math.PI / 360);
            double y = Math.sin(i * Math.PI / 360);
            circleData.add(x, y);
        }
        plot.add(circleData);
        plot.setLineRenderers(circleData, lineRenderer);
        for (PointRenderer pR : plot.getPointRenderers(circleData)) {
            Shape circle = new Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0);
            pR.setShape(circle);
        }
    }

    private void drawPoint(Complex point, int pointColor) {
        switch (pointColor) {
            case 0:
                firstRootData.add(point.getReal(), point.getImaginary());
                for (PointRenderer pR : plot.getPointRenderers(firstRootData)) {
                    Shape circle = new Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0);
                    pR.setShape(circle);
                    pR.setColor(red);
                }
                break;
            case 1:
                secondRootData.add(point.getReal(), point.getImaginary());
                for (PointRenderer pR : plot.getPointRenderers(secondRootData)) {
                    Shape circle = new Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0);
                    pR.setShape(circle);
                    pR.setColor(green);
                }
                break;
            case 2:
                thirdRootData.add(point.getReal(), point.getImaginary());
                for (PointRenderer pR : plot.getPointRenderers(thirdRootData)) {
                    Shape circle = new Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0);
                    pR.setShape(circle);
                    pR.setColor(blue);
                }
                break;
        }
    }

    public MainFrame() throws HeadlessException {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new Dimension(600, 600));

        drawCircle();
        plot.add(firstRootData);
        plot.add(secondRootData);
        plot.add(thirdRootData);

        interactivePanel = new InteractivePanel(plot);
        getContentPane().add(interactivePanel);
    }

    public static void main(String[] args) {
        MainFrame mainFrame = new MainFrame();
        mainFrame.setTitle("Fractals");
        mainFrame.drawPoint(new Complex(0.0f, 0.0f), 0);
        mainFrame.drawPoint(new Complex(0.5f, 0.5f), 1);
        mainFrame.drawPoint(new Complex(1.5f, 1.5f), 2);
        mainFrame.setVisible(true);
    }
}

@@ -7,24 +7,83 @@
 e fractal;%0A%0A
+import com.sun.javaws.exceptions.InvalidArgumentException;%0A
 import de.er
@@ -114,16 +114,16 @@
 aTable;%0A
-
 import d
@@ -501,16 +501,59 @@
 lipse2D;
+%0Aimport java.util.*;%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -1853,40 +1853,93 @@
 int(
-Complex point, int pointColor) %7B
+Solver.ColoredPoint coloredPoint) %7B%0A        Complex point = coloredPoint.getPoint();%0A
 %0A   
@@ -1955,18 +1955,31 @@
 ch (
-poin
+coloredPoint.ge
 tColor
+()
 ) %7B%0A
@@ -3660,35 +3660,119 @@
         
-mainFrame.drawPoint
+Solver solver = new Solver();%0A        try %7B%0A            List%3CSolver.ColoredPoint%3E answer = solver.solve
 (new Com
@@ -3780,90 +3780,117 @@
 lex(
-0
+-2.0f, -2
 .0f
+)
 , 
-0
+new Complex(2
 .0f
-)
 , 
-0);%0A        mainFrame.drawPoint(new Complex(0.5f, 0.5f), 1);%0A
+2.0f));%0A            for (Solver.ColoredPoint point : answer) %7B%0A     
         
 main
@@ -3877,32 +3877,35 @@
  %7B%0A             
+   
 mainFrame.drawPo
@@ -3912,36 +3912,155 @@
 int(
-new Complex(1.5f, 1.5f), 2);
+point);%0A            %7D%0A        %7D catch (InvalidArgumentException e) %7B%0A            System.out.println(%22%D0%9E%D1%87%D0%B5%D0%BD%D1%8C %D0%B6%D0%B0%D0%BB%D1%8C %22 + e.getRealMessage());%0A        %7D%0A
 %0A   

initial for z^3=1