在 java 中使用 bufferedImage 创建图块

creating tiles using bufferedImage in java

public static BufferedImage split(BufferedImage img) {
   BufferedImage pic = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
   Graphics g = pic.getGraphics();

    int width = 2000/2;
    int height = 2000/2;
    int imageW = pic.getWidth();
    int imageH = pic.getHeight();
                // Tile the image to fill our area.
    for (int x = 0; x < width; x += imageW) {
        for (int y = 0; y < height; y += imageH) {
            g.drawImage(pic, x, y, null);
        }
    }  
    return pic ;  
}  

代码的要点是创建一个 2x2 的图像图块(相同的图像在 2x2 网格中以较小的尺寸重现)。我想更新图片,以便可以将其打印到 jpanel 上。我得到的只是黑色图像。有人能告诉我代码有什么问题吗?或者告诉我如何创建更好的代码。

I want to make four smaller images of the original and place it in a grid of 2x2 that is the same size as the original image

类似...

public static BufferedImage split(BufferedImage img) {
    BufferedImage pic = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics g = pic.getGraphics();

    int width = pic.getWidth() / 4;
    int height = pic.getHeight() / 4;

    Image scaled = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);

    // Tile the image to fill our area.
    for (int x = 0; x < pic.getWidth(); x += width) {
        for (int y = 0; y < pic.getHeight(); y += height) {
            g.drawImage(scaled, x, y, null);
        }
    }
    g.dispose();
    return pic;
}

您可能还想查看 Java: maintaining aspect ratio of JPanel background image and Quality of Image after resize very low -- Java 以了解有关如何改进缩放算法的更多详细信息