将一张图像旋转到另一张图像下方

发布于 2024-12-08 23:18:38 字数 462 浏览 0 评论 0原文

我目前正在尝试旋转图像,然后在不旋转的顶部绘制图像。但每当我使用: g2d.rotate(Math.toRadians(rot), (x+15), (y+15)); 之后我绘制的每张图像也会旋转。有什么方法可以旋转一张图像而不旋转其余图像(天哪,这真的很难解释)。 这是我的绘画方法:

public void draw(Graphics2D g2d)
{
    move();
    if(bo.px==+1)rot--;
    if(bo.px==-1)rot++;
    g2d.rotate(Math.toRadians(rot), (x+15), (y+15));
    g2d.drawImage(img, x, y, null);//this should rotate
    g2d.drawImage(shine, x, y, null);//this shouldn't
}

提前致谢。

I am currently trying to rotate an image and then drawing an image on top which isn't rotating. But whenever I use:
g2d.rotate(Math.toRadians(rot), (x+15), (y+15));
every image I draw afterwards rotates as well. Is there any way I can rotate one image and not rotate the rest (gosh its really hard to explain).
Here's my paint method:

public void draw(Graphics2D g2d)
{
    move();
    if(bo.px==+1)rot--;
    if(bo.px==-1)rot++;
    g2d.rotate(Math.toRadians(rot), (x+15), (y+15));
    g2d.drawImage(img, x, y, null);//this should rotate
    g2d.drawImage(shine, x, y, null);//this shouldn't
}

Thanks in advance.

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

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

发布评论

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

评论(2

因为看清所以看轻 2024-12-15 23:18:38

您可以保存原始变换,旋转并绘制第一个图像,然后在绘制第二个图像之前应用回原始变换。

尝试

AffineTransform originalTransform = g2d.getTransform();
g2d.rotate(Math.toRadians(rot), (x+15), (y+15));
g2d.drawImage(img, x, y, null);
g2d.setTransform(originalTransform);
g2d.drawImage(shine, x, y, null);

You can save the original transform, rotate and draw the first image and then apply back the original transform before drawing the second image.

Try

AffineTransform originalTransform = g2d.getTransform();
g2d.rotate(Math.toRadians(rot), (x+15), (y+15));
g2d.drawImage(img, x, y, null);
g2d.setTransform(originalTransform);
g2d.drawImage(shine, x, y, null);
靖瑶 2024-12-15 23:18:38

绘制旋转图像后,您需要执行反向旋转以使事物恢复到原始的非旋转状态。

public void draw(Graphics2D g2d)
{
    move();
    if(bo.px==+1)rot--;
    if(bo.px==-1)rot++;
    g2d.rotate(Math.toRadians(rot), (x+15), (y+15));
    g2d.drawImage(img, x, y, null);//this should rotate
    g2d.rotate(-Math.toRadians(rot), (x+15), (y+15)); // this resets the rotation!
    g2d.drawImage(shine, x, y, null);//this shouldn't
}

After you draw the rotated image you need to perform the inverse rotation to bring things back to the original non-rotated state.

public void draw(Graphics2D g2d)
{
    move();
    if(bo.px==+1)rot--;
    if(bo.px==-1)rot++;
    g2d.rotate(Math.toRadians(rot), (x+15), (y+15));
    g2d.drawImage(img, x, y, null);//this should rotate
    g2d.rotate(-Math.toRadians(rot), (x+15), (y+15)); // this resets the rotation!
    g2d.drawImage(shine, x, y, null);//this shouldn't
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文