Sacrificial Anode
Sacrificial Anode

Reputation: 3

Animating a Grid of Pixels in Java

I'm a noob programmer, and I'm working on a little project that involves a 2D grid of 1000 x 1000 boolean values that change based on an instruction pattern. "After x instructions, how many values in the grid are true?" That kind of thing.

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 Oracle's tutorial, which helped, but the way I'm doing things is sufficiently different from its demo that I still feel lost.

My most immediate problem is that I can't even seem to initialize a grid of 1000 x 1000 black pixels using a BufferedImage. 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:

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 <= x2; x++) {
            for (int y = y1; y <= 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);
    }

}

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.

Upvotes: 0

Views: 1755

Answers (1)

copeg
copeg

Reputation: 8348

Your code creates a BufferedImage, but then doesn't do anything with it (graphically). A few options:

Option 1: Override paintComponent of the PixelGrid class and draw the image to the JPanel

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawImage(grid, 0, 0, this);
}

Option 2: Use a JLabel and ImageIcon

JLabel label = new JLabel(new ImageIcon(grid));
add(label);

In either case, you will have to call repaint on the Component every time the BufferedImage is changed

//some code
    grid.setRGB(x, y, color);
//some more code
repaint();//or label.repaint() if Option 2

Upvotes: 3

Related Questions