在java中使用bufferedImage创建tile

时间:2015-11-26 23:08:02

标签: java hexagonal-tiles

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网格中以较小的尺寸再现)。我想更新pic,所以我可以将它打印到jpanel上。我得到的只是黑色图像。谁能告诉我代码有什么问题。或告诉我如何创建更好的代码。

1 个答案:

答案 0 :(得分:0)

  

我想制作原件的四张较小的图像,并将其放置在与原始图像大小相同的2x2网格中

像...一样的东西。

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 imageQuality of Image after resize very low -- Java,了解有关如何改进缩放算法的详细信息