• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

java and fractals

Hi guys,

I am not so an experienced java programmer (only the theoretical stuff, courses and so on). I want to make a java program calculating fractals (mandelbrot). Later I would like to extend it with distributed technology (CORBA, JAVA IDL, RMI) or to make it a JavaBean.
But first the "simple" things. I plan to extend a Canvas Component for the drawing and I know that normally the paint-method is overridden. In this case it would mean that the calculation starts again each time the window has to be repainted. Second as the calculation is probably time-consuming the application won't be able to respond to other user actions. I programmed something similar with C++ and I drawed to a memory context. My "paint" method just displayed the memory context. Is this possible in Java, too ??

All comments appreciated

  • 3
1 Solution
there is standard technique called "double buffering" - you draw everything in offscreen buffer
and than render the buffer to screen. you can use a small variation - render your fractal in the offscreen buffer and in you paint method just  paint this buffer ...

// you should be initialized  & know you size
// maybe the best solution is this code to be execute when the paint method is first called

    Image offscreen;
    Dimension offscreensize;
    Graphics offgraphics;

      Dimension d = getSize();
      if ((offscreen == null) || (d.width != offscreensize.width) || (d.height != offscreensize.height)) {
          offscreen = createImage(d.width, d.height);
          offscreensize = d;
          offgraphics = offscreen.getGraphics();

// you render your fractal in offgrafics
public void paint(Graphics g) {
      g.drawImage(offscreen, 0, 0, null);

this solution gives you only the Graphics object methods

or you can render to an offscreen buffer of colors (integers) then create an Image object
and then just paint it to Screen

   pixels = new int[w * h];
    int index = 0;
    for (int y = 0; y < h; y++) {
        int red = (y * 255) / (h - 1);
        for (int x = 0; x < w; x++) {
    int blue = (x * 255) / (w - 1);
    pixels[index++] = (255 << 24) | (red << 16) | blue;
  image = createImage(new MemoryImageSource(w, h, pixels2, 0, w));

   public void paint(Graphics g)
       g.drawImage(image, 5, 5 , null);

every pixel color is int - in RGBA format
int pixel;
int alpha = (pixel >> 24) & 0xff;
int red   = (pixel >> 16) & 0xff;
int green = (pixel >>  8) & 0xff;
int blue  = (pixel      ) & 0xff;
System.out.println("[" + red + ", " + green + ", " + blue + ", " + alpha + "]");

i think that this is better technique for fractal rendering

Do you need more help ?

P.S. You've accidently posted two identical questions - you'd better delte the second one.
nl1545Author Commented:
That sounds very good, especially I can solve the other problem (responding to user requests) by making a second thread for the calculation (rendering the image offscreen) and the paint method just displaying the image. When tackling distributed computing this second thread will be the master thread for even more threads dedicated to the servers. The master thread collects the returned results from the working threads and paints them to the offscreen image (or buffer). Is there any mistake in my intentions ??


sounds Ok to me.
will you send me e-mail when you complete the project (heyhey@nettaxi.com)
of course if you encounter some problems you can e-mail me too


Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now