Animating a Grid of Pixels in Java
<p>I'm a noob programmer, and I'm working on <a href="http://adventofcode.com/day/6" rel="nofollow">a little project</a> that involves a 2D grid of 1000 x 1000 boolean values that change based on an instruction pattern. "After <code>x</code> instructions, how many values in the grid are true?" That kind of thing.</p>

<p>I want to put a little spin on it and render the values as a grid of pixels which are black if their corresponding values are false and white if they're true and that animates in real time as instructions are processed, but I'm pretty lost -- I've never dabbled with 2D graphics in Java. I've read through <a href="https://docs.oracle.com/javase/tutorial/uiswing/painting/step1.html" rel="nofollow">Oracle's tutorial</a>, which helped, but the way I'm doing things is sufficiently different from its demo that I still feel lost.</p>

<p>My most immediate problem is that I can't even seem to initialize a grid of 1000 x 1000 black pixels using a <code>BufferedImage</code>. Running my code yields a very tiny window with nothing (grayness) in it. Can anyone tell me what I'm doing wrong and suggest how to proceed? My code follows:</p>

<pre><code>import java.awt.image.BufferedImage;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PixelGrid extends JPanel {

    private BufferedImage grid;

    // Ctor initializing a grid of binary (black or white) pixels
    public PixelGrid(int width, int height) {
        grid = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
    }

    /**
     * Fill an area with a given color
     * @param color 0 for black; 1 for white
     * @param x1 Starting x coordinate
     * @param y1 Starting y coordinate
     * @param x2 Ending x coordinate
     * @param y2 Ending y coordinate
     */
    public void toggleBlock(int color, int x1, int y1, int x2, int y2) {
        if (color == 0) {
            color = Color.BLACK.getRGB();
        }
        else {
            color = Color.WHITE.getRGB();
        }
        for (int x = x1; x &lt;= x2; x++) {
            for (int y = y1; y &lt;= y2; y++) {
                grid.setRGB(x, y, color);
            }
        }
    }

    // Clear the grid (set all pixels to black)
    public void clear() {
        toggleBlock(0, 0, 0, grid.getWidth() - 1, grid.getHeight() - 1);
    }

    public static void main(String[] args) {
        int width = 1000;
        int height = 1000;
        PixelGrid aGrid = new PixelGrid(width, height);
        JFrame window = new JFrame("A Wild Pixel Grid Appears!");

        window.add(aGrid); // Incorporates the pixel grid into the window
        window.pack(); // Resizes the window to fit its content
        window.setVisible(true); // Makes the window visible
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}
</code></pre>

<p>Note that it doesn't yet deal at all with an actual 2D array of booleans or instruction processing; I'm pretty sure I can handle that on my own, but, for now, I'm just trying to understand how to set up the graphical component.</p>