The following has been originally posted here and here on June 13th and 14th, 2005.

There are two things I want to say about this shot.

1. Antialiasing
Antialiasing in your custom components can be achieved quite easily. I found this code when I was trying to find out how to rotate a String:

RenderingHints renderHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
renderHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

Quite easy, isn’t it? After doing that, all your lines and text will be antialiased. I don’t know in detail about what most settings do, because I shamelessly copied it from Aragorn94 of the Sun Java forums.

2. Rotating text
When you do a simple Google search, you end up with quite some code that has a few problems. Applying a rotation (AffineTransform) on the Graphics context is bad because you rotate around the origin of the context which again has to be fixed by using a translation, and so on. I had the problem that it was cut off at the end of the visible area of a scrollpane, although the component was bigger than that. Then there are some people who use an ImageFilter to rotate stuff, which needs way too much code.

The easiest way which actually works is this:

Font oldFont = g.getFont();
Font f = oldFont.deriveFont(AffineTransform.getRotateInstance(-Math.PI / 2.0));
g.drawString(”Test”, 100, 100);

I’m not sure why the code found on the web didn’t work for me, it looked good; However, doing a transform on the Font itself is easier anyway than doing a transform on the Graphics object or the whole image. Take this code as public domain without any strings attached for use in your own projects.

If you want transparency in your custom components, take a look at the AlphaComposite class and the Graphics2D setComposite(Composite) method. It’s sexy™.

Are you interested in reading more from CodingClues?
Then subscribe to new postings via RSS or via E-Mail.

Viewing 2 Comments

close Reblog this comment
blog comments powered by Disqus