Solved

java and fractals

Posted on 1998-12-02
4
203 Views
Last Modified: 2006-11-17
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

Nick
0
Comment
Question by:nl1545
  • 3
4 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 1228675
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


0
 
LVL 16

Accepted Solution

by:
heyhey_ earned 50 total points
ID: 1228676
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 ?
  heyhey


P.S. You've accidently posted two identical questions - you'd better delte the second one.
0
 

Author Comment

by:nl1545
ID: 1228677
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 ??

Nick

0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1228678
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

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now