在Java中为像素网格设置动画

在Java中为像素网格设置动画,java,swing,animation,jframe,jpanel,Java,Swing,Animation,Jframe,Jpanel,我是一个noob程序员,我正在研究一个包含1000 x 1000个布尔值的2D网格,它根据指令模式而变化。“在x指令之后,网格中有多少值是真的?”诸如此类的事情 我想对其进行一点旋转,并将值渲染为像素网格,如果相应的值为假,则为黑色,如果值为真,则为白色,并且随着指令的处理实时显示动画,但我非常迷茫——我从未涉足Java中的2D图形。我已经通读了,这很有帮助,但我做事情的方式与它的演示完全不同,我仍然感到迷茫 我最直接的问题是,我甚至无法使用BufferedImage初始化1000 x 1000

我是一个noob程序员,我正在研究一个包含1000 x 1000个布尔值的2D网格,它根据指令模式而变化。“在
x
指令之后,网格中有多少值是真的?”诸如此类的事情

我想对其进行一点旋转,并将值渲染为像素网格,如果相应的值为假,则为黑色,如果值为真,则为白色,并且随着指令的处理实时显示动画,但我非常迷茫——我从未涉足Java中的2D图形。我已经通读了,这很有帮助,但我做事情的方式与它的演示完全不同,我仍然感到迷茫

我最直接的问题是,我甚至无法使用
BufferedImage
初始化1000 x 1000黑色像素的网格。运行我的代码会产生一个非常小的窗口,其中没有任何内容(灰色)。谁能告诉我我做错了什么,并建议如何继续?我的代码如下:

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

}
导入java.awt.image.buffereImage;
导入java.awt.Color;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类像素网格扩展JPanel{
专用缓冲图像网格;
//初始化二进制(黑色或白色)像素的网格
公共像素栅格(整数宽度、整数高度){
网格=新的BuffereImage(宽度、高度、BuffereImage.TYPE_BYTE_BINARY);
}
/**
*用给定的颜色填充一个区域
*@param color 0表示黑色;1表示白色
*@param x1起始x坐标
*@param y1起始y坐标
*@param x2结束x坐标
*@param y2结束y坐标
*/
公共无效切换块(整数颜色、整数x1、整数y1、整数x2、整数y2){
如果(颜色==0){
color=color.BLACK.getRGB();
}
否则{
color=color.WHITE.getRGB();
}

对于(int x=x1;x您的代码创建了一个
buffereImage
,但随后不处理它(以图形方式)。有几个选项:

选项1:覆盖
PixelGrid
类的
paintComponent
,并将图像绘制到
JPanel

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawImage(grid, 0, 0, this);
}
选项2:使用
JLabel
ImageIcon

JLabel label = new JLabel(new ImageIcon(grid));
add(label);
在这两种情况下,每次更改BuffereImage时,都必须调用组件上的
repaint

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

您的代码创建了一个
buffereImage
,但随后不使用它(以图形方式)。有几个选项:

选项1:覆盖
PixelGrid
类的
paintComponent
,并将图像绘制到
JPanel

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawImage(grid, 0, 0, this);
}
选项2:使用
JLabel
ImageIcon

JLabel label = new JLabel(new ImageIcon(grid));
add(label);
在这两种情况下,每次更改BuffereImage时,都必须调用组件上的
repaint

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

我会尝试重写
paintComponent(Graphics g)
方法(确保调用
super.paintComponent(g)
作为方法的第一行)然后使用
Graphics
对象绘制变量。
我已经阅读了Oracle的教程
-您的代码与教程中的代码完全不同。1)您不重写paintComponent()方法,2)您不重写getPreferredSize()方法。
但我做事情的方式与它的演示完全不同
-绘画就是绘画。我建议你从教程中的演示代码开始,然后进行更改。基本上,你所需要的是一个2D数组,它跟踪每个像素的状态。然后在paintComponent方法中,你可以绘制每个像素。所以从wo开始我会尝试重写
paintComponent(Graphics g)
方法(确保调用
super.paintComponent(g)
作为方法的第一行)然后使用
Graphics
对象绘制变量。
我已经阅读了Oracle的教程
-您的代码与教程中的代码完全不同。1)您不重写paintComponent()方法,2)您不重写getPreferredSize()方法。
但我做事情的方式与它的演示完全不同
-绘画就是绘画。我建议你从教程中的演示代码开始,然后进行更改。基本上,你所需要的是一个2D数组,它跟踪每个像素的状态。然后在paintComponent方法中,你可以绘制每个像素。所以从wo开始重新编写代码并进行更改,而不是从头开始。