Rotating one image under another one

笑着哭i 提交于 2019-12-02 08:53:33

问题


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.


回答1:


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);



回答2:


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
}


来源:https://stackoverflow.com/questions/7698030/rotating-one-image-under-another-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!