翻转图像

发布于 2024-10-31 05:36:21 字数 1174 浏览 3 评论 0原文

我正在创建一个需要翻转图像的瓷砖游戏。使用我的代码,它会产生此错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

当爱已成负担 2024-11-07 05:36:21

如果 image.getWidth(null)image.getHeight(null) 尚不知道,它们可能会返回 -1。图像处理可以同时完成,并且当您尝试调用这些方法时它可能仍在进行(即使在本地它也会执行此 IIRC)。下面是我之前使用过的一段代码,用于在尝试访问图像的宽度和高度之前等待图像加载:

private void waitForImage(Image image) {
    Container container = new Container();
    MediaTracker tracker = new MediaTracker(container);
    tracker.addImage(image, 0);
    try {
        tracker.waitForID(0, 1000);
    } catch (Exception e) {
    }
}

我刚刚尝试了本地测试,对于一些小图像,等待时间通常为 1 毫秒。这实际上并不是任何额外的时间,它只是强制您的代码等待最后一个阶段完成才能获取宽度和高度。

It's possible for image.getWidth(null) and image.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:

private void waitForImage(Image image) {
    Container container = new Container();
    MediaTracker tracker = new MediaTracker(container);
    tracker.addImage(image, 0);
    try {
        tracker.waitForID(0, 1000);
    } catch (Exception e) {
    }
}

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.

黯然 2024-11-07 05:36:21

翻转图像最简单的方法是负缩放。示例:

g2.drawImage(image, x, y, -width, height, null);

这将垂直翻转它。这将水平翻转它:

g2.drawImage(image, x, y, width, -height, null);

The easiest way to flip the image is by negative scaling it. Example:

g2.drawImage(image, x, y, -width, height, null);

That will flip it vertically. This will flip it horizontally:

g2.drawImage(image, x, y, width, -height, null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文