Java 在给定图像坐标的情况下,用图像的一部分填充矩形?

Java 在给定图像坐标的情况下,用图像的一部分填充矩形?,java,graphics2d,Java,Graphics2d,我正在创建一个有许多独立的小正方形的板,我想用图像的部分填充这些正方形,这样它们一起形成原始图像。我该怎么做?我应该上哪门课?顺便说一句,我正在用油漆组件画黑板 public void paintComponent(Graphics g) { super.paintComponent(g); int boxHeight = boxHeight(); int boxWidth = boxWidth(); for (int row = 0; row < game

我正在创建一个有许多独立的小正方形的板,我想用图像的部分填充这些正方形,这样它们一起形成原始图像。我该怎么做?我应该上哪门课?顺便说一句,我正在用油漆组件画黑板

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    int boxHeight = boxHeight();
    int boxWidth = boxWidth();
    for (int row = 0; row < game.getRows(); row++) {
        for (int col = 0; col < game.getCols(); col++) {
            g.setColor(Color.lightGray);
            g.drawRect(col * boxWidth, row * boxHeight, boxWidth, boxHeight);
//this is where I want to fill the rectangles
            if (game.isAlive(row, col)) {
                g.setColor(color);
                g.fillRect(col * boxWidth, row * boxHeight, boxWidth, boxHeight);
            } 
        }
    }
}
公共组件(图形g){
超级组件(g);
int-boxHeight=boxHeight();
int-boxWidth=boxWidth();
对于(int row=0;row
假设您有一个BuffereImage b:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    int boxHeight = boxHeight();
    int boxWidth = boxWidth();
    for (int row = 0; row < game.getRows(); row++) {
        for (int col = 0; col < game.getCols(); col++) {
            g.setColor(Color.lightGray);
            g.drawRect(col * boxWidth, row * boxHeight, boxWidth, boxHeight);
//this is where I want to fill the rectangles
            if (game.isAlive(row, col)) {
                g.setColor(color);
                g.drawImage(b.getSubimage(col * boxWidth, row * boxHeight, boxWidth, boxHeight), col * boxWidth, row * boxHeight, boxWidth, boxHeight, null);
            } 
        }
    }
在循环中

g.drawImage(b2.getSubimage(col * boxWidth, row * box, boxWidth, boxHeight), col * boxWidth, row * boxHeight, boxWidth, boxHeight, null);
你的意思是像或?诀窍是将图像(使用
BufferedImage#subImage
)分割成更小的图像,并将它们存储在类似
列表的东西中,这样可以更容易地显示和管理
g.drawImage(b2.getSubimage(col * boxWidth, row * box, boxWidth, boxHeight), col * boxWidth, row * boxHeight, boxWidth, boxHeight, null);