Can Components be Rotated Visually?

L

larkmore

This has come up a lot recently. Is there a way to apply a rotation
to how a component is displayed? I'm not referring to things covered
by SwingConstants.VERTICAL or SwingConstants.HORIZONTAL. What I want
to do is take a JLabel and rotate the entire thing counterclockwise by
ninety degrees so it reads along the side of the screen, or flip a
JProgressBar so it "grows" from top to bottom instead of bottom to
top. Is there a way to do this with predefined components?
-Will
 
T

Tom Hawtin

This has come up a lot recently. Is there a way to apply a rotation
to how a component is displayed? I'm not referring to things covered
by SwingConstants.VERTICAL or SwingConstants.HORIZONTAL. What I want
to do is take a JLabel and rotate the entire thing counterclockwise by
ninety degrees so it reads along the side of the screen, or flip a
JProgressBar so it "grows" from top to bottom instead of bottom to
top. Is there a way to do this with predefined components?

You can rotate the Graphics object passed through paint. Unfortunately
it becomes more complicated if you include events (like repaint events),
and the whole component tree and co-ordinate thing is exposed and all
incestuous.

Tom Hawtin
 
L

larkmore

Sounds ugly, but how exactly would one do that? I skimmed the API
documentation for the Graphics class and didn't see anything about
rotating.
-Will
 
T

Tom Hawtin

Sounds ugly, but how exactly would one do that? I skimmed the API
documentation for the Graphics class and didn't see anything about
rotating.

It's the Graphics2D features you want.

Tom Hawtin
 
L

larkmore

It's the Graphics2D features you want.

Tom Hawtin

I'm still confused on how to do this. How do I convert a Graphics
object into a Graphics2D object so I can apply the rotation? If I
wait until the paint(Graphics g) method to be called, the Graphics
object is already instantiated and I see no way to cast it or convert
it into a Graphics2D object.
-Will
 
T

Tom Hawtin

I'm still confused on how to do this. How do I convert a Graphics
object into a Graphics2D object so I can apply the rotation? If I
wait until the paint(Graphics g) method to be called, the Graphics
object is already instantiated and I see no way to cast it or convert
it into a Graphics2D object.

The Graphics object you get from paint will be a Graphics2D object. You
just need to cast it to get the more specific static type.

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top