【问题标题】:creating tiles using bufferedImage in java在java中使用bufferedImage创建图块
【发布时间】:2015-11-26 23:08:01
【问题描述】:
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 上。我得到的只是黑色图像。有人可以告诉我代码有什么问题吗?或者告诉我如何创建更好的代码。

【问题讨论】:

  • g.drawImage(pic, x, y, null);?你在给自己画pic
  • 我不确定我是否理解您要执行的操作。您拍摄图像,创建另一个相同大小的图像,您(想要)将第一个图像绘制到第二个图像并给定间隔(1:1)...??第二张图像不应该更大/更小还是原始图像被缩放?
  • 我想制作四个较小的原始图像并将其放置在与原始图像大小相同的 2x2 网格中。

标签: java hexagonal-tiles


【解决方案1】:

我想制作四个较小的原始图像并将其放置在与原始图像相同大小的 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,了解有关如何改进缩放算法的更多详细信息

【讨论】:

  • 是的。你能解释一下这段代码发生了什么吗?请。我真的很想知道。
  • 这真的很基础。它会创建一个与img 大小相同的新BufferedImage。然后它会创建一个原始的缩放实例,将其减少 0.25%。然后它将缩放的实例绘制到pic
  • 图像缩放 = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
  • 好的。非常感谢您的帮助。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-10
  • 2013-08-08
  • 1970-01-01
相关资源
最近更新 更多