您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在Java中旋转图形

如何在Java中旋转图形

如果您使用plain Graphics,则强制转换为Graphics2Dfirst:

Graphics2D g2d = (Graphics2D)g;

旋转整个Graphics2D

g2d.rotate(Math.toradians(degrees));
//draw shape/image (will be rotated)

要重置旋转(因此您只旋转一件事):

AffineTransform old = g2d.getTransform();
g2d.rotate(Math.toradians(degrees));
//draw shape/image (will be rotated)
g2d.setTransform(old);
//things you draw after here will not be rotated

例:

class MyPanel extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        AffineTransform old = g2d.getTransform();
        g2d.rotate(Math.toradians(degrees));
        //draw shape/image (will be rotated)
        g2d.setTransform(old);
        //things you draw after here will not be rotated
    }
}
java 2022/1/1 18:17:29 有474人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶