diff options
3 files changed, 108 insertions, 40 deletions
| diff --git a/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotController.java b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotController.java new file mode 100644 index 0000000..930d589 --- /dev/null +++ b/Week11 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotController.java @@ -0,0 +1,56 @@ +/* + * 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 void redraw() { +        double centerX = mandelbrotProvider.getCenterX(); +        double centerY = mandelbrotProvider.getCenterY(); +        double scale = mandelbrotProvider.getScale(); +        int repetitions = mandelbrotProvider.getRepetitions(); +         +        int grid_w = grid.getWidth(), grid_h = grid.getHeight(); +         +        double min_x = centerX - grid_w / scale / 2, +                max_x = centerX + grid_w / scale / 2, +                min_y = centerY - grid_h / scale / 2, +                max_y = centerX + grid_h / scale / 2; +         +        for (float i = 0; i < grid_w; i++) { +            for (float j = 0; j < grid_h; j++) { +                int grayscale = Mandelbrot.mandelNumber( +                        min_x + (i / grid_w) * (max_x - min_x),  +                        min_y + (j / grid_h) * (max_y - min_y), +                        repetitions +                ) * 255 / repetitions; +                int[] color = {grayscale, grayscale, grayscale}; +                grid.setPixel((int) i, (int) j, color); +            }                +        } +    } +     +    public interface MandelbrotProvider { +        public double getCenterX(); +        public double getCenterY(); +        public double getScale(); +        public int getRepetitions(); +    } +     +} diff --git a/Week11 Mandelbrot/src/fractals/MainWindow.java b/Week11 Mandelbrot/src/fractals/MainWindow.java index 7e72b30..44e1791 100644 --- a/Week11 Mandelbrot/src/fractals/MainWindow.java +++ b/Week11 Mandelbrot/src/fractals/MainWindow.java @@ -2,13 +2,17 @@ package fractals; +import com.camilstaps.mandelbrot.MandelbrotController;
  import java.awt.BorderLayout;
  import java.awt.FlowLayout;
  import java.awt.Insets;
 +import java.awt.event.ActionEvent;
 +import java.awt.event.ActionListener;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
 +import javax.swing.JTextField;
  /**
   * 
 @@ -20,12 +24,20 @@ import javax.swing.JPanel;   * creates a window to which a GridView panel is added
   * 
   */
 -public class MainWindow  {
 +public class MainWindow implements MandelbrotController.MandelbrotProvider {
      // the size of the window
      public static final int WIDTH = 500, HEIGHT = 500;
      // The grip panel
 -    private GridView grid;
 +    private final GridView grid;
 +    
 +    private final JTextField field_centerX = new JTextField("0");
 +    private final JTextField field_centerY = new JTextField("0");
 +    private final JTextField field_scale = new JTextField("100");
 +    private final JTextField field_repetitions = new JTextField("100");
 +    private final JButton button_redraw = new JButton("Redraw");
 +    
 +    private final MandelbrotController mandelbrotController;
      public MainWindow () {
      	JFrame mainFrame = new JFrame ("Mandelbrot");
 @@ -41,8 +53,23 @@ public class MainWindow  {          grid = new GridView(WIDTH - insets.left - insets.right, HEIGHT - insets.top - insets.bottom);
          mainFrame.add(grid, BorderLayout.CENTER);
 +         mandelbrotController = new MandelbrotController(this, grid);
 +        
          JPanel panel = new JPanel(new FlowLayout());
 -        panel.add(new JButton("test"));
 +        button_redraw.addActionListener(new ActionListener() {
 +            @Override
 +            public void actionPerformed(ActionEvent ae) {
 +                if (ae.getActionCommand().equals("Redraw")) {
 +                    mandelbrotController.redraw();
 +                }
 +            }
 +        });
 +        panel.add(field_centerX);
 +        panel.add(field_centerY);
 +        panel.add(field_scale);
 +        panel.add(field_repetitions);
 +        panel.add(button_redraw);
 +        
          mainFrame.add(panel, BorderLayout.PAGE_END);
          mainFrame.pack();
 @@ -55,5 +82,25 @@ public class MainWindow  {      public Grid getGrid () {
      	return grid;
      }
 +
 +    @Override
 +    public double getCenterX() {
 +        return Double.parseDouble(field_centerX.getText());
 +    }
 +
 +    @Override
 +    public double getCenterY() {
 +        return Double.parseDouble(field_centerY.getText());
 +    }
 +
 +    @Override
 +    public double getScale() {
 +        return Double.parseDouble(field_scale.getText());
 +    }
 +
 +    @Override
 +    public int getRepetitions() {
 +        return Integer.parseInt(field_repetitions.getText());
 +    }
  }
 diff --git a/Week11 Mandelbrot/src/fractals/Mandelbrot.java b/Week11 Mandelbrot/src/fractals/Mandelbrot.java index 74b522e..3f2aaaa 100644 --- a/Week11 Mandelbrot/src/fractals/Mandelbrot.java +++ b/Week11 Mandelbrot/src/fractals/Mandelbrot.java @@ -7,25 +7,19 @@ package fractals;   */
  public class Mandelbrot {
 -    public final static int MAX_MANDEL_NUMBER = 100; // infinity will be this + 1
 -    
      public Mandelbrot() {
          MainWindow fractal_win = new MainWindow ();
 -        GridFiller filler = new GridFiller (fractal_win.getGrid());
 -        filler.fill();
 -        
 -        System.out.println(Integer.toString(mandelNumber(0,0)));
      }
      public static void main(String args[]) {    	       
          Mandelbrot mandelbrot = new Mandelbrot();
      }
 -    public static int mandelNumber(double x, double y) {
 +    public static int mandelNumber(double x, double y, int repetitions) {
          double x_n = x, y_n = y;
          int n = 0;
 -        while (x_n * x_n + y_n * y_n <= 4 && n <= MAX_MANDEL_NUMBER) {
 +        while (x_n * x_n + y_n * y_n <= 4 && n <= repetitions) {
              double new_x_n = x_n * x_n - y_n * y_n + x;
              y_n = 2 * x_n * y_n + y;
              x_n = new_x_n;
 @@ -34,34 +28,5 @@ public class Mandelbrot {          return n;
      }
 -    
 -    private class GridFiller {
 -        private Grid grid;              // the grid to be filled
 -        
 -        /**
 -         * The constructor
 -         * @param grid to be filled
 -         */
 -        public GridFiller (Grid grid) {
 -            this.grid = grid;
 -        }
 -        
 -        private final double MIN_X = -2.5, MAX_X = 2.5, MIN_Y = -2.5, MAX_Y = 2.5;
 -
 -        /**
 -         * fills the whole grid with some arbitrarily chosen color
 -         * 
 -         */
 -        public void fill () {
 -            int grid_w = grid.getWidth(), grid_h = grid.getHeight();
 -            for (float i = 0; i < grid_w; i++) {
 -                for (float j = 0; j < grid_h; j++) {
 -                    int grayscale = mandelNumber(MIN_X + (i / grid_w) * (MAX_X - MIN_X), MIN_Y + (j / grid_h) * (MAX_Y - MIN_Y)) * 256 / MAX_MANDEL_NUMBER;
 -                    int[] color = {grayscale, grayscale, grayscale};
 -                    grid.setPixel((int) i, (int) j, color);
 -                }               
 -            }
 -        }
 -    }
  }
\ No newline at end of file | 
