Solved

java and fractals

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

679 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