aboutsummaryrefslogtreecommitdiff
path: root/Week11 Mandelbrot/src
diff options
context:
space:
mode:
Diffstat (limited to 'Week11 Mandelbrot/src')
-rw-r--r--Week11 Mandelbrot/src/com/camilstaps/mandelbrot/Mandelbrot.java (renamed from Week11 Mandelbrot/src/fractals/Mandelbrot.java)26
-rw-r--r--Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java52
-rw-r--r--Week11 Mandelbrot/src/com/camilstaps/mandelbrot/ZoomFrame.java147
-rw-r--r--Week11 Mandelbrot/src/fractals/Grid.java1
-rw-r--r--Week11 Mandelbrot/src/fractals/MainWindow.java48
5 files changed, 200 insertions, 74 deletions
diff --git a/Week11 Mandelbrot/src/fractals/Mandelbrot.java b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/Mandelbrot.java
index 3f2aaaa..7adb93f 100644
--- a/Week11 Mandelbrot/src/fractals/Mandelbrot.java
+++ b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/Mandelbrot.java
@@ -1,20 +1,28 @@
-package fractals;
+package com.camilstaps.mandelbrot;
+
+import fractals.MainWindow;
/**
- * An example of the use of our pixel drawing framework
- * @author Sjaak Smetsers
- * @version 1.0, 14-03-2013
+ * Solutions to week 11
+ * @author Camil Staps
*/
public class Mandelbrot {
- public Mandelbrot() {
+ /**
+ * MainWindow does the hard work
+ * @param args
+ */
+ public static void main(String args[]) {
MainWindow fractal_win = new MainWindow ();
}
- public static void main(String args[]) {
- Mandelbrot mandelbrot = new Mandelbrot();
- }
-
+ /**
+ * Calculate the mandel number up to a certain amount of iterations of the function
+ * @param x
+ * @param y
+ * @param repetitions
+ * @return
+ */
public static int mandelNumber(double x, double y, int repetitions) {
double x_n = x, y_n = y;
int n = 0;
diff --git a/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java
index 4cb489a..fe6a69e 100644
--- a/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java
+++ b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java
@@ -4,10 +4,9 @@
package com.camilstaps.mandelbrot;
import fractals.Grid;
-import fractals.Mandelbrot;
/**
- *
+ * Showing the Mandelbrot fractal on the screen
* @author camilstaps
*/
public class MandelbrotView {
@@ -15,11 +14,21 @@ public class MandelbrotView {
private final MandelbrotProvider mandelbrotProvider;
private final Grid grid;
+ /**
+ * Get info from a MandelbrotProvider; work with a Grid
+ * @param mp
+ * @param grid
+ */
public MandelbrotView(MandelbrotProvider mp, Grid grid) {
mandelbrotProvider = mp;
this.grid = grid;
}
+ /**
+ * Get the 'mathematical' x value for a certain x coordinate of the screen
+ * @param x_on_screen
+ * @return
+ */
public double getX(int x_on_screen) {
double centerX = mandelbrotProvider.getCenterX();
double scale = mandelbrotProvider.getScale();
@@ -30,6 +39,11 @@ public class MandelbrotView {
return min_x + ((double) x_on_screen) * (max_x - min_x) / grid_w;
}
+ /**
+ * Get the 'mathematical' y value for a certain y coordinate of the screen
+ * @param y_on_screen
+ * @return
+ */
public double getY(int y_on_screen) {
double centerY = mandelbrotProvider.getCenterY();
double scale = mandelbrotProvider.getScale();
@@ -40,6 +54,9 @@ public class MandelbrotView {
return min_y + (((float) y_on_screen) * (max_y - min_y) / grid_h);
}
+ /**
+ * (Re)draw the fractal
+ */
public void redraw() {
int repetitions = mandelbrotProvider.getRepetitions();
@@ -47,21 +64,46 @@ public class MandelbrotView {
for (int i = 0; i < grid_w; i++) {
for (int j = 0; j < grid_h; j++) {
+ // Convert mandel number to some number in [0,pi] to let the colourise below work nicely
double mandel = ((double) Mandelbrot.mandelNumber(getX(i), getY(j), repetitions) * Math.PI) / repetitions;
- int[] color = {
+ // Different mandel numbers have different colours
+ int[] colour = {
(int) (30 + 220 * Math.sin(mandel)),
(int) (30 + 220 * Math.sin(mandel + 2 * Math.PI / 3)),
(int) (30 + 220 * Math.sin(mandel + 4 * Math.PI / 3))};
+ // This is a grayscale version:
//int[] color = {(int) (255 * mandel), (int) (255 * mandel), (int) (255 * mandel)};
- grid.setPixel(i, j, color);
+ grid.setPixel(i, j, colour);
}
}
}
-
+
+ /**
+ * Get user input
+ */
public interface MandelbrotProvider {
+ /**
+ * Get center X value
+ * @return
+ */
public double getCenterX();
+
+ /**
+ * Get center Y value
+ * @return
+ */
public double getCenterY();
+
+ /**
+ * Get scale factor
+ * @return
+ */
public double getScale();
+
+ /**
+ * Get max amount of iterations for the mandel function
+ * @return
+ */
public int getRepetitions();
}
diff --git a/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/ZoomFrame.java b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/ZoomFrame.java
index 1dfa2a7..3c15d5b 100644
--- a/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/ZoomFrame.java
+++ b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/ZoomFrame.java
@@ -12,8 +12,8 @@ import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
/**
- *
- * @author camilstaps
+ * A frame in which zooming is possible
+ * @author Camil Staps, s4498062
*/
public class ZoomFrame extends JFrame implements MouseListener, MouseMotionListener {
@@ -40,65 +40,103 @@ public class ZoomFrame extends JFrame implements MouseListener, MouseMotionListe
}
@Override
- public void mouseClicked(MouseEvent me) {
- }
+ public void mouseClicked(MouseEvent me) {}
+ /**
+ * Save the coordinates
+ * @param me
+ */
@Override
public void mousePressed(MouseEvent me) {
old_x = start_x = me.getX();
old_y = start_y = me.getY();
}
+ /**
+ * On shift-click, zoom out.
+ * On click, zoom in.
+ * On drag, zoom the box.
+ * @param me
+ */
@Override
public void mouseReleased(MouseEvent me) {
if (me.getX() == start_x && me.getY() == start_y) {
- if (mandelbrotView == null || mandelbrotTextFields == null)
- return;
-
- mandelbrotTextFields.setCenterX(mandelbrotView.getX(me.getX()));
- mandelbrotTextFields.setCenterY(mandelbrotView.getY(me.getY()));
-
if ((me.getModifiers() & InputEvent.SHIFT_MASK) != 0) {
- mandelbrotTextFields.setScale(mandelbrotTextFields.getScale() / 2);
+ zoomOut(me);
} else {
- mandelbrotTextFields.setScale(mandelbrotTextFields.getScale() * 2);
+ zoomIn(me);
}
} else {
- double center_x = (mandelbrotView.getX(me.getX())
- - mandelbrotView.getX(start_x)) / 2
- + mandelbrotView.getX(start_x);
- double center_y = (mandelbrotView.getY(me.getY())
- - mandelbrotView.getY(start_y)) / 2
- + mandelbrotView.getY(start_y);
-
- mandelbrotTextFields.setCenterX(center_x);
- mandelbrotTextFields.setCenterY(center_y);
-
- double scale = mandelbrotTextFields.getScale()
- / (me.getX() - start_x)
- * getWidth();
-
- System.err.println(mandelbrotTextFields.getScale());
- System.err.println(me.getX() - start_x);
- System.err.println(getWidth());
-
- mandelbrotTextFields.setScale(scale);
+ zoomBox(me);
}
mandelbrotView.redraw();
dragging = false;
}
+
+ /**
+ * Zoom in with factor 2
+ * @param me
+ */
+ private void zoomIn(MouseEvent me) {
+ if (mandelbrotView == null || mandelbrotTextFields == null)
+ return;
- @Override
- public void mouseEntered(MouseEvent me) {
+ mandelbrotTextFields.setCenterX(mandelbrotView.getX(me.getX()));
+ mandelbrotTextFields.setCenterY(mandelbrotView.getY(me.getY()));
+
+ mandelbrotTextFields.setScale(mandelbrotTextFields.getScale() * 2);
}
+
+ /**
+ * Zoom out with factor 2
+ * @param me
+ */
+ private void zoomOut(MouseEvent me) {
+ if (mandelbrotView == null || mandelbrotTextFields == null)
+ return;
- @Override
- public void mouseExited(MouseEvent me) {
+ mandelbrotTextFields.setCenterX(mandelbrotView.getX(me.getX()));
+ mandelbrotTextFields.setCenterY(mandelbrotView.getY(me.getY()));
+
+ mandelbrotTextFields.setScale(mandelbrotTextFields.getScale() / 2);
}
+
+ /**
+ * Zoom to the selected box.
+ * Intentionally chose to let the user only select squares, otherwise it's too easy to mess up the scale
+ * @param me
+ */
+ private void zoomBox(MouseEvent me) {
+ double center_x = (mandelbrotView.getX(me.getX())
+ - mandelbrotView.getX(start_x)) / 2
+ + mandelbrotView.getX(start_x);
+ double center_y = (mandelbrotView.getY(me.getY())
+ - mandelbrotView.getY(start_y)) / 2
+ + mandelbrotView.getY(start_y);
+
+ mandelbrotTextFields.setCenterX(center_x);
+ mandelbrotTextFields.setCenterY(center_y);
+
+ double scale = mandelbrotTextFields.getScale()
+ / (me.getX() - start_x)
+ * getWidth();
+
+ mandelbrotTextFields.setScale(scale);
+ }
+
+ @Override
+ public void mouseEntered(MouseEvent me) {}
@Override
+ public void mouseExited(MouseEvent me) {}
+
+ /**
+ * Erase the old box, draw the new box
+ * @param me
+ */
+ @Override
public void mouseDragged(MouseEvent me) {
Graphics g = getSafeGraphics();
if (g != null) {
@@ -111,6 +149,10 @@ public class ZoomFrame extends JFrame implements MouseListener, MouseMotionListe
}
}
+ /**
+ * Semi-singleton construction for graphics
+ * @return
+ */
private Graphics getSafeGraphics() {
if (graphics == null) {
graphics = getGraphics();
@@ -119,6 +161,9 @@ public class ZoomFrame extends JFrame implements MouseListener, MouseMotionListe
return graphics;
}
+ /**
+ * Erase the old zoombox if it exists
+ */
private void eraseZoombox() {
if (start_x < 0 || start_y < 0 || old_x < 0 || old_y < 0)
return;
@@ -128,15 +173,41 @@ public class ZoomFrame extends JFrame implements MouseListener, MouseMotionListe
}
@Override
- public void mouseMoved(MouseEvent me) {
- //System.err.println(me.toString());
- }
+ public void mouseMoved(MouseEvent me) {}
+ /**
+ * Interface to get and set user input
+ */
public interface MandelbrotTextFields {
+
+ /**
+ * Set the center X
+ * @param x
+ */
public void setCenterX(double x);
+
+ /**
+ * Set the center Y
+ * @param y
+ */
public void setCenterY(double y);
+
+ /**
+ * Get the scale factor
+ * @return
+ */
public double getScale();
+
+ /**
+ * Set the scale factor
+ * @param scale
+ */
public void setScale(double scale);
+
+ /**
+ * Set the max amount of iterations for the mandel function
+ * @param repetitions
+ */
public void setRepetitions(int repetitions);
}
diff --git a/Week11 Mandelbrot/src/fractals/Grid.java b/Week11 Mandelbrot/src/fractals/Grid.java
index be6048b..7daad63 100644
--- a/Week11 Mandelbrot/src/fractals/Grid.java
+++ b/Week11 Mandelbrot/src/fractals/Grid.java
@@ -1,7 +1,6 @@
package fractals;
/**
- *
* @author Sjaak Smetsers
* @version 1.0, 13-03-2013
*/
diff --git a/Week11 Mandelbrot/src/fractals/MainWindow.java b/Week11 Mandelbrot/src/fractals/MainWindow.java
index b6276af..8f89217 100644
--- a/Week11 Mandelbrot/src/fractals/MainWindow.java
+++ b/Week11 Mandelbrot/src/fractals/MainWindow.java
@@ -1,7 +1,5 @@
package fractals;
-
-
import com.camilstaps.mandelbrot.MandelbrotView;
import com.camilstaps.mandelbrot.ZoomFrame;
import java.awt.BorderLayout;
@@ -17,19 +15,17 @@ import javax.swing.JPanel;
import javax.swing.JTextField;
/**
- *
+ * The window for week 11: a gridview for the fractal, and controls
* @author Sjaak Smetsers
- * @version 1.0, 14-03-2013
- */
-
-/**
- * creates a window to which a GridView panel is added
- *
+ * @author Camil Staps
*/
public class MainWindow implements MandelbrotView.MandelbrotProvider, ZoomFrame.MandelbrotTextFields {
// the size of the window
public static final int WIDTH = 650, HEIGHT = 650;
+ /**
+ * Initial values
+ */
private final String INITIAL_CENTER_X = "0",
INITIAL_CENTER_Y = "0",
INITIAL_SCALE = "100",
@@ -38,13 +34,19 @@ public class MainWindow implements MandelbrotView.MandelbrotProvider, ZoomFrame.
// The grip panel
private final GridView grid;
+ /**
+ * Controls
+ */
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 final MandelbrotView mandelbrotController;
+ /**
+ * To allow redrawing
+ */
+ private final MandelbrotView mandelbrotView;
public MainWindow () {
ZoomFrame mainFrame = new ZoomFrame ("Mandelbrot");
@@ -60,20 +62,26 @@ public class MainWindow implements MandelbrotView.MandelbrotProvider, ZoomFrame.
grid = new GridView(WIDTH - insets.left - insets.right, HEIGHT - insets.top - insets.bottom);
mainFrame.add(grid, BorderLayout.CENTER);
- mandelbrotController = new MandelbrotView(this, grid);
- mandelbrotController.redraw();
+ mandelbrotView = new MandelbrotView(this, grid);
+ mandelbrotView.redraw();
- mainFrame.setMandelbrotView(mandelbrotController);
+ mainFrame.setMandelbrotView(mandelbrotView);
mainFrame.setMandelbrotTextFields(this);
- JPanel left = new JPanel(new BorderLayout());
+ mainFrame.add(getControlsPanel(), BorderLayout.EAST);
+
+ mainFrame.pack();
+ }
+
+ private JPanel getControlsPanel() {
+ JPanel right = new JPanel(new BorderLayout());
JPanel panel = new JPanel(new GridLayout(10,1));
button_redraw.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("Redraw")) {
- mandelbrotController.redraw();
+ mandelbrotView.redraw();
}
}
});
@@ -94,17 +102,15 @@ public class MainWindow implements MandelbrotView.MandelbrotProvider, ZoomFrame.
field_centerY.setText(INITIAL_CENTER_Y);
field_scale.setText(INITIAL_SCALE);
field_repetitions.setText(INITIAL_REPETITIONS);
- mandelbrotController.redraw();
+ mandelbrotView.redraw();
}
});
panel.add(button_reset);
- left.add(panel, BorderLayout.NORTH);
- left.add(new JPanel(), BorderLayout.CENTER);
+ right.add(panel, BorderLayout.NORTH);
+ right.add(new JPanel(), BorderLayout.CENTER);
- mainFrame.add(left, BorderLayout.EAST);
-
- mainFrame.pack();
+ return right;
}
/**