aboutsummaryrefslogtreecommitdiff
path: root/Week11 Mandelbrot/src/fractals/GridView.java
blob: d85dc6ed97a612df00083ac8bc705a72eab27d9b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package fractals;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

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;
    }

}