我正在尝试使用graphics.drawImage()
将一张图像绘制到另一张图像上,但它只对某些图像准确工作,而其他图像则困惑。我的代码如下,我已确保 texture
在进入方法时是正确的图像,因此绝对不是它。有什么想法吗?
private BufferedImage currentSheet;
public void swapRegionWithTexture(Rectangle region, Image texture) {
Graphics sheetGraphics = currentSheet.createGraphics();
for (int ix = region.x; ix < region.x + region.width; ix++) {
for (int iy = region.y; iy < region.y + region.height; iy++) {
currentSheet.setRGB(ix, iy, 0x000000);
}
}
sheetGraphics.drawImage(texture, region.x, region.y, null);
sheetGraphics.dispose();
}
总体思路是:
- 抓取要绘制的图形。
- 清除将要绘制的图形部分。
- 在图形上的给定位置绘制图像。
- 处理图形。
最佳答案
您当前的原子步骤是:
- 创建新的缓冲图像
- 向缓冲图像写入对原始纹理的一些疯狂操作(看起来你正在反转它?)
- 将原始图像写入当前工作表。
如果你改变这个会怎么样
sheetGraphics.drawImage(texture, region.x, region.y, null);
到此
sheetGraphics.drawImage(bufferedTexture, region.x, region.y, null);
否则,您将花费时间反转图像并放入缓冲纹理,然后永远不会对该缓冲区执行任何操作...因此您很可能打算在某个地方使用该缓冲纹理。
关于Java 的 Graphics.drawImage 生成不准确的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11567861/