From 57cc1163708260f3e4f5b56cc1de2ad2ee625018 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Wed, 29 Apr 2015 22:37:13 +0200 Subject: cleanup --- .../mandelbrot/MandelbrotController.java | 67 --------------------- .../com/camilstaps/mandelbrot/MandelbrotView.java | 68 +++++++++++++++++++++ .../src/com/camilstaps/mandelbrot/ZoomFrame.java | 28 ++++----- Week11 Mandelbrot/src/fractals/ColorTable.java | 69 ---------------------- Week11 Mandelbrot/src/fractals/GridFiller.java | 40 ------------- Week11 Mandelbrot/src/fractals/GridView.java | 1 - Week11 Mandelbrot/src/fractals/MainWindow.java | 58 ++++++++++++------ 7 files changed, 122 insertions(+), 209 deletions(-) delete mode 100644 Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotController.java create mode 100644 Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java delete mode 100644 Week11 Mandelbrot/src/fractals/ColorTable.java delete mode 100644 Week11 Mandelbrot/src/fractals/GridFiller.java diff --git a/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotController.java b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotController.java deleted file mode 100644 index f072805..0000000 --- a/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotController.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2015 Camil Staps - */ -package com.camilstaps.mandelbrot; - -import fractals.Grid; -import fractals.Mandelbrot; - -/** - * - * @author camilstaps - */ -public class MandelbrotController { - - private final MandelbrotProvider mandelbrotProvider; - private final Grid grid; - - public MandelbrotController(MandelbrotProvider mp, Grid grid) { - mandelbrotProvider = mp; - this.grid = grid; - } - - public double getX(int x_on_screen) { - double centerX = mandelbrotProvider.getCenterX(); - double scale = mandelbrotProvider.getScale(); - int grid_w = grid.getWidth(); - double min_x = centerX - grid_w / scale / 2, - max_x = centerX + grid_w / scale / 2; - - return min_x + (((float) x_on_screen) / grid_w) * (max_x - min_x); - } - - public double getY(int y_on_screen) { - double centerY = mandelbrotProvider.getCenterY(); - double scale = mandelbrotProvider.getScale(); - int grid_h = grid.getHeight(); - double min_y = centerY - grid_h / scale / 2, - max_y = centerY + grid_h / scale / 2; - - return min_y + (((float) y_on_screen) / grid_h) * (max_y - min_y); - } - - public void redraw() { - int repetitions = mandelbrotProvider.getRepetitions(); - - int grid_w = grid.getWidth(), grid_h = grid.getHeight(); - - for (int i = 0; i < grid_w; i++) { - for (int j = 0; j < grid_h; j++) { - double mandel = ((double) Mandelbrot.mandelNumber(getX(i), getY(j), repetitions) * Math.PI) / repetitions; - int[] color = { - (int) (255 * Math.sin(mandel)), - (int) (255 * Math.sin(mandel + Math.PI / 3)), - (int) (255 * Math.sin(mandel + 2 * Math.PI / 3))}; - grid.setPixel(i, j, color); - } - } - } - - public interface MandelbrotProvider { - public double getCenterX(); - public double getCenterY(); - public double getScale(); - public int getRepetitions(); - } - -} diff --git a/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java new file mode 100644 index 0000000..4cb489a --- /dev/null +++ b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotView.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2015 Camil Staps + */ +package com.camilstaps.mandelbrot; + +import fractals.Grid; +import fractals.Mandelbrot; + +/** + * + * @author camilstaps + */ +public class MandelbrotView { + + private final MandelbrotProvider mandelbrotProvider; + private final Grid grid; + + public MandelbrotView(MandelbrotProvider mp, Grid grid) { + mandelbrotProvider = mp; + this.grid = grid; + } + + public double getX(int x_on_screen) { + double centerX = mandelbrotProvider.getCenterX(); + double scale = mandelbrotProvider.getScale(); + int grid_w = grid.getWidth(); + double min_x = centerX - grid_w / scale / 2, + max_x = centerX + grid_w / scale / 2; + + return min_x + ((double) x_on_screen) * (max_x - min_x) / grid_w; + } + + public double getY(int y_on_screen) { + double centerY = mandelbrotProvider.getCenterY(); + double scale = mandelbrotProvider.getScale(); + int grid_h = grid.getHeight(); + double min_y = centerY - grid_h / scale / 2, + max_y = centerY + grid_h / scale / 2; + + return min_y + (((float) y_on_screen) * (max_y - min_y) / grid_h); + } + + public void redraw() { + int repetitions = mandelbrotProvider.getRepetitions(); + + int grid_w = grid.getWidth(), grid_h = grid.getHeight(); + + for (int i = 0; i < grid_w; i++) { + for (int j = 0; j < grid_h; j++) { + double mandel = ((double) Mandelbrot.mandelNumber(getX(i), getY(j), repetitions) * Math.PI) / repetitions; + int[] color = { + (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))}; + //int[] color = {(int) (255 * mandel), (int) (255 * mandel), (int) (255 * mandel)}; + grid.setPixel(i, j, color); + } + } + } + + public interface MandelbrotProvider { + public double getCenterX(); + public double getCenterY(); + public double getScale(); + public int getRepetitions(); + } + +} diff --git a/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/ZoomFrame.java b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/ZoomFrame.java index b7b5a68..1dfa2a7 100644 --- a/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/ZoomFrame.java +++ b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/ZoomFrame.java @@ -17,7 +17,7 @@ import javax.swing.JFrame; */ public class ZoomFrame extends JFrame implements MouseListener, MouseMotionListener { - private MandelbrotController mandelbrotController; + private MandelbrotView mandelbrotView; private MandelbrotTextFields mandelbrotTextFields; private Graphics graphics; @@ -31,8 +31,8 @@ public class ZoomFrame extends JFrame implements MouseListener, MouseMotionListe addMouseMotionListener(this); } - public void setMandelbrotController(MandelbrotController mc) { - mandelbrotController = mc; + public void setMandelbrotView(MandelbrotView mv) { + mandelbrotView = mv; } public void setMandelbrotTextFields(MandelbrotTextFields mtf) { @@ -52,11 +52,11 @@ public class ZoomFrame extends JFrame implements MouseListener, MouseMotionListe @Override public void mouseReleased(MouseEvent me) { if (me.getX() == start_x && me.getY() == start_y) { - if (mandelbrotController == null || mandelbrotTextFields == null) + if (mandelbrotView == null || mandelbrotTextFields == null) return; - mandelbrotTextFields.setCenterX(mandelbrotController.getX(me.getX())); - mandelbrotTextFields.setCenterY(mandelbrotController.getY(me.getY())); + mandelbrotTextFields.setCenterX(mandelbrotView.getX(me.getX())); + mandelbrotTextFields.setCenterY(mandelbrotView.getY(me.getY())); if ((me.getModifiers() & InputEvent.SHIFT_MASK) != 0) { mandelbrotTextFields.setScale(mandelbrotTextFields.getScale() / 2); @@ -64,12 +64,12 @@ public class ZoomFrame extends JFrame implements MouseListener, MouseMotionListe mandelbrotTextFields.setScale(mandelbrotTextFields.getScale() * 2); } } else { - double center_x = (mandelbrotController.getX(me.getX()) - - mandelbrotController.getX(start_x)) / 2 - + mandelbrotController.getX(start_x); - double center_y = (mandelbrotController.getY(me.getY()) - - mandelbrotController.getY(start_y)) / 2 - + mandelbrotController.getY(start_y); + 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); @@ -85,7 +85,7 @@ public class ZoomFrame extends JFrame implements MouseListener, MouseMotionListe mandelbrotTextFields.setScale(scale); } - mandelbrotController.redraw(); + mandelbrotView.redraw(); dragging = false; } @@ -137,7 +137,7 @@ public class ZoomFrame extends JFrame implements MouseListener, MouseMotionListe public void setCenterY(double y); public double getScale(); public void setScale(double scale); - public void setRepetitions(double repetitions); + public void setRepetitions(int repetitions); } } diff --git a/Week11 Mandelbrot/src/fractals/ColorTable.java b/Week11 Mandelbrot/src/fractals/ColorTable.java deleted file mode 100644 index 064bd15..0000000 --- a/Week11 Mandelbrot/src/fractals/ColorTable.java +++ /dev/null @@ -1,69 +0,0 @@ -package fractals; - -import java.awt.Color; - -/** - * - * @author Sjaak Smetsers - * @version 1.0. 14-03-2014 - */ -/** - * Converting indexes (ranging from 0 to tableSize) to RGB colors - * - * @author Sjaak - */ -public class ColorTable { - - // a two dimensional conversion array - private int[][] rgbColors; - private int tableSize; - - private static final int MAXRGB = 256; - - public static final int[] BLACK = new int[3]; - - /** - * converts specified color to an rgb array - * - * @param color the color to be converted - * @return the corresponding array of rgb values - */ - private static int[] color2RGB(Color color) { - int[] rgb = {color.getRed(), color.getGreen(), color.getBlue()}; - return rgb; - } - - /** - * creates and fills the table with the specified size - * - * @param tableSize the size of the table - */ - public ColorTable(int tableSize) { - this.tableSize = tableSize; - this.rgbColors = new int[tableSize][3]; - - randomColorSet(); - } - - /** - * fills the table randomly - */ - private void randomColorSet() { - for (int[] color : rgbColors) { - for (int c = 0; c < 3; c++) { - color[c] = (int) (Math.random() * 256); - } - } - } - - /** - * converts an index into an rgb value - * - * @param color_index to be converted - * @return the resulting rgb value - */ - public int[] getColor(int color_index) { - return rgbColors[color_index % tableSize]; - } - -} diff --git a/Week11 Mandelbrot/src/fractals/GridFiller.java b/Week11 Mandelbrot/src/fractals/GridFiller.java deleted file mode 100644 index 73b422e..0000000 --- a/Week11 Mandelbrot/src/fractals/GridFiller.java +++ /dev/null @@ -1,40 +0,0 @@ -package fractals; - -/** - * - * @author Sjaak Smetsers -** @version 1.0, 13-03-2013 - */ - -/** - * A skeleton class illustrating the use of the grid interface - * - */ -public class GridFiller { - private Grid grid; // the grid to be filled - private ColorTable colorTable; // a table for converting indexes to - // rgb values - - /** - * The constructor - * @param grid to be filled - */ - public GridFiller (Grid grid) { - colorTable = new ColorTable (30); // some random value, needs to be adjusted - this.grid = grid; - } - - /** - * fills the whole grid with some arbitrarily chosen color - * - */ - public void fill () { - int grid_w = grid.getWidth(), grid_h = grid.getHeight(); - for (int i = 0; i < grid_w; i++) { - for (int j = 0; j < grid_h; j++) { - int color_index = i/5 * grid_w/5 + j/5; - grid.setPixel(i, j, colorTable.getColor(color_index)); - } - } - } -} diff --git a/Week11 Mandelbrot/src/fractals/GridView.java b/Week11 Mandelbrot/src/fractals/GridView.java index 4a51114..d85dc6e 100644 --- a/Week11 Mandelbrot/src/fractals/GridView.java +++ b/Week11 Mandelbrot/src/fractals/GridView.java @@ -5,7 +5,6 @@ import java.awt.Graphics; import java.awt.image.BufferedImage; import java.awt.image.WritableRaster; -import javax.swing.JComponent; import javax.swing.JPanel; /** diff --git a/Week11 Mandelbrot/src/fractals/MainWindow.java b/Week11 Mandelbrot/src/fractals/MainWindow.java index 7a7a94f..b6276af 100644 --- a/Week11 Mandelbrot/src/fractals/MainWindow.java +++ b/Week11 Mandelbrot/src/fractals/MainWindow.java @@ -2,10 +2,10 @@ package fractals; -import com.camilstaps.mandelbrot.MandelbrotController; +import com.camilstaps.mandelbrot.MandelbrotView; import com.camilstaps.mandelbrot.ZoomFrame; import java.awt.BorderLayout; -import java.awt.FlowLayout; +import java.awt.GridLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -26,20 +26,25 @@ import javax.swing.JTextField; * creates a window to which a GridView panel is added * */ -public class MainWindow implements MandelbrotController.MandelbrotProvider, ZoomFrame.MandelbrotTextFields { +public class MainWindow implements MandelbrotView.MandelbrotProvider, ZoomFrame.MandelbrotTextFields { // the size of the window public static final int WIDTH = 650, HEIGHT = 650; + private final String INITIAL_CENTER_X = "0", + INITIAL_CENTER_Y = "0", + INITIAL_SCALE = "100", + INITIAL_REPETITIONS = "100"; + // The grip panel private final GridView grid; - private final JTextField field_centerX = new JTextField("0", 6); - private final JTextField field_centerY = new JTextField("0", 6); - private final JTextField field_scale = new JTextField("100", 6); - private final JTextField field_repetitions = new JTextField("100", 6); + 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 MandelbrotController mandelbrotController; + private final MandelbrotView mandelbrotController; public MainWindow () { ZoomFrame mainFrame = new ZoomFrame ("Mandelbrot"); @@ -55,13 +60,15 @@ public class MainWindow implements MandelbrotController.MandelbrotProvider, Zoom grid = new GridView(WIDTH - insets.left - insets.right, HEIGHT - insets.top - insets.bottom); mainFrame.add(grid, BorderLayout.CENTER); - mandelbrotController = new MandelbrotController(this, grid); + mandelbrotController = new MandelbrotView(this, grid); mandelbrotController.redraw(); - mainFrame.setMandelbrotController(mandelbrotController); + mainFrame.setMandelbrotView(mandelbrotController); mainFrame.setMandelbrotTextFields(this); - JPanel panel = new JPanel(new FlowLayout()); + JPanel left = new JPanel(new BorderLayout()); + + JPanel panel = new JPanel(new GridLayout(10,1)); button_redraw.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { @@ -72,15 +79,30 @@ public class MainWindow implements MandelbrotController.MandelbrotProvider, Zoom }); panel.add(new JLabel("Center x:")); panel.add(field_centerX); - panel.add(new JLabel("y:")); + panel.add(new JLabel("Center y:")); panel.add(field_centerY); panel.add(new JLabel("Scale:")); panel.add(field_scale); panel.add(new JLabel("Repetitions:")); panel.add(field_repetitions); panel.add(button_redraw); + JButton button_reset = new JButton("Reset"); + button_reset.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent ae) { + field_centerX.setText(INITIAL_CENTER_X); + field_centerY.setText(INITIAL_CENTER_Y); + field_scale.setText(INITIAL_SCALE); + field_repetitions.setText(INITIAL_REPETITIONS); + mandelbrotController.redraw(); + } + }); + panel.add(button_reset); + + left.add(panel, BorderLayout.NORTH); + left.add(new JPanel(), BorderLayout.CENTER); - mainFrame.add(panel, BorderLayout.PAGE_END); + mainFrame.add(left, BorderLayout.EAST); mainFrame.pack(); } @@ -115,22 +137,22 @@ public class MainWindow implements MandelbrotController.MandelbrotProvider, Zoom @Override public void setCenterX(double x) { - field_centerX.setText(Double.toString(x)); + field_centerX.setText(String.format("%.5f", x)); } @Override public void setCenterY(double y) { - field_centerY.setText(Double.toString(y)); + field_centerY.setText(String.format("%.5f", y)); } @Override public void setScale(double scale) { - field_scale.setText(Double.toString(scale)); + field_scale.setText(String.format("%.5f", scale)); } @Override - public void setRepetitions(double repetitions) { - field_repetitions.setText(Double.toString(repetitions)); + public void setRepetitions(int repetitions) { + field_repetitions.setText(Integer.toString(repetitions)); } } -- cgit v1.2.3