aboutsummaryrefslogtreecommitdiff
path: root/Week15 Mandelbrot/src/mandelbrot
diff options
context:
space:
mode:
Diffstat (limited to 'Week15 Mandelbrot/src/mandelbrot')
-rw-r--r--Week15 Mandelbrot/src/mandelbrot/Area.java27
-rw-r--r--Week15 Mandelbrot/src/mandelbrot/AreaController.java22
-rw-r--r--Week15 Mandelbrot/src/mandelbrot/AreaSelector.java41
-rw-r--r--Week15 Mandelbrot/src/mandelbrot/ColorChooser.java36
-rw-r--r--Week15 Mandelbrot/src/mandelbrot/Grid.java16
-rw-r--r--Week15 Mandelbrot/src/mandelbrot/GridFiller.java59
-rw-r--r--Week15 Mandelbrot/src/mandelbrot/GridView.java63
-rw-r--r--Week15 Mandelbrot/src/mandelbrot/MainWindow.java33
-rw-r--r--Week15 Mandelbrot/src/mandelbrot/Mandelbrot.java34
-rw-r--r--Week15 Mandelbrot/src/mandelbrot/RGBColors.java82
-rw-r--r--Week15 Mandelbrot/src/mandelbrot/Stopper.java14
11 files changed, 427 insertions, 0 deletions
diff --git a/Week15 Mandelbrot/src/mandelbrot/Area.java b/Week15 Mandelbrot/src/mandelbrot/Area.java
new file mode 100644
index 0000000..75331e3
--- /dev/null
+++ b/Week15 Mandelbrot/src/mandelbrot/Area.java
@@ -0,0 +1,27 @@
+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
new file mode 100644
index 0000000..304eb0f
--- /dev/null
+++ b/Week15 Mandelbrot/src/mandelbrot/AreaController.java
@@ -0,0 +1,22 @@
+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
new file mode 100644
index 0000000..3af6fc2
--- /dev/null
+++ b/Week15 Mandelbrot/src/mandelbrot/AreaSelector.java
@@ -0,0 +1,41 @@
+/*
+ * 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
new file mode 100644
index 0000000..c3aa441
--- /dev/null
+++ b/Week15 Mandelbrot/src/mandelbrot/ColorChooser.java
@@ -0,0 +1,36 @@
+/*
+ * 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
new file mode 100644
index 0000000..74b4bd6
--- /dev/null
+++ b/Week15 Mandelbrot/src/mandelbrot/Grid.java
@@ -0,0 +1,16 @@
+/*
+ * 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
new file mode 100644
index 0000000..1e741a2
--- /dev/null
+++ b/Week15 Mandelbrot/src/mandelbrot/GridFiller.java
@@ -0,0 +1,59 @@
+/*
+ * 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
new file mode 100644
index 0000000..9c43b68
--- /dev/null
+++ b/Week15 Mandelbrot/src/mandelbrot/GridView.java
@@ -0,0 +1,63 @@
+/*
+ * 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
new file mode 100644
index 0000000..eba8c3e
--- /dev/null
+++ b/Week15 Mandelbrot/src/mandelbrot/MainWindow.java
@@ -0,0 +1,33 @@
+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
new file mode 100644
index 0000000..1980eed
--- /dev/null
+++ b/Week15 Mandelbrot/src/mandelbrot/Mandelbrot.java
@@ -0,0 +1,34 @@
+/*
+ * 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
new file mode 100644
index 0000000..2abd7d5
--- /dev/null
+++ b/Week15 Mandelbrot/src/mandelbrot/RGBColors.java
@@ -0,0 +1,82 @@
+/*
+ * 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
new file mode 100644
index 0000000..f47a471
--- /dev/null
+++ b/Week15 Mandelbrot/src/mandelbrot/Stopper.java
@@ -0,0 +1,14 @@
+/*
+ * 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();
+}