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

  • SEARCH
  • Home
  • 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 7633229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:49:30+00:00 2026-05-31T06:49:30+00:00

I am trying to implement multi-threading in my Java Mandelbrot application: This is what

  • 0

I am trying to implement multi-threading in my Java Mandelbrot application:

This is what I have so far:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public class MandelbrotSet {
   private int numberOfIterations;
   private double realMin;
   private double realMax;
   private double imaginaryMin;
   private double imaginaryMax;
   private int width;
   private int height;
   public BufferedImage image;
   public Graphics2D imageGraphics;

   public MandelbrotSet() {
      // Set the width and the height
      this.width = 600;
      this.height = 400;
      image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
      imageGraphics = image.createGraphics();
                this.realMin = -2.0;
      this.realMax = 1;
      this.imaginaryMin = -1;
      this.imaginaryMax = 1;
      this.numberOfIterations = 1000;
   }

   public Complex calculateComplexNumber(int x, int y) {
      double realPart = realMin + x * (realMax - realMin) / (this.getWidth() - 1);
      double imaginaryPart = imaginaryMax - y * (imaginaryMax - imaginaryMin) / (this.getHeight() - 1);

      return new Complex(realPart, imaginaryPart);
   }

   public void calculateMandelbrotImagePoints() {
      Thread[] threads = new Thread[4];

      for (int i = 0; i < maxThreads; i++) {
         threads[i] = new Thread(new MThread(i));
         threads[i].start();
      }
   }

   class MThread implements Runnable {
      private int i;

      public MThread(int i) {
         this.i = i;
      }

      //Method uses the thread number to draw the mandelbrot in columns
      public void run() {
         for (int x = i; x < width; x += 4) {
            for (int y = 0; y < height; y++) {
               int n = 0;
               Complex c = calculateComplexNumber(x, y);
               Complex z = c;

               while ((zNumber.modulusSquared() < 4.0D) && (n < numberOfIterations)) {
                  z = z.square();
                  z.add(c);
                  n++;
               }

               if (n == numberOfIterations) {
                  imageGraphics.setColor(Color.BLACK);
               } else {
                  imageGraphics.setColor(Color.getHSBColor(n / 100.0F, 1, 1));
               }
               imageGraphics.drawLine(x,y,x,y);
            }
         }
      }
   }
}

The problem that is occurring is that when the image is drawn incorrect pixels are displayed in the image:

https://i.stack.imgur.com/wq2TN.png

When I check a thread with something like:

threads[i].isAlive();

the image seems to display successfully, however the image takes much longer (up to 3x as long) to render.

Two things I was wondering.

  1. Where am I going wrong?

  2. What would be the best way for drawing Mandelbrots to a BufferedImage for a large number of iterations (>1000)?

  • 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-05-31T06:49:31+00:00Added an answer on May 31, 2026 at 6:49 am

    I expect his is what @Michael Chang was suggesting. I’ve adjusted the code to render in bands.

    Please note that I have not been able to test this. I am not familiar with Java graphics.

                                                                                               import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    
    public class MandelbrotSet {
      private int numberOfIterations;
      private double realMin;
      private double realMax;
      private double imaginaryMin;
      private double imaginaryMax;
      private int width;
      private int height;
      public BufferedImage image;
      public Graphics2D imageGraphics;
      static final int nThreads = 4;
    
      public MandelbrotSet(int width, int height) {
        // Set the width and the height
        this.width = width;
        this.height = height;
        image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
        imageGraphics = image.createGraphics();
        this.realMin = -2.0;
        this.realMax = 1;
        this.imaginaryMin = -1;
        this.imaginaryMax = 1;
        this.numberOfIterations = 1000;
      }
    
      public Complex calculateComplexNumber(int x, int y) {
        double realPart = realMin + x * (realMax - realMin) / (width - 1);
        double imaginaryPart = imaginaryMax - y * (imaginaryMax - imaginaryMin) / (height - 1);
    
        return new Complex(realPart, imaginaryPart);
      }
    
      public void calculateMandelbrotImagePoints() {
        Thread[] threads = new Thread[nThreads];
        int bandHeight = height / nThreads;
    
        for (int i = 0; i < nThreads; i++) {
          BufferedImage band = new BufferedImage(width, bandHeight, BufferedImage.TYPE_4BYTE_ABGR);
          threads[i] = new Thread(new MThread(band, i * bandHeight, bandHeight));
          threads[i].start();
        }
      }
    
      class MThread implements Runnable {
        final BufferedImage band;
        final Graphics2D g;
        final int top;
        final int height;
    
        private MThread(BufferedImage band, int top, int height) {
          this.band = band;
          g = band.createGraphics();
          this.top = top;
          this.height = height;
        }
    
        @Override
        public void run() {
          for (int x = 0; x < width; x++) {
            for (int y = top; y < top + height; y++) {
              int n = 0;
              Complex c = calculateComplexNumber(x, y);
              Complex z = c;
    
              while ((z.times(z).mod() < 4.0D) && (n < numberOfIterations)) {
                z = z.times(z).plus(c);
                n++;
              }
    
              if (n == numberOfIterations) {
                g.setColor(Color.BLACK);
              } else {
                g.setColor(Color.getHSBColor(n / 100.0F, 1, 1));
              }
              g.drawLine(x, y-top, x, y-top);
            }
          }
          // Do somehing to merge this band ino the main one.
          // Not familiar with java graphics so this may be wrong.
          imageGraphics.drawImage(band, null, 0, top);
        }
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a multi-tenant application, that is - data of all
I am trying to implement this multi-touch android tutorial http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-5-implementing-the-drag-gesture/1789?tag=mantle_skin;content I am stuck in
I have a multi-page form, and I'm trying to implement Back buttons, so that
I have a multi page form that I am trying to implement on my
I'm trying to implement a server-client socket program in Java that can support multiple
Hi fellow android developers, I'm trying to implement a multi-pane layout as described here
I'm pulling my hair out on this one. I am trying to implement a
I am trying to implement MVVM (Model View ViewModel) pattern for my WinForms application.
I've been trying to implement a dynamic multi-model form with accepts_nested_attributes_for in my rails
I am trying to implement multi touch zoom in and zoom out functionality to

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.