Skip to content

Commit

Permalink
Added window showing rendering progress
Browse files Browse the repository at this point in the history
  • Loading branch information
smathep committed Aug 12, 2023
1 parent 7f5339a commit 2f28299
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
40 changes: 38 additions & 2 deletions FractalRenderer.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import java.awt.FlowLayout;
import java.awt.Image;
// import java.awt.Panel;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
Expand All @@ -10,13 +13,18 @@
import java.util.concurrent.atomic.AtomicInteger;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

//https://www.javamex.com/tutorials/graphics/bufferedimage_setrgb.shtml
// https://en.wikipedia.org/wiki/Mandelbrot_set#Computer_drawings
// https://blog.bede.io/how-to-render-a-fractal-fast/

public class FractalRenderer{
public final int MAX_ITERATION = 5000;
public final int DISPLAY_WIDTH = 1920;
public final int DISPLAY_HEIGHT = 1080;
private Path cwd;
private int width, height;
private BufferedImage img;
Expand All @@ -25,6 +33,8 @@ public class FractalRenderer{
private double centerX; //default: -.875
private double centerY; //default: 0
private double zoomFactor; //default: 1
private JFrame frame;
private JLabel label;

public FractalRenderer(){
Path cwdTest = Paths.get(System.getProperty("user.dir") + "/images");
Expand All @@ -36,9 +46,19 @@ public FractalRenderer(){
System.exit(1);
}
}
frame = new JFrame("Mandelbrot");
label = new JLabel();
cwd = cwdTest;
}

private void displayFractal(){
//had to scale the sizes down slightly to fit the entire set in the GUI window, 0.79 just happened to be the best scaling factor
label.setIcon(new ImageIcon(img.getScaledInstance((int)(DISPLAY_WIDTH*0.79), (int)(DISPLAY_HEIGHT*0.79), Image.SCALE_FAST)));
// label.setIcon(new ImageIcon(img));
frame.setVisible(true);

}

public boolean renderSetup(int width, int height, double cX, double cY, double zoom, int threadC){
this.width = width;
this.height = height;
Expand All @@ -50,6 +70,19 @@ public boolean renderSetup(int width, int height, double cX, double cY, double z
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
RenderThread[] threads = new RenderThread[threadCount];
int numSections = threadCount*2;

ImageIcon imageicon = new ImageIcon(img);

frame.setLayout(new FlowLayout());
// frame.setSize(img.getWidth(), img.getHeight());
frame.setSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);

label.setIcon(imageicon);
frame.getContentPane().add(label);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.add(;)

for(int tCount = 0; tCount < threadCount; tCount++){
threads[tCount] = new RenderThread(cwd, "r-thread" + tCount, img, width, height, this);
threads[tCount].setStartX(0);
Expand Down Expand Up @@ -87,9 +120,7 @@ public void setPixel(int px, int py){
double xScaleUpper = centerX + xToYRatio / zoomFactor;
double yScaleLower = centerY - (Math.abs(xScaleUpper - xScaleLower) / 2 / xToYRatio);
double yScaleUpper = centerY + (Math.abs(xScaleUpper - xScaleLower) / xToYRatio / 2);
// double x0 = scale(px, width, -1.666666666675, -1.666666666625); //-2.75, 1
double x0 = scale(px, width, xScaleLower, xScaleUpper); //-2.75, 1
// double y0 = scale(py, height, -0.000000000013, 0.000000000013); //-1, 1 Ratio for x to y: 1.875
double y0 = scale(py, height, yScaleLower, yScaleUpper); //-1, 1 Ratio for x to y: 1.875
double x = 0.0, y = 0.0;
int iteration = 0;
Expand All @@ -100,10 +131,14 @@ public void setPixel(int px, int py){
x = xtemp;
iteration++;
}
// System.out.println("Iteration: " + iteration);

int r = (int)scale(Math.pow(iteration, 4), MAX_ITERATION, 0, 255);// red component 0...255
int g = (int)scale(Math.pow(iteration, 3), MAX_ITERATION, 0, 255);// green component 0...255
int b = (int)scale(Math.pow(iteration, 3), MAX_ITERATION, 0, 255);// blue component 0...255
// int r = (int)scale(iteration, MAX_ITERATION, 0, 255);// red component 0...255
// int g = (int)scale(iteration, MAX_ITERATION, 0, 255);// green component 0...255
// int b = (int)scale(iteration, MAX_ITERATION, 0, 255);// blue component 0...255
// int a = (int) (255);// alpha (transparency) component 0...255
// builds an RGB int value
int rgb = (r << 16) | (g << 8) | b;
Expand All @@ -118,6 +153,7 @@ public double scale(double n, int max, double minPrime, double maxPrime){
}

public boolean startNextSection(RenderThread rt){
displayFractal();

if(nextRenderSection.get() < (threadCount * 2)){
// System.out.println(rt.tName + " getting new section");
Expand Down
4 changes: 2 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
compile:
compile: FractalRenderer.java RenderThread.java Driver.java
javac Driver.java FractalRenderer.java RenderThread.java -d bin

package: compile
jar --create --file Mandelbrot.jar -e Driver -C bin .

#runs default settings
run:
run: package
java -jar Mandelbrot.jar

clean:
Expand Down

0 comments on commit 2f28299

Please sign in to comment.