Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Rotating a JPanel

843806Dec 18 2007 — edited Dec 18 2007
Can anyone point me to tutorials or blogs that explain how to rotate a JPanel? Using/following the tutorial at http://java.sun.com/docs/books/tutorial/2d/advanced/transforming.html I've developed the following code but it doesn't work. All I get is the panel drawn normally albeit with the larger rotated size.

I've also tried to use Alexander Potochkin's JXTransform code (http://weblogs.java.net/blog/alexfromsun/archive/2006/07/jxtransformer_t.html) but when I put a JPanel in as a component the example tanks.

Ultimately I want to put some arrow buttons on the JPanel and rotate everything around the JPanel's center point.

Any help appreciated.
Lori <*>
import java.awt.*;
import java.awt.geom.AffineTransform;

import javax.swing.*;

public class RotateExample extends JFrame  {

    public RotateExample() {
        super("Rotate demo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel outer = new JPanel();
        outer.setPreferredSize(new Dimension(200, 200));
        outer.add(createExample());
        add(outer);
        pack();
    }

    private JPanel createExample() {
    	final RotateComponent btnPanel = new RotateComponent(30);
    	btnPanel.setBorder(BorderFactory.createLineBorder(Color.black));
    	btnPanel.setPreferredSize(new Dimension(50, 50));
        return btnPanel;
    }


    public static void main(String[] args) {
        final RotateExample demo = new RotateExample();
//        demo.setSize(800, 600);
        demo.setVisible(true);
    }

	public class RotateComponent extends JPanel {
		private final double mRadian;
		private final AffineTransform mRotate;

		// constructors
		/**
		 * @param rotate 
		 */
		public RotateComponent(final int rotate) {
			super(null);
			mRadian = Math.toRadians(rotate);
			mRotate = new AffineTransform();
			mRotate.rotate(mRadian);
		}

		// Container methods
		public Dimension getPreferredSize() {
//			if (isPreferredSizeSet()) {
//				return super.getPreferredSize();
//			}
			final Dimension size = getTransformedSize().getSize();
			final Insets insets = getInsets();
			size.width += insets.left + insets.right;
			size.height += insets.top + insets.bottom;
			return size;
		}

		private Rectangle getTransformedSize() {
			final Dimension viewSize = super.getPreferredSize();
			final Rectangle viewRect = new Rectangle(viewSize);
			return mRotate.createTransformedShape(viewRect).getBounds();
		}

		/**
		 * @see javax.swing.JComponent#paint(java.awt.Graphics)
		 */
		public void paintComponent(final Graphics g) {
			final Graphics2D g2 = (Graphics2D) g;
			final AffineTransform oldAt = g2.getTransform();
			final Rectangle r = getBounds();
			final double x = r.getCenterX();
			final double y = r.getCenterY();
			final AffineTransform at = new AffineTransform();
			at.concatenate(oldAt);
			at.translate(-x, -y);
			at.rotate(mRadian, x, y);
			g2.transform(at);
			super.paintComponent(g2);
			g2.setTransform(oldAt);
		}
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 15 2008
Added on Dec 18 2007
2 comments
659 views