Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8142801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:55:09+00:00 2026-06-06T12:55:09+00:00

EDIT: solved, look below for my solution. first of all, this is my very

  • 0

EDIT: solved, look below for my solution.

first of all, this is my very first question here, so if I make any mistakes, please tell me.

I am trying to write a Mandelbrot Fractal Program in Java, for training purposes. The ideal for all the functionality I want to have is the Fractalizer (http://www.fractalizer.de/en/), but for now, I will be happy with a program that draws the Mandelbrot Set on the screen (instead of, for example, writing it to an image file). Of course, I want the program to be fast, so I thought that I could split the calculation into multiple threads to utilize my multi-core processor; for example, on a quad-core system, the image would be divided into 2×2=4 images, each to be calculated by a separate thread. All these threads get a Graphics object passed on which they draw the pixels as they are calculated.

My first attempt to get this working was to have the threads draw on a BufferedImage.getGraphics() and to have the paint() method constantly call repaint() as long as the image isn’t finished:

g.drawImage(tempImg, 0, 0, null);
if (waiterThread.isAlive())
{
    try
    {
        Thread.sleep(10);
    } catch (InterruptedException e)
    {
        // do nothing
    }
    repaint(10);
}

(waiterThread joins all calculating threads one after another, so as long as the waiterThread is alive, at least one calculating thread is not yet finished.)

This works, but causes ugly flickering on the canvas because of the frequent repainting.

Then, by means of a small test program, I found out that Graphics.draw*anything* draws on the screen instantly, before the paint method returns, so my current approach is the following:

  • One Panel with a GridLayout that contains 2×2 (on a <4-core system, 1×1) MandelbrotCanvas objects
  • Each MandelbrotCanvas object will, on the first paint() call, initialize a calculating Thread, pass its own Graphics object to it (actually, I’m using a custom GroupGraphics class that passes one Graphics call to several graphics, to “backup” the image into a BufferedImage.getGraphics(), but that’s not important), and start the calculating thread.
  • The panel will in its paint() method fetch the calculating threads from each of the MandelbrotCanvases and join() them.

Unfortunately, this creates only a black screen. Only when calculation is finished, the image is displayed.

What is the right way to have several threads paint onto one component?

EDIT:

What I didn’t know: Only the Event Dispatch Thread is allowed to paint on AWT components (roughly spoken), which means that the last approach above can’t possibly work – apparently, it’s supposed to throw an exception, but I didn’t get one. My solution is to use the first approach – draw the image onto a BufferedImage and draw that onto the Canvas – with the only modification that I overload the update() method to call the paint() method without clearing the painting area:

public void update(Graphics g)
{
    paint(g);
}

So I guess my answer to the general question (“How do I let multiple Threads paint onto an AWT component?”) would be: You can’t, it’s not allowed. Let the Threads draw onto a BufferedImage.getGraphics(), and draw that image repeatedly. Overload the update() method like above to avoid flickering. (It looks really great now.) Another tip that I can’t use in my case, but is still good, is that there is a variant of repaint() that takes rectangle arguments to specify the area that has to be redrawn, and a variant that takes a time argument (in milliseconds), so the repaint doesn’t have to happen immediately.

EDIT2: This link provides very helpful information: http://java.sun.com/products/jfc/tsc/articles/painting/index.html

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:55:10+00:00Added an answer on June 6, 2026 at 12:55 pm

    Only the GUI thread can paint directly on your component.

    So you must call the repaint method.

    When you have background computation, to force a fast drawing, you should use the version taking a time as parameter.

    Some details from here :

    NOTE: If multiple calls to repaint() occur on a component before the
    initial repaint request is processed, the multiple requests may be
    collapsed into a single call to update(). The algorithm for
    determining when multiple requests should be collapsed is
    implementation-dependent. If multiple requests are collapsed, the
    resulting update rectangle will be equal to the union of the
    rectangles contained in the collapsed requests.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: This problem has been solved. See below. Hey all. I'm building an iPhone
edit #2: Question solved halfways. Look below As a follow-up question, does anyone know
[EDIT: Problem solved. Please see my answer below.] In my app I call the
Answer solved in edit below I had this piece of code Dictionary<Merchant, int> remaingCards
edit This question is solved! Having something weird. I'm using html { font-size: 100%
EDIT: SOLVED Thanks Brooks. Your question led me to keep digging into if the
EDIT Thanks for the prompt responses. Please see what the real question is. I
PROBLEM SOLVED: I was overlooking a very simple issue, and that is that this
SOLVED Look at the comments below <head> <script src=http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js></script> <script type=text/javascript> $(document).ready(function() { $('#playlistToggle').click(function(event)
EDIT: I solved my issue...take a look at my answer. Correct if it's wrong.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.