如何在java中使图像逆时针旋转?

发布于 2024-12-10 00:41:24 字数 1761 浏览 0 评论 0原文

我有一个图像,我希望它沿逆时针方向移动,这就是我的问题。我有一个代码,但似乎我的代码无法正常工作。你可以检查我的代码是否有错误。请帮助我解决这个问题...

这是我的代码:

public void move(long dt)
{

    double dt_s = dt / 1e9;
    double dx_m = speed * dt_s;
    double dy_m = speed * dt_s;

    double width = board.x1_world;
    double height = board.y1_world;

    double min_height = 0.0;
    double max_height = height;
    double min_width = 0.0;
    double max_width = width;

    x += dx_m;
    if (x >= max_width)
    {
        x = max_width;

        if (y >= min_height)
        {
            y += dy_m;
        }
    }
    if (y >= max_height)
    {
        y = max_height;
        if (x >= min_width)
        {
            dx_m *= -1;
        }
    }
    if (x <= min_width)
    {
        x = min_width;
        if (y <= max_height)
        {
            y -= dy_m;
        }
    }
    if (y <= min_height)
    {
        y = min_height;
        if (x <= max_width)
        {
            dx_m *= -1;
        }
    }


}


@Override
public void render(Graphics2D g2d)
{
    AffineTransform t = g2d.getTransform();

    double height = 0.3;//meter
    double width = 0.3;//meter

    double bird_footy = height;
    double bird_footx = width / 2;


    int xx = board.convertToPixelX(x - bird_footx);
    int yy = board.convertToPixelY(y + bird_footy);

    g2d.translate(xx, yy);

    double x_expected_pixels = width * board.meter;
    double y_expected_pixels = height * board.meter;

    double x_s = x_expected_pixels / ((ToolkitImage) birdImage).getWidth();

    double y_s = y_expected_pixels / ((ToolkitImage) birdImage).getHeight();

    g2d.scale(x_s, y_s);
    g2d.drawImage(getImage(), 0, 0, this);

    g2d.setColor(Color.BLACK);

    g2d.setTransform(t);
}

I have an image and I want it to move in a counter-clockwise direction and that is my problem. I have a code but it seems my code doesn't work properly. You can check my code if where I've made my mistakes. Please help me regarding this matter...

This is my code:

public void move(long dt)
{

    double dt_s = dt / 1e9;
    double dx_m = speed * dt_s;
    double dy_m = speed * dt_s;

    double width = board.x1_world;
    double height = board.y1_world;

    double min_height = 0.0;
    double max_height = height;
    double min_width = 0.0;
    double max_width = width;

    x += dx_m;
    if (x >= max_width)
    {
        x = max_width;

        if (y >= min_height)
        {
            y += dy_m;
        }
    }
    if (y >= max_height)
    {
        y = max_height;
        if (x >= min_width)
        {
            dx_m *= -1;
        }
    }
    if (x <= min_width)
    {
        x = min_width;
        if (y <= max_height)
        {
            y -= dy_m;
        }
    }
    if (y <= min_height)
    {
        y = min_height;
        if (x <= max_width)
        {
            dx_m *= -1;
        }
    }


}


@Override
public void render(Graphics2D g2d)
{
    AffineTransform t = g2d.getTransform();

    double height = 0.3;//meter
    double width = 0.3;//meter

    double bird_footy = height;
    double bird_footx = width / 2;


    int xx = board.convertToPixelX(x - bird_footx);
    int yy = board.convertToPixelY(y + bird_footy);

    g2d.translate(xx, yy);

    double x_expected_pixels = width * board.meter;
    double y_expected_pixels = height * board.meter;

    double x_s = x_expected_pixels / ((ToolkitImage) birdImage).getWidth();

    double y_s = y_expected_pixels / ((ToolkitImage) birdImage).getHeight();

    g2d.scale(x_s, y_s);
    g2d.drawImage(getImage(), 0, 0, this);

    g2d.setColor(Color.BLACK);

    g2d.setTransform(t);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

魂牵梦绕锁你心扉 2024-12-17 00:41:24

就这样:

import java.io.File;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;

class RT {
    public static void main(String[] args) throws java.io.IOException {
        BufferedImage img = ImageIO.read(new File("input-image.png"));

        BufferedImage rotated = new AffineTransformOp(
                AffineTransform.getQuadrantRotateInstance(
                    3, img.getWidth() / 2, img.getHeight() / 2),
                AffineTransformOp.TYPE_BILINEAR).filter(img, null);

        ImageIO.write(rotated, "PNG", new File("output-image.png"));
    }
}
  • 通过 ImageIO 辅助方法将图像加载为 BufferedImage
  • 创建一个 AffineTransformOp 对象,并给它一个旋转实例转换,最好是 QuadrantRotateInstance,因为您希望精确旋转 90°3 表示 3 个象限,即旋转 3 次,每次 90°;相当于逆时针旋转一圈。
  • 通过您喜欢的质量因子,通常选择BILINEAR方法,这是半快半好质量的。
  • 过滤你的转换;也就是说,将其应用到图像上。过滤返回一个新的 BufferedImage ,或者将新图像存储在给定的 filter 的第二个参数中(上面的 null - 我们将图像存储在新的 BufferredImage 对象)
  • 写入或将图像保存到文件中。

注释
这是一个关于如何进行高效 90° 变换的非常简单的示例。当然这不是唯一的方法。获取此代码并适合您的代码。确保正确处理异常(与我的示例不同)。
如果您想了解更多信息,请阅读有关每个对象的 javadoc 的更多信息。


哎呀

,是的,所以,我发现 - 在阅读了 OP 的代码之后 - 这实际上是不相关的,因为 OP 似乎想要一个视觉效果 > 旋转,不仅仅是旋转图像并呈现它。
如果有人遇到这个问题并正在寻找这个,我会将其留在这里。
对于其余的,请忽略

there you go:

import java.io.File;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;

class RT {
    public static void main(String[] args) throws java.io.IOException {
        BufferedImage img = ImageIO.read(new File("input-image.png"));

        BufferedImage rotated = new AffineTransformOp(
                AffineTransform.getQuadrantRotateInstance(
                    3, img.getWidth() / 2, img.getHeight() / 2),
                AffineTransformOp.TYPE_BILINEAR).filter(img, null);

        ImageIO.write(rotated, "PNG", new File("output-image.png"));
    }
}
  • load your image as BufferedImage through ImageIO helper methods
  • create an AffineTransformOp object, and give it a rotate instance transformation, preferably an QuadrantRotateInstance as you're interested in rotating by 90° exactly. 3 there means 3 quadrants, so three times rotation of 90° each; that equals to one counter-clockwise rotation.
  • pass your prefered quality factor, usually chosen is BILINEAR method, which is semi-fast and semi-good-quality.
  • filter your transform; that is, apply it to the image. A filtering returns a new BufferedImage or it stores the new image in the second argument given to filter (null above - we store the image in a new BufferredImage object)
  • write or save your image to a file.

Notes:
This is a very quick example on how you can do an efficient 90° transform. It's not the only way ofcourse. Take this code and fit it to yours. Make sure to correctly handle exceptions (unlike my example).
Read more on the javadoc about each of those objects if you want to know more.


Woops

yeah, sooo, I see that - after reading the code of OP - that this is unrelated actually, cause the OP seems to want a visual rotation, not just to rotate an image and present it.
I'll leave this here, if anyone bumps into this question and is looking for this.
For the rest, ignore it please.

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