我正在尝试使用下面的代码缩放图像(来自 Sprite 表)。
Point pos = new Point(100, 100); //this shows where to display the image
Rectangle a = getAnimatedPos(); //just gets the coordinates of a specified image (which works correctly)
g.drawImage(img, pos.x, pos.y, pos.x+getWidth(), pos.y+getHeight(), a.x, a.y, a.x+a.width, a.y+a.height, null);
the function getWidth() returns the width of the frame. The formula is (imageWidth/rows).
the function getHeight() returns the height of the frame. The formula is (imageHeight/cols).
当我缩放图像(通过更改宽度或高度)时,它开始显示其他帧?如果我将图像的宽度或高度保留为默认大小,它就可以工作。我认为通过定义源坐标和目标坐标不会这样做。
澄清一下,图像是在尺寸为 640x480 的内部框架上绘制/渲染的。另外,当我指的是框架时,我指的是 Sprite 表,该表中的每个图像都是一个框架。当然是用于动画。
另一件事需要澄清,图像没有绘制在组件之上。只是一个内部框架。
我应该如何解决这个问题?
屏幕截图
当一切都是默认的时候,它看起来像这样:http://wisemindstudios.com/good.png
当我更改某些内容时:http://wisemindstudios.com/bad.png
非常感谢!
最佳答案
我不知道这对你是否可行,但是。
基本上,我提取要渲染的帧,然后绘制它。
这可以让我可怜的小大脑免于因所有参数而崩溃
Rectangle frameBounds = //... calculate the frame bounds to be extracted from the sprite sheet
BufferedImage cell = spriteSheet.getSubimage(frameBounds.x, frameBounds.y, frameBounds.width, frameBounds.height);
Rectangle renderBounds = //... calculate the location and size of the sprite to be rendered
g2d.drawImage(cell, renderBounds.x, renderBounds.y, renderBounds.width, renderBounds.height, this);
在我的测试中,这导致了非常糟糕的结果。您确实应该考虑更好的缩放算法;)
已更新
我使用了这个 Sprite 表
产生了这个输出
现在我添加了帧编号,将帧边界添加到渲染中以帮助其脱颖而出。
使用此方法可能会从上一帧/下一帧中失败的唯一原因是样式表错误(即帧大小不统一)或当前帧的计算错误。
关于Java - 图形 DrawImage 缩放问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12101520/