package Mendelbrot;
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
/**
*
* @author
*/
public class Canvas extends JFrame
{
int width=200;
int height=200;
int iterations=4;
Thread thread=new Thread();
public static void main(String args[])
{
System.out.println("Running Canvas.java");
Canvas canvas = new Canvas();
}
public Canvas()
{
setSize(width, height);
setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(Color.yellow);
g.fillRect(0,0, width, height);
int[][] grid=render(width, height);
for(int i=0;i<width;i++)
{
for(int j=0;j<height;j++)
{
if(grid[i][j]==0)
{
g.setColor(Color.WHITE);
}
if(grid[i][j]==1)
{
g.setColor(Color.BLACK);
}
g.fillRect(i,j,1,1); //fills a single pixel
}
}
}
/**
*
* @param c complex number
* @param n how many iterations are used to find the answer
* @return true if c is part of the mendelbrot set
*/
public boolean isMend(Z c,int n)
{
return isMend(c,n,c);
}
public boolean isMend(Z zn, int n, Z c)
{
// System.out.println(n);
if(n==0)
{
if(zn.getSize()<=2)
return true;
else
return false;
}
Z zn1=Z.sum(Z.multiply(zn,zn),c);
return isMend(zn1,n-1,c);
}
/**
*
* @param w width
* @param h height
* @return an image to be painted
*/
public int[][] render(int w, int h)
{
System.out.println("Render "+w+","+h);
int[][] grid=new int[w][h];
//Initialization of the grid
for(int i=0;i<w;i++)
{
for(int j=0;j<h;j++)
{
grid[i][j]=0;
}
}
//Give values
for(int i=0;i<w;i++)
{
for(int j=0;j<h;j++)
{
// -1<x<1 -1<y<1
int x=i/100-1;
int y=j/100-1;
Z c=new Z(x,y);
if(isMend(c,iterations))
{
grid[i][j]=1;
}
System.out.println("Finished rendering pixel ["+i+","+j+"]");
}
}
return grid;
}
// public void run()
// {
// while(true)
// {
// repaint();
// System.out.println("Repainting");
//
//
// try
// {
// thread.sleep(2000);
// }
// catch (InterruptedException ex)
// {
// Logger.getLogger(Canvas.class.getName()).log(Level.SEVERE, null, ex);
// }
// }
// }
}