Solved

java and fractals

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 24
Java asynchronous logging 4 50
xampp tool 12 30
java jdbc batch example 8 22
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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.

862 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