From a3c4d590c471ddf79b204c7039a597b4be264ad9 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Tue, 28 Apr 2015 13:36:51 +0200 Subject: Initial commit w11 --- Week11 Mandelbrot/Assignment (in Dutch).pdf | Bin 0 -> 616505 bytes Week11 Mandelbrot/src/fractals/ColorTable.java | 69 +++++++++++++++++++ Week11 Mandelbrot/src/fractals/Grid.java | 30 +++++++++ Week11 Mandelbrot/src/fractals/GridFiller.java | 40 +++++++++++ Week11 Mandelbrot/src/fractals/GridView.java | 89 +++++++++++++++++++++++++ Week11 Mandelbrot/src/fractals/MainWindow.java | 50 ++++++++++++++ Week11 Mandelbrot/src/fractals/Mandelbrot.java | 16 +++++ 7 files changed, 294 insertions(+) create mode 100644 Week11 Mandelbrot/Assignment (in Dutch).pdf create mode 100644 Week11 Mandelbrot/src/fractals/ColorTable.java create mode 100644 Week11 Mandelbrot/src/fractals/Grid.java create mode 100644 Week11 Mandelbrot/src/fractals/GridFiller.java create mode 100644 Week11 Mandelbrot/src/fractals/GridView.java create mode 100644 Week11 Mandelbrot/src/fractals/MainWindow.java create mode 100644 Week11 Mandelbrot/src/fractals/Mandelbrot.java (limited to 'Week11 Mandelbrot') diff --git a/Week11 Mandelbrot/Assignment (in Dutch).pdf b/Week11 Mandelbrot/Assignment (in Dutch).pdf new file mode 100644 index 0000000..33df886 Binary files /dev/null and b/Week11 Mandelbrot/Assignment (in Dutch).pdf differ 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 -- cgit v1.2.3