 |
JXTransformer: The power of a real Swing !
Posted by alexfromsun on July 17, 2006 at 08:40 AM | Comments (18)
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
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
This is really sweet, I've wanted this for a long time, and it has tons of applications. Awesome.
Posted by: jessewilson on July 17, 2006 at 09:17 AM
-
This is really cool, but AFAIK there is not way to scale text linear in Swing. This means that if you scale a real world UI you wil get some strange effects, because for some scaling values the text will exeed the label, combo box, etc, while for other scaling values the text appears too small. I have seen this effect a hundred times.
I hope someone can tell me that I am just missing some rendering hint that is called KEY_TEXT_SCALING with a value VALUE_TEXT_SCALING_LINEAR, but I guess this just doesn't exist.
Posted by: jansan on July 17, 2006 at 09:44 AM
-
Nice work! Unfortunately, JCheckBox and the Slider thumb do not rotate on MacOS...
Posted by: voorth on July 17, 2006 at 09:45 AM
-
I love this thing! :D Does it work well with say an editing JTree? Im going to have to test that out, at one point in time I really wanted a zooming interface for a JTree I was working on, this may be the ticket.
leouser
Posted by: leouser on July 17, 2006 at 09:46 AM
-
Neat! I was looking for something like this to do animations on a Kiosk product we have. Keep it up!
Posted by: aalmiray on July 17, 2006 at 10:12 AM
-
nice
i want it in jdk :P :P haha
its great :+P
Posted by: j0ke on July 17, 2006 at 12:04 PM
-
Very nice but true, some things do not rotate on Mac Os X. BTW, the approach is different but you may be interested by what we do to rotate JInternalFrame in Diamonspin projet (web site). We have JWS too but I'm not sure all the example work with today version (dstour 6 doesn't seems to work).
Posted by: vernier on July 17, 2006 at 01:28 PM
-
With Windows L&F and using shear the components look very bad even
with JDK 6 (using beta 88 currently). May not be a bad idea to use AA.
Posted by: carmello on July 18, 2006 at 08:36 AM
-
Very cool!
Posted by: prunge on July 18, 2006 at 04:02 PM
-
It's pretty good. A facility to bring them back to the horizontal would dbe great though. This could be a snap or a fine tune knob.
Posted by: ovisvana on July 18, 2006 at 08:37 PM
-
If you could add an anti-alias option to this all swing developers would love you. I assume it wouldn't be too hard considering that AffineTransforms are doing the rotating & scaling, etc & you are setting the Graphic's object's transform.
Posted by: commanderkeith on July 18, 2006 at 11:24 PM
-
You should consider adding this to SwingFX (https://swingfx.dev.java.net/t).
Its the type of thing that project promotes!
Posted by: codecraig on July 19, 2006 at 07:18 PM
-
I impressed!
Posted by: thetan on July 21, 2006 at 12:57 AM
-
i see fisheye components suddenly cropping up everywhere :)
Posted by: ilazarte on July 21, 2006 at 08:11 AM
-
Yeah, that s very cool.
But don't work in my application. When I do:
this.add(createTransformer(myObject));
I see nothing on my Panel (this).
But when I do : this.add(myObject);
I see my object on the JPanel ... I don t understand ...
Posted by: bardack on February 21, 2007 at 02:40 PM
-
Hello bardack
Runnable test case from you will help to solve this problem
alepx
Posted by: alexfromsun on February 26, 2007 at 04:14 AM
-
Hello,
Thats a great code. I liked it a lot. I am trying to use it in my java applet at the moment. But I have the problem that my applet is completely black, and the code doesn't throw any exceptions. Any ideas? Did anybody use this with Applets?
Thanks,
Archie
Posted by: avaliani_360t on April 22, 2008 at 06:19 AM
-
Using the JXTransformer in the applets causes flickering. In order to avoid it, one should implement double buffering for JXTransformer. This solved the problem for me.
Another thing is resizing the fonts. This is the problem I am trying to solve. If you put several labels, texts in the JPanel and wrap only the JPanel, after the scaling the fonts get very ugly. I have implemented the class that goes thorough all children components. This looked much better. The problem is that if I use absolute layout, things get ugly even though I pass constraints. Any suggestions are appreciated.
Archie
Posted by: avaliani_360t on April 24, 2008 at 03:45 AM
|