diff options
author | Camil Staps | 2015-04-28 13:36:51 +0200 |
---|---|---|
committer | Camil Staps | 2015-04-28 13:36:51 +0200 |
commit | a3c4d590c471ddf79b204c7039a597b4be264ad9 (patch) | |
tree | 800c93ba842b1e8ab64fc28b33eb5cdd8986d165 /Week11 Mandelbrot/src/fractals | |
parent | Added assignment week 10 (diff) |
Initial commit w11
Diffstat (limited to 'Week11 Mandelbrot/src/fractals')
-rw-r--r-- | Week11 Mandelbrot/src/fractals/ColorTable.java | 69 | ||||
-rw-r--r-- | Week11 Mandelbrot/src/fractals/Grid.java | 30 | ||||
-rw-r--r-- | Week11 Mandelbrot/src/fractals/GridFiller.java | 40 | ||||
-rw-r--r-- | Week11 Mandelbrot/src/fractals/GridView.java | 89 | ||||
-rw-r--r-- | Week11 Mandelbrot/src/fractals/MainWindow.java | 50 | ||||
-rw-r--r-- | Week11 Mandelbrot/src/fractals/Mandelbrot.java | 16 |
6 files changed, 294 insertions, 0 deletions
diff --git a/Week11 Mandelbrot/src/fractals/ColorTable.java b/Week11 Mandelbrot/src/fractals/ColorTable.java new file mode 100644 index 0000000..064bd15 --- /dev/null +++ b/Week11 Mandelbrot/src/fractals/ColorTable.java @@ -0,0 +1,69 @@ +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/Grid.java b/Week11 Mandelbrot/src/fractals/Grid.java new file mode 100644 index 0000000..be6048b --- /dev/null +++ b/Week11 Mandelbrot/src/fractals/Grid.java @@ -0,0 +1,30 @@ +package fractals;
+
+/**
+ *
+ * @author Sjaak Smetsers
+ * @version 1.0, 13-03-2013
+ */
+
+/**
+ * An interface providing a context for drawing
+ * pictures pixel-wise
+ */
+public interface Grid {
+ /**
+ * setPixel puts a pixel on the specified location
+ * @param x, y coordinates of the location
+ * @param rgb the color specified as an rgb value
+ */
+ void setPixel (int x, int y, int [] rgb);
+
+ /**
+ * @return the width of the grid
+ */
+ int getWidth ();
+ /**
+ * @return the height of the grid
+ */
+ int getHeight ();
+
+}
diff --git a/Week11 Mandelbrot/src/fractals/GridFiller.java b/Week11 Mandelbrot/src/fractals/GridFiller.java new file mode 100644 index 0000000..1fbb676 --- /dev/null +++ b/Week11 Mandelbrot/src/fractals/GridFiller.java @@ -0,0 +1,40 @@ +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 (20); // 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 new file mode 100644 index 0000000..4a51114 --- /dev/null +++ b/Week11 Mandelbrot/src/fractals/GridView.java @@ -0,0 +1,89 @@ +package fractals;
+
+import java.awt.Dimension;
+import java.awt.Graphics;
+import java.awt.image.BufferedImage;
+import java.awt.image.WritableRaster;
+
+import javax.swing.JComponent;
+import javax.swing.JPanel;
+
+/**
+ *
+ * @author Sjaak Smetsers
+ * @version 1.1 --- 14-03-2013
+ */
+/**
+ * An implementation of the Grid interface
+ *
+ * @author Sjaak
+ */
+public class GridView extends JPanel implements Grid {
+
+ private BufferedImage gridImage;
+ private WritableRaster gridRaster;
+
+ private int gridWidth, gridHeight;
+
+ private static final int REPAINT_NUMBER = 2000;
+
+ private int nrOfPixelsSet;
+
+ /**
+ * creates a drawing panel with specified size
+ *
+ * @param width of the panel
+ * @param height of the panel
+ */
+ public GridView(int width, int height) {
+ gridWidth = width;
+ gridHeight = height;
+ gridImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+ gridRaster = gridImage.getRaster();
+ setPreferredSize(new Dimension(width, height));
+ }
+
+ @Override
+ public void paintComponent(Graphics g) {
+ super.paintComponent(g);
+ g.drawImage(gridImage, 0, 0, null);
+ }
+
+ /**
+ * draws the specified pixel with the indicated color
+ *
+ * @param x coordinate of the location
+ * @param y coordinate of the location
+ * @param rgb array with length 3 containing the rgb values
+ */
+ @Override
+ public void setPixel(int x, int y, int[] rgb) {
+ gridRaster.setPixel(x, y, rgb);
+ nrOfPixelsSet++;
+ if (nrOfPixelsSet > REPAINT_NUMBER) {
+ nrOfPixelsSet = 0;
+ repaint();
+ }
+ }
+
+ /**
+ * a getter for width
+ *
+ * @return width of the drawing panel
+ */
+ @Override
+ public int getWidth() {
+ return gridWidth;
+ }
+
+ /**
+ * a getter for height
+ *
+ * @return height of the drawing panel
+ */
+ @Override
+ public int getHeight() {
+ return gridHeight;
+ }
+
+}
diff --git a/Week11 Mandelbrot/src/fractals/MainWindow.java b/Week11 Mandelbrot/src/fractals/MainWindow.java new file mode 100644 index 0000000..5c56b7f --- /dev/null +++ b/Week11 Mandelbrot/src/fractals/MainWindow.java @@ -0,0 +1,50 @@ +package fractals;
+
+
+
+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 {
+ // the size of the window
+ public static final int WIDTH = 650, HEIGHT = 650;
+
+ // The grip panel
+ private GridView grid;
+
+ public MainWindow () {
+ JFrame mainFrame = new JFrame ("Mandelbrot");
+
+ 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);
+
+ mainFrame.add(grid);
+ mainFrame.pack();
+ }
+
+ /**
+ * A getter for the grid
+ * @return the grid
+ */
+ public Grid getGrid () {
+ return grid;
+ }
+
+}
diff --git a/Week11 Mandelbrot/src/fractals/Mandelbrot.java b/Week11 Mandelbrot/src/fractals/Mandelbrot.java new file mode 100644 index 0000000..fa68372 --- /dev/null +++ b/Week11 Mandelbrot/src/fractals/Mandelbrot.java @@ -0,0 +1,16 @@ +package fractals;
+
+/**
+ * An example of the use of our pixel drawing framework
+ * @author Sjaak Smetsers
+ * @version 1.0, 14-03-2013
+ */
+public class Mandelbrot
+{
+ public static void main(String args[]) {
+ MainWindow fractal_win = new MainWindow ();
+ GridFiller filler = new GridFiller (fractal_win.getGrid());
+ filler.fill();
+ }
+
+}
\ No newline at end of file |