0

A program I am writing, upon running, generates a mandelbrot set mostly correct, however it additionally generates a grid of blank space. I have not found a solution that does not sacrifice image quality.

Two nested loops is used to loop through every pixel on the JPanel, and run functionParser.Parse(...) recursively to calculate whether a given pixel is a part of the Mandelbrot set. Since the grid is generated without respect to the size of the window while the set is generated, I do not believe there is a problem with the parser, rather that there is either a problem with the Graphics class or with the loop.

A simplified example is below, which is intended to fill the window black, but exhibits the same grid behavior.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

import Fractal.FractalPanel.DrawPane;

public class Dysfunctional extends JFrame{
    private static final long serialVersionUID = 1L;
    int r;
    JFrame frame;
    DrawPane d;
    public Dysfunctional() {
        setSize(200,200);
        this.setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        d=new DrawPane();
        setContentPane(d);
    }
    class DrawPane extends JPanel{
        int dimenX;
        int dimenY;
        public DrawPane() {
            this.setVisible(true);
            System.out.println("Pane generated");
            Dimension dim = getSize();
            dimenX=(int)dim.getWidth();
            dimenY=(int)dim.getHeight();
            System.out.println("Painting...");
        }
        public void paintComponent(Graphics g) {
            System.out.println("Generating...");
            Dimension dim = getSize();
            dimenX=(int)dim.getWidth();
            dimenY=(int)dim.getHeight();
            for(int xVal=0;xVal<dimenX;xVal++) {
                for(int yVal=0;yVal<dimenY;yVal++) {
                    if(true) {
                        g.drawRect(xVal,yVal,0,0);
                        //System.out.println("drawing");
                    }
                }
            }
            System.out.println("Done!");
            
        }
    }
    public static void main(String[] args) {
        new Dysfunctional();
    }
}

Output of contracted code, with unintended grid

Output of full code, Mandelbrot set with grid

camickr
  • 321,443
  • 19
  • 166
  • 288
  • All I see when I run the code is a completely black panel. Note, you can't make the panel and the frame the same size. The frame contains the titlebar and borders. So the proper way to do custom painting is to override the get preferred size of DrawPane to return your desired size. Then when you create the frame you add your DrawPane to the frame and then invoke pack() on the frame before making it visible. Now all components can be sized correctly. – camickr May 13 '22 at 22:26
  • 1
    unable to reproduce the problem... are you eventually using Windows, and scaled display? (Settings - System - Display - Scale and Layout) – user16320675 May 13 '22 at 22:27
  • I am using windows to run the program, though I'm not sure if the display is considered scaleable. If you mean that the window can be expanded or contracted, then it can, however it does not impact the grid. I am using Eclipse as an IDE, if that could affect things. – InboardApollo20 May 13 '22 at 22:31
  • I retract that, it seems I was scaled in by 125% and that changing to 100% has removed the problem. – InboardApollo20 May 13 '22 at 22:32
  • It means you have a high resolution screen and your screen display is likely set to 125% or 150%. You can remove the scaling for your application (instead of changing it for all applications on your desktop). Check out: https://stackoverflow.com/questions/65742162/how-to-fix-the-blurry-image-when-i-run-my-java-swing-project/65742492#65742492. Or, you might try using fillRect(...) instead of drawRect(...). Can't remember if this will fix the scaling. – camickr May 13 '22 at 22:33

0 Answers0