954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Rotate JLabel or image in label?

Hey guys,

I've seen very limited examples out there about this unsupported function, however I am sure there must be a quick method to get this done with the Affine Transform ?

Any ideas appreciated.

Cleo

StephNicolaou
Posting Whiz in Training
204 posts since Nov 2007
Reputation Points: 77
Solved Threads: 18
 

If you have the image, and are drawing it in a paintComponent method, then you can cast the Graphics g parameter to Graphics2D and use its rotate method. (which is an easier cover method for a rotational AffineTransform)

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Thanks for your advice :)

Currently breaking this down, it works great but shorted is always better.

private JLabel lblRover = new JLabel(imageIcon) {
	protected void paintComponent(Graphics g) {
	Graphics2D g2 = (Graphics2D)g;
	g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
							RenderingHints.VALUE_ANTIALIAS_ON);
	AffineTransform aT = g2.getTransform();
	Shape oldshape = g2.getClip();
	double x = getWidth()/2.0;
	double y = getHeight()/2.0;
	aT.rotate(Math.toRadians(angle), x, y);
	g2.setTransform(aT);
	g2.setClip(oldshape);
	super.paintComponent(g);
    }
};


http://www.java-forums.org/awt-swing/24770-jlabel-rotation-property.html

StephNicolaou
Posting Whiz in Training
204 posts since Nov 2007
Reputation Points: 77
Solved Threads: 18
 

Shorter is not always better - easier to understand is better. If you have ever had to fix or update someone else's code you would agree. Breaking it down to easy steps is a good thing; the compiler is very good at optimising simple code. Your code is great, don't change it.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You