diff options
| author | Camil Staps | 2015-04-28 22:39:41 +0200 | 
|---|---|---|
| committer | Camil Staps | 2015-04-28 22:39:41 +0200 | 
| commit | 2b77f0a7cd5c1a5591b943970900788472db28ac (patch) | |
| tree | eaeb10464ff977b3a5d05cc3615279b102e0fa10 | |
| parent | Project week 11 (diff) | |
Working mandelbrot function
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | Week11 Mandelbrot/nbproject/project.properties | 2 | ||||
| -rw-r--r-- | Week11 Mandelbrot/src/fractals/GridFiller.java | 2 | ||||
| -rw-r--r-- | Week11 Mandelbrot/src/fractals/MainWindow.java | 19 | ||||
| -rw-r--r-- | Week11 Mandelbrot/src/fractals/Mandelbrot.java | 57 | 
5 files changed, 72 insertions, 11 deletions
| @@ -1,3 +1,4 @@  /Week10 Weather GUI/nbproject/private/  /Week10 Weather GUI/build/ -/Week11 Mandelbrot/nbproject/private/
\ No newline at end of file +/Week11 Mandelbrot/nbproject/private/ +/Week11 Mandelbrot/build/
\ No newline at end of file diff --git a/Week11 Mandelbrot/nbproject/project.properties b/Week11 Mandelbrot/nbproject/project.properties index 0c027f6..c0f18c5 100644 --- a/Week11 Mandelbrot/nbproject/project.properties +++ b/Week11 Mandelbrot/nbproject/project.properties @@ -54,7 +54,7 @@ javadoc.splitindex=true  javadoc.use=true  javadoc.version=false  javadoc.windowtitle= -main.class= +main.class=fractals.Mandelbrot  manifest.file=manifest.mf  meta.inf.dir=${src.dir}/META-INF  mkdist.disabled=false diff --git a/Week11 Mandelbrot/src/fractals/GridFiller.java b/Week11 Mandelbrot/src/fractals/GridFiller.java index 1fbb676..73b422e 100644 --- a/Week11 Mandelbrot/src/fractals/GridFiller.java +++ b/Week11 Mandelbrot/src/fractals/GridFiller.java @@ -20,7 +20,7 @@ public class GridFiller {       * @param grid to be filled
       */
      public GridFiller (Grid grid) {
 -    	colorTable = new ColorTable (20); // some random value, needs to be adjusted
 +    	colorTable = new ColorTable (30); // some random value, needs to be adjusted
          this.grid = grid;
      }
 diff --git a/Week11 Mandelbrot/src/fractals/MainWindow.java b/Week11 Mandelbrot/src/fractals/MainWindow.java index 5c56b7f..7e72b30 100644 --- a/Week11 Mandelbrot/src/fractals/MainWindow.java +++ b/Week11 Mandelbrot/src/fractals/MainWindow.java @@ -2,9 +2,13 @@ package fractals; +import java.awt.BorderLayout;
 +import java.awt.FlowLayout;
  import java.awt.Insets;
 +import javax.swing.JButton;
  import javax.swing.JFrame;
 +import javax.swing.JPanel;
  /**
   * 
 @@ -18,7 +22,7 @@ import javax.swing.JFrame;   */
  public class MainWindow  {
      // the size of the window
 -    public static final int WIDTH = 650, HEIGHT = 650;
 +    public static final int WIDTH = 500, HEIGHT = 500;
      // The grip panel
      private GridView grid;
 @@ -26,16 +30,21 @@ public class MainWindow  {      public MainWindow () {
      	JFrame mainFrame = new JFrame ("Mandelbrot");
 -    	mainFrame.setSize (WIDTH, HEIGHT);                
 +        mainFrame.setLayout(new BorderLayout());
 +    	mainFrame.setSize (WIDTH, HEIGHT);
      	mainFrame.setLocationRelativeTo(null);
      	mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      	mainFrame.setResizable(false);
      	mainFrame.setVisible(true);
 -        Insets insets = mainFrame.getInsets ();
 -        grid = new GridView (WIDTH - insets.left - insets.right, HEIGHT - insets.top - insets.bottom);
 +        Insets insets = mainFrame.getInsets();
 +        grid = new GridView(WIDTH - insets.left - insets.right, HEIGHT - insets.top - insets.bottom);
 +        mainFrame.add(grid, BorderLayout.CENTER);
 +        
 +        JPanel panel = new JPanel(new FlowLayout());
 +        panel.add(new JButton("test"));
 +        mainFrame.add(panel, BorderLayout.PAGE_END);
 -        mainFrame.add(grid);
          mainFrame.pack();
      }
 diff --git a/Week11 Mandelbrot/src/fractals/Mandelbrot.java b/Week11 Mandelbrot/src/fractals/Mandelbrot.java index fa68372..74b522e 100644 --- a/Week11 Mandelbrot/src/fractals/Mandelbrot.java +++ b/Week11 Mandelbrot/src/fractals/Mandelbrot.java @@ -5,12 +5,63 @@ package fractals;   * @author Sjaak Smetsers
   * @version 1.0, 14-03-2013
   */
 -public class Mandelbrot 
 -{
 -    public static void main(String args[]) {    	       
 +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) {
 +        double x_n = x, y_n = y;
 +        int n = 0;
 +        
 +        while (x_n * x_n + y_n * y_n <= 4 && n <= MAX_MANDEL_NUMBER) {
 +            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;
 +            n++;
 +        }
 +        
 +        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 | 
