翻转图像
我正在创建一个需要翻转图像的瓷砖游戏。使用我的代码,它会产生此错误:
Exception in thread "main" java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0
从这一行:
Image newImage = gc.createCompatibleImage(
image.getWidth(null),
image.getHeight(null),
Transparency.BITMASK);
这是我的编码:
public Image getMirrorImage(Image image) {
return getScaledImage(image, -1, 1);
}
private Image getScaledImage(Image image, float x, float y) {
// set up the transform
AffineTransform transform = new AffineTransform();
transform.scale(x, y);
transform.translate(
(x-1) * image.getWidth(null) / 2,
(y-1) * image.getHeight(null) / 2);
// create a transparent (not translucent) image
Image newImage = gc.createCompatibleImage(
image.getWidth(null),
image.getHeight(null),
Transparency.BITMASK);
// draw the transformed image
Graphics2D g = (Graphics2D)newImage.getGraphics();
g.drawImage(image, transform, null);
g.dispose();
return newImage;
}
任何人都可以向我解释我可能需要做哪些不同的事情或者我如何才能使其工作?非常感谢
I am creating a tile game where I need to flip images. With the code I have it produces this error:
Exception in thread "main" java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0
from this line:
Image newImage = gc.createCompatibleImage(
image.getWidth(null),
image.getHeight(null),
Transparency.BITMASK);
here is the coding i have:
public Image getMirrorImage(Image image) {
return getScaledImage(image, -1, 1);
}
private Image getScaledImage(Image image, float x, float y) {
// set up the transform
AffineTransform transform = new AffineTransform();
transform.scale(x, y);
transform.translate(
(x-1) * image.getWidth(null) / 2,
(y-1) * image.getHeight(null) / 2);
// create a transparent (not translucent) image
Image newImage = gc.createCompatibleImage(
image.getWidth(null),
image.getHeight(null),
Transparency.BITMASK);
// draw the transformed image
Graphics2D g = (Graphics2D)newImage.getGraphics();
g.drawImage(image, transform, null);
g.dispose();
return newImage;
}
Can anyone explain to me what i might need to do different or how i can make it work? Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
image.getWidth(null)
和image.getHeight(null)
尚不知道,它们可能会返回 -1。图像处理可以同时完成,并且当您尝试调用这些方法时它可能仍在进行(即使在本地它也会执行此 IIRC)。下面是我之前使用过的一段代码,用于在尝试访问图像的宽度和高度之前等待图像加载:我刚刚尝试了本地测试,对于一些小图像,等待时间通常为 1 毫秒。这实际上并不是任何额外的时间,它只是强制您的代码等待最后一个阶段完成才能获取宽度和高度。
It's possible for
image.getWidth(null)
andimage.getHeight(null)
to return -1 if they're not known yet. Image processing can be done concurrently and it might still be going when you try to call those methods (even locally it'll do this IIRC). Here's a snippet of code I've used before to wait for an image to be loaded before trying to access its width and height:I just tried a local test and the wait times were generally 1ms for some small images. It's not really any additional time, it just forces your code to wait for the last stage to complete to get the width and height.
翻转图像最简单的方法是负缩放。示例:
这将垂直翻转它。这将水平翻转它:
The easiest way to flip the image is by negative scaling it. Example:
That will flip it vertically. This will flip it horizontally: