swoop

JuliaDrawer

Apr 10th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics;
  4. import java.awt.image.BufferedImage;
  5.  
  6.  
  7. public class JuliaDrawer extends FractalDrawer {
  8.  
  9.     double pointX;
  10.     double pointY;
  11.    
  12.     public JuliaDrawer(double[] imageDetails, Dimension size, Graphics g, double x, double y) {
  13.         super(imageDetails, size, g);
  14.         pointX = x;
  15.         pointY = y;
  16.         calculate();
  17.     }
  18.    
  19.     public void setXY(double x, double y){
  20.         pointX = x;
  21.         pointY = y;
  22.     }
  23.  
  24.     @Override
  25.     public void draw() {
  26.                 g.drawImage (bufImg, 0, 0, null);              
  27.     }
  28.    
  29.     public void loadJulia(BufferedImage faveBuff){
  30.         g.drawImage (faveBuff, 0, 0, null);;
  31.     }
  32.  
  33.     public void calculate() {
  34.         int row = 0;
  35.         int col = 0;
  36.         int pixPoint = 0;
  37.         int panelWidthInt = (int) panelWidth;
  38.         int panelHeightInt = (int) panelHeight;
  39.         int pixNum = (panelWidthInt * panelHeightInt);
  40.         int [] pixels = new int[pixNum];
  41.         Complex z = new Complex(0,0);
  42.         Complex c = new Complex(pointX, pointY);
  43.        
  44.         for(row = 0; row < panelHeightInt; row++){
  45.             double y = yMaxVal - (row * yScale);
  46.                
  47.             for(col = 0; col < panelWidthInt; col++){
  48.                 pixPoint = col + (row * panelWidthInt);
  49.                 double x = xMinVal + (col * xScale);
  50.                 z.setReal(x);
  51.                 z.setImag(y);
  52.                    
  53.                 int iter = 0;
  54.                 boolean escaped = false;
  55.                
  56.                 while(iter <= maxIter && !escaped){
  57.                     z.square();
  58.                     z.add(c);
  59.                     iter++;
  60.                     if(z.modulusSquared() < 4){
  61.                         escaped = false;
  62.                     }else{
  63.                         escaped = true;
  64.                     }
  65.                 }
  66.                
  67.                 if(!escaped){
  68.                     pixels[pixPoint] = (0|0|0);
  69.                 }
  70.                 else{
  71.                     double smoothCol = iter + 1 - Math.log((Math.log(z.modulus())))/Math.log(2);   
  72.                     smoothCol = smoothCol / maxIter;
  73.                     int buffPixel =  Color.HSBtoRGB((float) (0.68f- smoothCol) ,0.9f,1.05f);
  74.                     pixels[pixPoint] = buffPixel;
  75.                 }              
  76.             }
  77.         }
  78.        
  79.         bufImg = new BufferedImage (panelWidthInt, panelHeightInt, BufferedImage.TYPE_INT_RGB);
  80.                 bufImg.setRGB (0,0,panelWidthInt, panelHeightInt,pixels,0,panelWidthInt);
  81.  
  82.        
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment