Skip to main content

JXTransformer: The power of a real Swing !

Posted by alexfromsun on July 17, 2006 at 8:40 AM PDT

A long time ago I wanted to have a component which could paint rotated text for my application

I definitely could create my own component, override paintComponent(), implement correct resizing etc...

Yes, it was clear how to implement your own rotatable JLabel, but what if I want to have a rotatable JButton and I want to have it for each Look and Feels, how many classes I would have to sublclass in this case?

At the same time Swing controls the entire painting routine using the power of Java2D library and there is
Graphics2D.setTransform(AffineTransform Tx) method
which can scale, rotate and shear Graphics2D instance.

It sounded like a good starting point and I decided to quicky implement a Swing container which can transform its children component without affecting its functionality.

Nevertheless, this task turned out to be so challenging so I spent about 3 months looking for the best solution from time to time.

(I feel I am ready to give a lecture about the long way to the current implementation :-))

My goal was to implement it using public interfaces only with no reflection hacks and with no affecting the whole system (no custom EventQueue, custom RepaintManager or AWTEventListener) because it would allow using the component in restricted evironments like WebStart applicaitons

Finally I am happy to present the result:
to rotate your JButton just do

JButton button = new JButton("Hello");
JXTransformer t = new JXTransformer(button);
t.rotate(Math.PI/2);

And you'll have a rotated and live button, which works like a usual JButton

Check the WebStart demo and play with three bottom sliders

Source and demo files are published on SwingHelper.dev.java.net
and don't forget to check
CheckThreadViolationRepaintManager and EventDispatchThreadHangMonitor as well

Check it, Test it, Use it and send your valuble feedback !

The JXTransformer is pretty hacky sometimes, so if you believe we should better support things like that for the next JDK
leave a comment here

Note: JXTransformer is an experimental project and has several known limitations

Thanks

Related Topics >>

Comments

Hi, I am working a project related to video, hence I ...

Hi,

I am working a project related to video, hence I have integrated jvlcplayer on my Panel. I needed to have the video in a rotated way, even after using the JXTransformer, I could not do it.

Hello! First all, thank you

Hello! First all, thank you for the library, it is very usefull! Nevertheless, I had a problem and I don`t not really know how to solve it. When I am using JXTRansformer with a JButton inside, a rectangle is always painted when it is transformed. The problem is that this rectangle is not transparent and it is not nice to use without a special background color. I have already check the sources and I have seen that MagicGlassPane is set to Opaque(false). I have done the same with JXTransformer, but I can not solve it. Have you got any idea how to solve it? Thank you in advance!

Hello, I have found how to

Hello,

I have found how to solve the problem and maybe it can be an improvement in the library. In JXTransformer.java I have commented lines 175 and 176 :

g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());

Due to panel is not opaque and the rectangle is not painted, it works fine when we need to show a dinamyc background. In my case, I use it in a JComponent Editor, where component can be placed among a canvas, so it is necessary to have this feature.

Ciao.