Solved

java and fractals

Posted on 1998-12-02
4
207 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 104
use lov values 2 63
JavaFX TableView not displaying correctly 3 57
restrict decimal places for double datatype 10 21
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

838 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