John Smith
John Smith

Reputation: 719

How to change the colour of my Mandelbrot Set display?

I found some code to display a Mandelbrot set in java, this one has a turqoise colour theme and I was wondering how to change this to another colour, for example a red colour theme. Here is the code:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;

public class Mandelbrot extends JFrame {

    private final int MAX_ITER = 570;
    private final double ZOOM = 150;
    private BufferedImage I;
    private double zx, zy, cX, cY, tmp;

    public Mandelbrot() {
        super("Mandelbrot Set");
        setBounds(100, 100, 800, 600);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        I = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
        for (int y = 0; y < getHeight(); y++) {
            for (int x = 0; x < getWidth(); x++) {
                zx = zy = 0;
                cX = (x - 400) / ZOOM;
                cY = (y - 300) / ZOOM;
                int iter = MAX_ITER;
                while (zx * zx + zy * zy < 4 && iter > 0) {
                    tmp = zx * zx - zy * zy + cX;
                    zy = 2.0 * zx * zy + cY;
                    zx = tmp;
                    iter--;
                }
                I.setRGB(x, y, iter | (iter << 8));
            }
        }
    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(I, 0, 0, this);
    }

    public static void main(String[] args) {
        new Mandelbrot().setVisible(true);
    }
}

The hex code for red is #FF0000 and the rbg decimal code is (255,0,0) if that helps.

Thanks for your time.

Upvotes: 1

Views: 242

Answers (1)

gpasch
gpasch

Reputation: 2682

The essense of your code (in coloring) is this

 I.setRGB(x, y, iter | (iter << 8));

whatever iter is is plugged into the lower bits and also shifted by 8 bits which corersponds to the Green value in the middle.

So I guess you could try

 I.setRGB(x, y, iter << 12);

This will plug the iter value in the upper bits (Red Green).

Upvotes: 1

Related Questions