# Making really big pictures

**843853**Dec 21, 2009 9:33 PM

Hello, I have written a program that saves the Dragon Fractal to a file. The fractal is iteration based but I can only go up to the 26th iteration before I run out of heap space when I try to create the BufferedImage. How can I either use another image object or better manage my memory? I have extended my default heap space to 6 gigs when I run it from the terminal. My specs for my PC are: i7 920 @2.66*4, 6 Gb DDR3 2000, 1TB 7200 RPM HDD, Asus P6T Deluxe V2 Mobo, and a GTX 275. Here is my code, all in one class:

```
package fractal;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import javax.imageio.ImageIO;
/
@author Chris Philip
*/*
*public class DragonFractal {*
*private static boolean[] iteration;*
*private static int lineLength, xMin, xMax, yMin, yMax, width, height;*
*public static void main(String args[]) {*
*int iterations=Integer.parseInt(args[0]);*
*calcTurns(iterations);*
*calcSize(iterations);*
*draw(iterations);*
*}*
*private static void calcTurns(int iterations) {*
*boolean old[];*
*iteration = new boolean[] {true};*
*for(int i=2;i<=iterations;i++) {*
*old=iteration;*
*iteration=new boolean[old.length*2+1];*
*iteration[old.length]=true;*
*System.arraycopy(old, 0, iteration, 0, old.length);*
*System.arraycopy(old, 0, iteration, old.length+1, old.length/2);*
*iteration[old.length*3/2+1]=false;*
*System.arraycopy(old,old.length/2+1,iteration,(iteration.length-3)*3/4+3,old.length/2);*
*}*
*}*
*private static void calcSize(int iterations) {*
*double angle=Math.PI/4*iterations;*
*lineLength=2+(iterations%2);*
*int xStart=0,*
*xEnd=xStart+(int)(lineLength*Math.cos(angle)),*
*yStart=0,*
*yEnd=yStart+(int)(lineLength*Math.sin(angle));*
*xMin=xStart>xEnd?xEnd:xStart;*
*xMax=xStart>xEnd?xStart:xEnd;*
*yMin=yStart>yEnd?yEnd:yStart;*
*yMax=yStart>yEnd?yStart:yEnd;*
*for(int i=0; i<iteration.length; i++) {*
*angle+=Math.PI/2*(iteration
```*?-1:1);*

xStart=xEnd;

yStart=yEnd;

xEnd=xStart+(int)(lineLength*Math.cos(angle));

yEnd=yStart+(int)(lineLength*Math.sin(angle));

xMin=xMin>xEnd?xEnd:xMin;

xMax=xMax<xEnd?xEnd:xMax;

yMin=yMin>yEnd?yEnd:yMin;

yMax=yMax<yEnd?yEnd:yMax;

}

width=xMax-xMin+1;

height=yMax-yMin+1;

+}+

+private static void draw(int iterations) {+

*BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);*

*Graphics g=image.getGraphics();*

*BigDecimal counter=new BigDecimal("1"),*

*max=(new BigDecimal("2")).pow(iterations);*

*double angle=Math.PI/4*iterations;*

*int xStart=-xMin,*

*xEnd=xStart*(int)(lineLength*Math.cos(angle)),

yStart=-yMin+1,

yEnd=yStart+(int)(lineLength*Math.sin(angle));

for(boolean turn:iteration) {

g.setColor(new Color(Color.HSBtoRGB((counter.divide(max, 3, RoundingMode.UP)).floatValue(), 1, 1)));

counter = counter.add(BigDecimal.ONE);

g.drawLine(xStart,height-yStart,xEnd,height-yEnd);

angle+=Math.PI/2*(turn?-1:1);

xStart=xEnd;

yStart=yEnd;

xEnd=xStart+(int)(lineLength*Math.cos(angle));

yEnd=yStart+(int)(lineLength*Math.sin(angle));

}

g.setColor(new Color(Color.HSBtoRGB((counter.divide(max, RoundingMode.UP)).floatValue(), 1, 1)));

g.drawLine(xStart,height-yStart,xEnd,height-yEnd);

try {

BufferedImage bi = image;

File outputfile = new File("C:\\Users\\Chris\\Desktop\\NewDragonCurve-"+iterations+".png");

ImageIO.write(bi, "png", outputfile);

} catch (IOException e) {

System.out.println("YOU FAIL");

}

}

}
` `

- 55 Views
- Tags: none (add)