From 85751141b5705dba503b507ab34cc4ee734a9e6a Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Thu, 4 Jun 2015 22:33:05 +0200 Subject: Started own version --- Week15 Mandelbrot/build.xml | 73 - Week15 Mandelbrot/manifest.mf | 3 - Week15 Mandelbrot/nbproject/build-impl.xml | 1413 -------------------- Week15 Mandelbrot/nbproject/genfiles.properties | 8 - Week15 Mandelbrot/nbproject/project.properties | 71 - Week15 Mandelbrot/nbproject/project.xml | 15 - .../src/com/camilstaps/mandelbrot/DrawView.java | 168 +++ .../com/camilstaps/mandelbrot/FractalModel.java | 67 + .../mandelbrot/MandelbrotController.java | 18 + .../camilstaps/mandelbrot/MandelbrotFractal.java | 96 ++ .../camilstaps/mandelbrot/MandelbrotWindow.java | 39 + .../src/com/camilstaps/mandelbrot/ZoomFrame.java | 67 + Week15 Mandelbrot/src/mandelbrot/Area.java | 27 - .../src/mandelbrot/AreaController.java | 22 - Week15 Mandelbrot/src/mandelbrot/AreaSelector.java | 41 - Week15 Mandelbrot/src/mandelbrot/ColorChooser.java | 36 - Week15 Mandelbrot/src/mandelbrot/Grid.java | 16 - Week15 Mandelbrot/src/mandelbrot/GridFiller.java | 59 - Week15 Mandelbrot/src/mandelbrot/GridView.java | 63 - Week15 Mandelbrot/src/mandelbrot/MainWindow.java | 33 - Week15 Mandelbrot/src/mandelbrot/Mandelbrot.java | 34 - Week15 Mandelbrot/src/mandelbrot/RGBColors.java | 82 -- Week15 Mandelbrot/src/mandelbrot/Stopper.java | 14 - 23 files changed, 455 insertions(+), 2010 deletions(-) delete mode 100644 Week15 Mandelbrot/build.xml delete mode 100644 Week15 Mandelbrot/manifest.mf delete mode 100644 Week15 Mandelbrot/nbproject/build-impl.xml delete mode 100644 Week15 Mandelbrot/nbproject/genfiles.properties delete mode 100644 Week15 Mandelbrot/nbproject/project.properties delete mode 100644 Week15 Mandelbrot/nbproject/project.xml create mode 100644 Week15 Mandelbrot/src/com/camilstaps/mandelbrot/DrawView.java create mode 100644 Week15 Mandelbrot/src/com/camilstaps/mandelbrot/FractalModel.java create mode 100644 Week15 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotController.java create mode 100644 Week15 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotFractal.java create mode 100644 Week15 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotWindow.java create mode 100644 Week15 Mandelbrot/src/com/camilstaps/mandelbrot/ZoomFrame.java delete mode 100644 Week15 Mandelbrot/src/mandelbrot/Area.java delete mode 100644 Week15 Mandelbrot/src/mandelbrot/AreaController.java delete mode 100644 Week15 Mandelbrot/src/mandelbrot/AreaSelector.java delete mode 100644 Week15 Mandelbrot/src/mandelbrot/ColorChooser.java delete mode 100644 Week15 Mandelbrot/src/mandelbrot/Grid.java delete mode 100644 Week15 Mandelbrot/src/mandelbrot/GridFiller.java delete mode 100644 Week15 Mandelbrot/src/mandelbrot/GridView.java delete mode 100644 Week15 Mandelbrot/src/mandelbrot/MainWindow.java delete mode 100644 Week15 Mandelbrot/src/mandelbrot/Mandelbrot.java delete mode 100644 Week15 Mandelbrot/src/mandelbrot/RGBColors.java delete mode 100644 Week15 Mandelbrot/src/mandelbrot/Stopper.java diff --git a/Week15 Mandelbrot/build.xml b/Week15 Mandelbrot/build.xml deleted file mode 100644 index c5a2ca3..0000000 --- a/Week15 Mandelbrot/build.xml +++ /dev/null @@ -1,73 +0,0 @@ - - - - - - - - - - - Builds, tests, and runs the project Week15 Mandelbrot. - - - diff --git a/Week15 Mandelbrot/manifest.mf b/Week15 Mandelbrot/manifest.mf deleted file mode 100644 index 1574df4..0000000 --- a/Week15 Mandelbrot/manifest.mf +++ /dev/null @@ -1,3 +0,0 @@ -Manifest-Version: 1.0 -X-COMMENT: Main-Class will be added automatically by build - diff --git a/Week15 Mandelbrot/nbproject/build-impl.xml b/Week15 Mandelbrot/nbproject/build-impl.xml deleted file mode 100644 index fa82428..0000000 --- a/Week15 Mandelbrot/nbproject/build-impl.xml +++ /dev/null @@ -1,1413 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must set src.dir - Must set test.src.dir - Must set build.dir - Must set dist.dir - Must set build.classes.dir - Must set dist.javadoc.dir - Must set build.test.classes.dir - Must set build.test.results.dir - Must set build.classes.excludes - Must set dist.jar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must set javac.includes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - No tests executed. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must set JVM to use for profiling in profiler.info.jvm - Must set profiler agent JVM arguments in profiler.info.jvmargs.agent - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must select some files in the IDE or set javac.includes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - To run this application from the command line without Ant, try: - - java -jar "${dist.jar.resolved}" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must select one file in the IDE or set run.class - - - - Must select one file in the IDE or set run.class - - - - - - - - - - - - - - - - - - - - - - - Must select one file in the IDE or set debug.class - - - - - Must select one file in the IDE or set debug.class - - - - - Must set fix.includes - - - - - - - - - - This target only works when run from inside the NetBeans IDE. - - - - - - - - - Must select one file in the IDE or set profile.class - This target only works when run from inside the NetBeans IDE. - - - - - - - - - This target only works when run from inside the NetBeans IDE. - - - - - - - - - - - - - This target only works when run from inside the NetBeans IDE. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must select one file in the IDE or set run.class - - - - - - Must select some files in the IDE or set test.includes - - - - - Must select one file in the IDE or set run.class - - - - - Must select one file in the IDE or set applet.url - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Must select some files in the IDE or set javac.includes - - - - - - - - - - - - - - - - - - - - Some tests failed; see details above. - - - - - - - - - Must select some files in the IDE or set test.includes - - - - Some tests failed; see details above. - - - - Must select some files in the IDE or set test.class - Must select some method in the IDE or set test.method - - - - Some tests failed; see details above. - - - - - Must select one file in the IDE or set test.class - - - - Must select one file in the IDE or set test.class - Must select some method in the IDE or set test.method - - - - - - - - - - - - - - Must select one file in the IDE or set applet.url - - - - - - - - - Must select one file in the IDE or set applet.url - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/Week15 Mandelbrot/nbproject/genfiles.properties b/Week15 Mandelbrot/nbproject/genfiles.properties deleted file mode 100644 index 50b0aed..0000000 --- a/Week15 Mandelbrot/nbproject/genfiles.properties +++ /dev/null @@ -1,8 +0,0 @@ -build.xml.data.CRC32=b07ebbd6 -build.xml.script.CRC32=7abc5264 -build.xml.stylesheet.CRC32=8064a381@1.75.2.48 -# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. -# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. -nbproject/build-impl.xml.data.CRC32=b07ebbd6 -nbproject/build-impl.xml.script.CRC32=73fd3cc1 -nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.75.2.48 diff --git a/Week15 Mandelbrot/nbproject/project.properties b/Week15 Mandelbrot/nbproject/project.properties deleted file mode 100644 index a69efe3..0000000 --- a/Week15 Mandelbrot/nbproject/project.properties +++ /dev/null @@ -1,71 +0,0 @@ -annotation.processing.enabled=true -annotation.processing.enabled.in.editor=false -annotation.processing.processor.options= -annotation.processing.processors.list= -annotation.processing.run.all.processors=true -annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output -build.classes.dir=${build.dir}/classes -build.classes.excludes=**/*.java,**/*.form -# This directory is removed when the project is cleaned: -build.dir=build -build.generated.dir=${build.dir}/generated -build.generated.sources.dir=${build.dir}/generated-sources -# Only compile against the classpath explicitly listed here: -build.sysclasspath=ignore -build.test.classes.dir=${build.dir}/test/classes -build.test.results.dir=${build.dir}/test/results -# Uncomment to specify the preferred debugger connection transport: -#debug.transport=dt_socket -debug.classpath=\ - ${run.classpath} -debug.test.classpath=\ - ${run.test.classpath} -# This directory is removed when the project is cleaned: -dist.dir=dist -dist.jar=${dist.dir}/Week15_Mandelbrot.jar -dist.javadoc.dir=${dist.dir}/javadoc -excludes= -includes=** -jar.compress=false -javac.classpath= -# Space-separated list of extra javac options -javac.compilerargs= -javac.deprecation=false -javac.processorpath=\ - ${javac.classpath} -javac.source=1.6 -javac.target=1.6 -javac.test.classpath=\ - ${javac.classpath}:\ - ${build.classes.dir} -javac.test.processorpath=\ - ${javac.test.classpath} -javadoc.additionalparam= -javadoc.author=false -javadoc.encoding=${source.encoding} -javadoc.noindex=false -javadoc.nonavbar=false -javadoc.notree=false -javadoc.private=false -javadoc.splitindex=true -javadoc.use=true -javadoc.version=false -javadoc.windowtitle= -main.class=mandelbrot.Mandelbrot -manifest.file=manifest.mf -meta.inf.dir=${src.dir}/META-INF -mkdist.disabled=false -platform.active=default_platform -run.classpath=\ - ${javac.classpath}:\ - ${build.classes.dir} -# Space-separated list of JVM arguments used when running the project -# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value -# or test-sys-prop.name=value to set system properties for unit tests): -run.jvmargs= -run.test.classpath=\ - ${javac.test.classpath}:\ - ${build.test.classes.dir} -source.encoding=UTF-8 -src.dir=src -test.src.dir=test diff --git a/Week15 Mandelbrot/nbproject/project.xml b/Week15 Mandelbrot/nbproject/project.xml deleted file mode 100644 index 013727d..0000000 --- a/Week15 Mandelbrot/nbproject/project.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - org.netbeans.modules.java.j2seproject - - - Week15 Mandelbrot - - - - - - - - - diff --git a/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/DrawView.java b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/DrawView.java new file mode 100644 index 0000000..16500f5 --- /dev/null +++ b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/DrawView.java @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2015 Camil Staps + */ +package com.camilstaps.mandelbrot; + +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.image.BufferedImage; +import java.awt.image.WritableRaster; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Observable; +import java.util.Observer; +import javax.swing.JPanel; +import javax.swing.SwingWorker; + +/** + * + * @author camilstaps + */ +public class DrawView extends JPanel implements Observer { + + private final FractalModel fractalModel; + + private final BufferedImage image; + private final WritableRaster raster; + private final int width, height; + private static final int DEFAULT_WIDTH = 500, DEFAULT_HEIGHT = 500; + + private int pixelCounter; + private static final int COUNTER_MAX = 1000; + + private static final int REPETITIONS_MAX = 150; + + public DrawView(FractalModel fractalModel) { + this.fractalModel = fractalModel; + fractalModel.addObserver(this); + + width = DEFAULT_WIDTH; + height = DEFAULT_HEIGHT; + + image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + raster = image.getRaster(); + + setPreferredSize(new Dimension(width, height)); + + update(fractalModel, null); + } + + @Override + public int getWidth() { + return width; + } + + @Override + public int getHeight() { + return height; + } + + @Override + public void update(Observable o, Object o1) { + Updater task = new Updater(); + task.execute(); + } + + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + g.drawImage(image, 0, 0, null); + } + + public void setPixel(int x, int y, int[] rgb) { + raster.setPixel(x, y, rgb); + pixelCounter++; + if (pixelCounter > COUNTER_MAX) { + pixelCounter = 0; + repaint(); + } + } + + protected double getRealX(int pixel_x) { + return ((double) pixel_x * (fractalModel.getEndX() - fractalModel.getStartX()) / width) + fractalModel.getStartX(); + } + + protected double getRealY(int pixel_y) { + return ((double) pixel_y * (fractalModel.getEndY() - fractalModel.getStartY()) / height) + fractalModel.getStartY(); + } + + protected class Updater extends SwingWorker, Map> { + private boolean doneProcessing = true; + + @Override + protected Map doInBackground() throws Exception { + Map results = new HashMap<>(); + + for (int repetitions = 1; repetitions < REPETITIONS_MAX; repetitions++) { + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + // Convert mandel number to some number in [0,pi] to let the colourise below work nicely + double mandel = ((double) fractalModel.getMandelNumber(getRealX(x), getRealY(y), repetitions) * Math.PI) / repetitions; + + //System.err.println("x,y : " + getRealX(x) + "," + getRealY(y) + "; " + mandel); + + Point p = new Point(x, y); + results.put(p, mandel); + } + } + + System.out.println("Rep " + repetitions); + + if (doneProcessing) { + doneProcessing = false; + publish(results); + } + setProgress(repetitions * 100 / REPETITIONS_MAX); + } + + return results; + } + + @Override + protected synchronized void process(List> results) { + for (Map resultMap : results) { + for (Entry result : resultMap.entrySet()) { + double value = result.getValue(); + + // Different mandel numbers have different colours +// int[] rgb = { +// (int) (30 + 220 * Math.sin(value)), +// (int) (30 + 220 * Math.sin(value + 2 * Math.PI / 3)), +// (int) (30 + 220 * Math.sin(value + 4 * Math.PI / 3))}; + // This is a grayscale version: + int[] rgb = {(int) (255 * value), (int) (255 * value), (int) (255 * value)}; + + setPixel(result.getKey().x, result.getKey().y, rgb); + } + } + doneProcessing = true; + } + } + + protected class Point { + int x, y; + + public Point(int x, int y) { + this.x = x; + this.y = y; + } + + @Override + public boolean equals(Object another) { + if (another == null || another.getClass() != Point.class) { + return false; + } + Point that = (Point) another; + return that.x == x && that.y == y; + } + + @Override + public int hashCode() { + return (x << 8) | y; + } + } + +} diff --git a/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/FractalModel.java b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/FractalModel.java new file mode 100644 index 0000000..184913d --- /dev/null +++ b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/FractalModel.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2015 Camil Staps + */ +package com.camilstaps.mandelbrot; + +import java.util.Observable; + +/** + * + * @author camilstaps + */ +public class FractalModel extends Observable { + + private double start_x, start_y, end_x, end_y; + + public FractalModel() { + start_x = -1; + start_y = -1; + end_x = 1; + end_y = 1; + } + + public int getMandelNumber(Fractal.Point p, int repetitions) { + return Fractal.mandelNumber(p, repetitions); + } + + public int getMandelNumber(double x, double y, int repetitions) { + return Fractal.mandelNumber(x, y, repetitions); + } + + public double getStartX() { + return start_x; + } + + public double getStartY() { + return start_y; + } + + public double getEndX() { + return end_x; + } + + public double getEndY() { + return end_y; + } + + public void setStartX(double x) { + start_x = x; + notifyObservers(); + } + + public void setStartY(double y) { + start_y = y; + notifyObservers(); + } + + public void setEndX(double x) { + end_x = x; + notifyObservers(); + } + + public void setEndY(double y) { + end_y = y; + notifyObservers(); + } + +} diff --git a/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotController.java b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotController.java new file mode 100644 index 0000000..9233cc8 --- /dev/null +++ b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotController.java @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2015 Camil Staps + */ +package com.camilstaps.mandelbrot; + +/** + * + * @author camilstaps + */ +public class MandelbrotController { + + private final Fractal fractal; + + public MandelbrotController(Fractal fractal) { + this.fractal = fractal; + } + +} diff --git a/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotFractal.java b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotFractal.java new file mode 100644 index 0000000..72c3d7a --- /dev/null +++ b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotFractal.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2015 Camil Staps + */ +package com.camilstaps.mandelbrot; + +import java.util.HashMap; +import java.util.Map; + +/** + * + * @author camilstaps + */ +public class MandelbrotFractal { + + private static final Map mandelNumbers = new HashMap<>(); + + public static int mandelNumber(double x, double y, int repetitions) { + Point p = new Point(x, y); + return mandelNumber(p, repetitions); + } + + public static int mandelNumber(Point p, int repetitions) { + if (mandelNumbers.containsKey(p)) { + Result result = mandelNumbers.get(p); + if (repetitions < result.repetitions || + result.mandelNumber < result.repetitions) { + return result.mandelNumber; + } else { + calculateMandelNumber(p, repetitions, result); + return result.mandelNumber; + } + } else { + Result result = calculateMandelNumber(p, repetitions); + mandelNumbers.put(p, result); + return result.mandelNumber; + } + } + + protected static Result calculateMandelNumber(Point p, int repetitions) { + Result start = new Result(); + start.x = p.x; + start.y = p.y; + calculateMandelNumber(p, repetitions, start); + return start; + } + + protected static void calculateMandelNumber(Point p, int repetitions, Result start) { + int n = start.repetitions; + + while (start.x * start.x + start.y * start.y <= 4 && n <= repetitions) { + double new_x = start.x * start.x - start.y * start.y + p.x; + start.y = 2 * start.x * start.y + p.y; + start.x = new_x; + n++; + } + + start.mandelNumber = n; + } + + public static class Point { + double x, y; + + public Point(double x, double y) { + this.x = x; + this.y = y; + } + + @Override + public boolean equals(Object another) { + if (another == null || another.getClass() != Point.class) { + return false; + } + Point that = (Point) another; + return that.x == x && that.y == y; + } + + @Override + public int hashCode() { + int hash = 7; + hash = 97 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32)); + hash = 97 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32)); + return hash; + } + + @Override + public String toString() { + return String.format("(%f,%f)", x, y); + } + } + + protected static class Result { + int mandelNumber = -1, repetitions = -1; + double x, y; + } + +} diff --git a/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotWindow.java b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotWindow.java new file mode 100644 index 0000000..163dca9 --- /dev/null +++ b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotWindow.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2015 Camil Staps + */ +package com.camilstaps.mandelbrot; + +import javax.swing.JButton; +import javax.swing.JTextField; + +/** + * + * @author camilstaps + */ +public class MandelbrotWindow { + + private final DrawView drawView; + + private final String INITIAL_CENTER_X = "0", + INITIAL_CENTER_Y = "0", + INITIAL_SCALE = "100", + INITIAL_REPETITIONS = "100"; + + private final JTextField field_centerX = new JTextField(INITIAL_CENTER_X, 6); + private final JTextField field_centerY = new JTextField(INITIAL_CENTER_Y, 6); + private final JTextField field_scale = new JTextField(INITIAL_SCALE, 6); + private final JTextField field_repetitions = new JTextField(INITIAL_REPETITIONS, 6); + private final JButton button_redraw = new JButton("Redraw"); + + private MandelbrotWindow() { + FractalModel fm = new FractalModel(); + drawView = new DrawView(fm); + + ZoomFrame frame = new ZoomFrame("Mandelbrot", drawView); + } + + public static void main(String[] args) { + MandelbrotWindow mw = new MandelbrotWindow(); + } + +} diff --git a/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/ZoomFrame.java b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/ZoomFrame.java new file mode 100644 index 0000000..38d16fc --- /dev/null +++ b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/ZoomFrame.java @@ -0,0 +1,67 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015 Camil Staps + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.camilstaps.mandelbrot; + +import java.awt.Color; +import java.awt.Graphics; +import javax.swing.JFrame; + +/** + * A frame in which zooming is possible + * @author Camil Staps, s4498062 + */ +public class ZoomFrame extends JFrame { + + private Graphics graphics; + + private final DrawView drawView; + + public ZoomFrame(String s, DrawView drawView) { + super(s); + + this.drawView = drawView; + + setSize(drawView.getWidth(), drawView.getHeight()); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setResizable(false); + setLocationRelativeTo(null); + setVisible(true); + + add(drawView); + } + + /** + * Semi-singleton construction for graphics + * @return + */ + private Graphics getSafeGraphics() { + if (graphics == null) { + graphics = getGraphics(); + graphics.setXORMode(Color.white); + } + + return graphics; + } + +} diff --git a/Week15 Mandelbrot/src/mandelbrot/Area.java b/Week15 Mandelbrot/src/mandelbrot/Area.java deleted file mode 100644 index 75331e3..0000000 --- a/Week15 Mandelbrot/src/mandelbrot/Area.java +++ /dev/null @@ -1,27 +0,0 @@ -package mandelbrot; - -/** - * - * @author Sjaak - */ -public class Area { - private final double startX, startY, width, height; - - public Area (double x_tl, double y_tl, double width, double height) { - startX = x_tl; startY = y_tl; this.width = width; this.height = height; - } - - public double getX () { return startX; } - public double getY () { return startY; } - public double getWidth () { return width; } - public double getHeight () { return height; } - - public Area zoom (int xul, int yul, int zw, int zh, int tw, int th) { - double zoom_fact = ((double) zw) / tw; - return new Area (startX + (width * xul) / tw, - startY - (height * yul) / th, - width * zoom_fact, - height * zoom_fact); - } - -} diff --git a/Week15 Mandelbrot/src/mandelbrot/AreaController.java b/Week15 Mandelbrot/src/mandelbrot/AreaController.java deleted file mode 100644 index 304eb0f..0000000 --- a/Week15 Mandelbrot/src/mandelbrot/AreaController.java +++ /dev/null @@ -1,22 +0,0 @@ -package mandelbrot; - -/** - * - * @author Sjaak Smetsers - */ -public class AreaController { - GridFiller filler; - Grid grid; - - public AreaController ( GridFiller filler, Grid grid ) { - this.filler = filler; - this.grid = grid; - } - - public void setArea(AreaSelector selector, int x, int y, int w, int h ) { - Area area = filler.getArea().zoom(x, y, w, h, grid.getWidth(), grid.getHeight()); - filler = new GridFiller (grid, area); - filler.fill(); - } - -} diff --git a/Week15 Mandelbrot/src/mandelbrot/AreaSelector.java b/Week15 Mandelbrot/src/mandelbrot/AreaSelector.java deleted file mode 100644 index 3af6fc2..0000000 --- a/Week15 Mandelbrot/src/mandelbrot/AreaSelector.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ -package mandelbrot; - -import java.awt.Component; -import java.awt.event.MouseEvent; -import javax.swing.event.MouseInputAdapter; - -/** - * - * @author Sjaak - */ -public class AreaSelector extends MouseInputAdapter { - - private Component component; - private AreaController controller; - private Stopper toBeStopped ; - - public AreaSelector(Component component, AreaController controller) { - this.component = component; - this.controller = controller; - component.addMouseListener(this); - } - - @Override - public void mouseClicked(MouseEvent e) { - if ( toBeStopped != null ){ - toBeStopped.tryToStop(); - } - int w = component.getWidth() / 2; - int h = component.getHeight() / 2; - controller.setArea(this, e.getX() - w / 2, e.getY() - h / 2, w, h); - } - - public void setStopper( Stopper stopper ) { - this.toBeStopped = stopper; - } - -} diff --git a/Week15 Mandelbrot/src/mandelbrot/ColorChooser.java b/Week15 Mandelbrot/src/mandelbrot/ColorChooser.java deleted file mode 100644 index c3aa441..0000000 --- a/Week15 Mandelbrot/src/mandelbrot/ColorChooser.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ -package mandelbrot; - -/** - * - * @author Sjaak - */ -public class ColorChooser { - private int maxIndex; - - public ColorChooser () { - maxIndex = 0; - } - - public void setMaxIndex (int max_index) { - maxIndex = max_index; - } - - public int getColorIndex (double x0, double y0) { - double x = x0, y = y0; - int color_index = 0; - while (x * x + y * y < 4.0) { - double nx = x * x - y * y + x0; - y = 2 * x * y + y0; - x = nx; - color_index++; - if (color_index == maxIndex) { - return -1; - } - } - return color_index; - } -} diff --git a/Week15 Mandelbrot/src/mandelbrot/Grid.java b/Week15 Mandelbrot/src/mandelbrot/Grid.java deleted file mode 100644 index 74b4bd6..0000000 --- a/Week15 Mandelbrot/src/mandelbrot/Grid.java +++ /dev/null @@ -1,16 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ -package mandelbrot; - -/** - * - * @author Sjaak - */ -public interface Grid { - void setPixel (int x, int y, int [] rgb); - - int getWidth (); - int getHeight (); -} diff --git a/Week15 Mandelbrot/src/mandelbrot/GridFiller.java b/Week15 Mandelbrot/src/mandelbrot/GridFiller.java deleted file mode 100644 index 1e741a2..0000000 --- a/Week15 Mandelbrot/src/mandelbrot/GridFiller.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ -package mandelbrot; - -import java.awt.Color; -import java.awt.Graphics; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; -import javax.swing.SwingWorker; - -/** - * - * @author Sjaak - */ -public class GridFiller { - - private Area area; - private Grid grid; - public static final int MAX_ITERATIONS = 400; - private static final RGBColors rgbColors = new RGBColors(MAX_ITERATIONS); - private static final ColorChooser colorChooser = new ColorChooser(); - - public GridFiller(Grid grid, Area area) { - this.grid = grid; - this.area = area; - colorChooser.setMaxIndex(MAX_ITERATIONS); - } - - public Area getArea() { - return area; - } - - public void fill() { - int grid_w = grid.getWidth(), grid_h = grid.getHeight(); - double area_w = area.getWidth(), area_h = area.getHeight(); - double dx = area_w / grid_w, dy = area_h / grid_h; - - double x = area.getX(); - for (int i = 0; i < grid_w; i++) { - double y = area.getY(); - for (int j = 0; j < grid_h; j++) { - int color = colorChooser.getColorIndex(x, y); - grid.setPixel(i, j, - color == -1 ? RGBColors.BLACK : rgbColors.getColor(color)); - - y -= dy; - } - try { - Thread.sleep( 2 ); - } catch (InterruptedException e) { - System.out.println("Sleeping thread interrupted"); - } - x += dx; - } - } -} diff --git a/Week15 Mandelbrot/src/mandelbrot/GridView.java b/Week15 Mandelbrot/src/mandelbrot/GridView.java deleted file mode 100644 index 9c43b68..0000000 --- a/Week15 Mandelbrot/src/mandelbrot/GridView.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ -package mandelbrot; - -import java.awt.Dimension; -import java.awt.Graphics; -import java.awt.image.BufferedImage; -import java.awt.image.WritableRaster; -import java.util.logging.Level; -import java.util.logging.Logger; -import javax.swing.JComponent; -import javax.swing.JPanel; - -/** - * - * @author Sjaak - */ -public class GridView extends JPanel implements Grid -{ - private BufferedImage gridImage; - private WritableRaster gridRaster; - - - public static final int GRID_WIDTH = 600, GRID_HEIGHT = 600; - public static final int PIXELS_PER_REPAINT = (GRID_WIDTH * GRID_WIDTH) / 20; - - public GridView() { - gridImage = new BufferedImage(GRID_WIDTH, GRID_WIDTH, BufferedImage.TYPE_INT_RGB); - gridRaster = gridImage.getRaster(); - setPreferredSize(new Dimension(GRID_WIDTH, GRID_WIDTH)); - } - - @Override - public void paintComponent(Graphics g) { - super.paintComponent(g); - g.drawImage(gridImage, 0, 0, null); - } - - private int nrOfPixelsSet = 0; - - @Override - public void setPixel(int x, int y, int[] rgb) { - gridRaster.setPixel (x, y, rgb); - nrOfPixelsSet++; - if (nrOfPixelsSet == PIXELS_PER_REPAINT) { - repaint(); - nrOfPixelsSet = 0; - } - } - - @Override - public int getWidth() { - return GRID_WIDTH; - } - - @Override - public int getHeight() { - return GRID_HEIGHT; - } - -} diff --git a/Week15 Mandelbrot/src/mandelbrot/MainWindow.java b/Week15 Mandelbrot/src/mandelbrot/MainWindow.java deleted file mode 100644 index eba8c3e..0000000 --- a/Week15 Mandelbrot/src/mandelbrot/MainWindow.java +++ /dev/null @@ -1,33 +0,0 @@ -package mandelbrot; - - - -import java.awt.Insets; - -import javax.swing.JFrame; - -/** - * - * @author Sjaak Smetsers - * @version 1.0, 14-03-2013 - */ - -/** - * creates a window to which a GridView panel is added - * - */ -public class MainWindow { - - public MainWindow ( GridView grid ) { - JFrame mainFrame = new JFrame ("Mandelbrot"); - - mainFrame.setLocationRelativeTo(null); - mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - mainFrame.setResizable(false); - mainFrame.setVisible(true); - - mainFrame.add(grid); - mainFrame.pack(); - } - -} diff --git a/Week15 Mandelbrot/src/mandelbrot/Mandelbrot.java b/Week15 Mandelbrot/src/mandelbrot/Mandelbrot.java deleted file mode 100644 index 1980eed..0000000 --- a/Week15 Mandelbrot/src/mandelbrot/Mandelbrot.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ -package mandelbrot; - -import java.awt.BorderLayout; -import java.awt.Insets; -import javax.swing.JPanel; - - -/* -This class creates an applet for generating Mandelbrot sets. The applet is -meant to be embedded in an HTML page and has hooks to interact with the page. -Functions are provided to allow the user to zoom in and out and to move around -on the surface of the figure. The user can also select from a set of color -schemes. -*/ - -public class Mandelbrot -{ - public static void main(String args[]) { - - GridView grid = new GridView(); - MainWindow mandel = new MainWindow ( grid ); - - Area area = new Area ( -2.5, 2.5, 5, 5 ); - GridFiller filler = new GridFiller ( grid, area ); - AreaController controller = new AreaController ( filler, grid ); - AreaSelector selector = new AreaSelector ( grid, controller ); - filler.fill(); - } - -} \ No newline at end of file diff --git a/Week15 Mandelbrot/src/mandelbrot/RGBColors.java b/Week15 Mandelbrot/src/mandelbrot/RGBColors.java deleted file mode 100644 index 2abd7d5..0000000 --- a/Week15 Mandelbrot/src/mandelbrot/RGBColors.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ -package mandelbrot; - -/** - * - * @author Sjaak - */ -public class RGBColors { - - private int [][] rgbColors; - - public static final int [] BLACK = new int [3]; - - public RGBColors (int max_color_index) { - rgbColors = new int [max_color_index][3]; - randomPalets (); - randomColorSet (); - - } - - private static final int MAXRGB = 256; - - private static int [][][] palets = { - {{0,0},{1,-1},{0,1}}, - {{1,-1},{1,0},{0,0}}, - {{0,1},{0,1},{1,-1}}, - {{1,-1},{0,0},{1,0}}, - {{0,1},{0,1},{1,0}}, - {{1,0},{1,-1},{1,-1}}, - {{1,0},{0,1},{0,1}}, - {{1,-1},{1,0},{1,-1}} - }; - - private boolean randomBool () { - return Math.random() < 0.5; - } - - void randomPalets () { - for (int p = 0; p < palets.length; p++) { - for (int c = 0; c < 3; c++) { - int cw = randomBool () ? 1 : 0; - int cd = randomBool () ? (cw * -1) : (1 - cw); - palets [p][c][0] = cw; - palets [p][c][1] = cd; - } - } - } - - private void randomColorSet () { - int p_size = rgbColors.length / palets.length; - for (int p = 0; p < palets.length; p++) { - int [][] palet = palets [p]; - int r = palet [0][0] == 1 ? MAXRGB-1 : 0; - int g = palet [1][0] == 1 ? MAXRGB-1 : 0; - int b = palet [2][0] == 1 ? MAXRGB-1 : 0; - - int dr = palet [0][1] * MAXRGB / p_size; - int dg = palet [1][1] * MAXRGB / p_size; - int db = palet [2][1] * MAXRGB / p_size; - - for (int i = 0 ; i < p_size ; i++) { - rgbColors[i + p * p_size][0] = r; - rgbColors[i + p * p_size][1] = g; - rgbColors[i + p * p_size][2] = b; - r += dr; - g += dg; - b += db; - } - } - } - - public int [] getColor (int color_index) { - return rgbColors [color_index]; - } - - public int [] getColor2 (int color_index) { - return rgbColors [color_index]; - } -} diff --git a/Week15 Mandelbrot/src/mandelbrot/Stopper.java b/Week15 Mandelbrot/src/mandelbrot/Stopper.java deleted file mode 100644 index f47a471..0000000 --- a/Week15 Mandelbrot/src/mandelbrot/Stopper.java +++ /dev/null @@ -1,14 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package mandelbrot; - -/** - * - * @author Sjaak - */ -public interface Stopper { - void tryToStop(); -} -- cgit v1.2.3