2

I want to rotate a rectangle in a method but do not understand how to do it and tried as follows:

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

Thanks for all.

user1387989
  • 77
  • 1
  • 2
  • 8
  • You might like to have look at [this](http://stackoverflow.com/questions/11911610/affinetransform-rotate-how-do-i-xlate-rotate-and-scale-at-the-same-time/11911758#11911758) and [this](http://stackoverflow.com/questions/12824684/change-the-angle-position-of-a-drawing-with-a-algorithm-in-java/12826882#12826882) and [this](http://stackoverflow.com/questions/11911610/affinetransform-rotate-how-do-i-xlate-rotate-and-scale-at-the-same-time/11911758#11911758). While they work with images, they use AffineTransform to perform their rotations. – MadProgrammer Nov 26 '12 at 21:17
  • You could also have a look at [Rectangle#getPathIterator](http://docs.oracle.com/javase/7/docs/api/java/awt/geom/Rectangle2D.html#getPathIterator(java.awt.geom.AffineTransform)) which will you to pass the transformation directly to it – MadProgrammer Nov 26 '12 at 21:19

2 Answers2

1

You need to call the transform() method on your transform object, passing in the co-ordinates of your rectangle in an array.

Simon Nickerson
  • 42,159
  • 20
  • 102
  • 127
  • I do not understand how to apply my rectangle because the pictures are examples and not create object Graphics. – user1387989 Nov 26 '12 at 22:50
0

This is a little subjective, it all depends on what it is you want to achieve.

The following code uses an AffineTransform to rotate the rectangle, but in order to do so, I need to get a PathIterator and add it back to 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();

        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366