【问题标题】:how to rotate a rectangle in Java2D如何在 Java2D 中旋转矩形
【发布时间】:2012-11-14 09:53:40
【问题描述】:

我想在一个方法中旋转一个矩形但不明白怎么做,尝试如下:

private void setBoundaryRotate(Rectangle b, int radio) {
        AffineTransform transform = new AffineTransform();
        transform.rotate(Math.toRadians(45), b.getX() + b.width/2, b.getY() + b.height/2);}

谢谢大家。

【问题讨论】:

  • 你可能想看看thisthisthis。在处理图像时,他们使用 AffineTransform 来执行旋转。
  • 您还可以查看Rectangle#getPathIterator,您可以将转换直接传递给它

标签: java java-2d


【解决方案1】:

您需要在 transform 对象上调用 transform() 方法,将矩形的坐标传入数组中。

【讨论】:

  • 我不明白如何应用我的矩形,因为图片是示例而不是创建对象图形。
【解决方案2】:

这有点主观,这完全取决于您想要实现的目标。

以下代码使用AffineTransform 来旋转矩形,但为此,我需要获取PathIterator 并将其添加回Path2D

public class SpinBox {

    public static void main(String[] args) {
        new SpinBox();
    }

    public SpinBox() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new SpinPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class SpinPane extends JPanel {

        private Rectangle box = new Rectangle(0, 0, 100, 100);
        private float angle = 0;

        public SpinPane() {
            Timer timer = new Timer(1000/60, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    angle += 1;
                    repaint();
                }
            });
            timer.setRepeats(true);
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            int width = getWidth() - 1;
            int height = getHeight() - 1;

            int x = (width - box.width) / 2;
            int y = (height - box.height) / 2;

            Graphics2D g2d = (Graphics2D) g.create();
            AffineTransform at = new AffineTransform();
            at.rotate(Math.toRadians(angle), box.x + (box.width / 2), box.y + (box.height / 2));
            PathIterator pi = box.getPathIterator(at);
            Path2D path = new Path2D.Float();
            path.append(pi, true);
            g2d.translate(x, y);
            g2d.draw(path);
            g2d.dispose();

        }

    }

}

【讨论】:

    猜你喜欢
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 2016-12-10
    • 1970-01-01
    相关资源
    最近更新 更多