import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//import java.awt.image.BufferedImage;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import com.sun.opengl.util.Animator;
//import com.sun.opengl.util.FPSAnimator;
public class Mendelbrot extends GLCanvas implements GLEventListener, KeyListener
{
//static BufferedImage Image =null;
static Animator animator=null;
double MinResolution=-2.0;
double MaxResolution=1.0;
double MinImage=-1.2;
static int imageheight=640;
static int imagewidth=640;
double MaxImage=MinImage+(MaxResolution-MinResolution)*imageheight/imagewidth;
double resolutionfactor=(MaxResolution-MinResolution)/(imagewidth-1);
double Imagefactor=(MaxImage-MinImage)/(imageheight-1);
int MaxIteration=100;
boolean checkflag;
double complexsquare;
double compleximagesquare;
public void drawMendelbrot(GL gl)
{
gl.glBegin(GL.GL_POINT);
for(int x=0;x<imageheight;++x)
{
double cimage=MaxImage-(x*Imagefactor);
for(int y=0;y<imagewidth;++y)
{
double cres=MinResolution+(y*resolutionfactor);
double complexres=cres;
double compleximage=cimage;
checkflag=true;
for(int n=0;n<MaxIteration;n++)
{
complexsquare=complexres*complexres;
compleximagesquare=compleximage*compleximage;
if((complexsquare+compleximagesquare)>4)
{
checkflag=false;
break;
}
compleximage=2*complexres*compleximage+cimage;
complexres=complexsquare-compleximagesquare+cres;
}
if(checkflag)
{
gl.glColor3f(0.0f, 0.0f, 0.0f);
gl.glVertex2f(x,y);
}
}
}
gl.glEnd();
}
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
Frame frame=new Frame("Kalyan banik");
GLCanvas canvas=new GLCanvas();
canvas.addGLEventListener(new Mendelbrot());
frame.setSize(imageheight, imagewidth);
animator=new Animator(canvas);
frame.addWindowListener(new WindowAdapter()
{
public void WindowClose(WindowEvent e)
{
animator.stop();
System.exit(0);
}
});
frame.setVisible(true);
animator.start();
canvas.requestFocus();
}
@Override
public void display(GLAutoDrawable glmendel)
{
// TODO Auto-generated method stub
final GL gl=glmendel.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT |GL.GL_DEPTH_BUFFER_BIT);
gl.glPushMatrix();
drawMendelbrot(gl);
gl.glPopMatrix();
gl.glFlush();
}
@Override
public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2)
{
// TODO Auto-generated method stub
}
@Override
public void init(GLAutoDrawable glDrawable)
{
// TODO Auto-generated method stub
final GL gl=glDrawable.getGL();
gl.glShadeModel(GL.GL_SMOOTH);
gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
gl.glClearDepth(1.0f);
gl.glEnable(GL.GL_DEPTH_TEST);
glDrawable.addKeyListener(this);
}
@Override
public void reshape(GLAutoDrawable glDrawable, int x, int y, int width,
int height)
{
// TODO Auto-generated method stub
final GL gl= glDrawable.getGL();
final GLU glue=new GLU();
if(height<=0)
{
height=1;
}
final float h=(float)width/(float)height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glue.gluPerspective(45.0f, h, 1.0, 20.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
glue.gluLookAt(0, 0, 40, 0, 0, 0, 0, 1, 0);
}
@Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
// TODO Auto-generated method stub
{
animator.stop();
System.exit(0);
}
}
@Override
public void keyReleased(KeyEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0)
{
// TODO Auto-generated method stub
}
}