...
>>> public void paintComponent(Graphics g) {
>>> Graphics2D g2 = (Graphics2D) g ;
[quoted text clipped - 34 lines]
> }
> }
Looks to me like you are still not doing what the parent poster
suggested. Where did you put the
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
??
And are you sure you are meant to dispose of the Graphics when you are
finished?
I would expect that:
class JTextAreaAA extends JTextArea {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create() ;
g2.setRenderingHint(
RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_SPEED
) ;
g2.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON
) ;
super.paintComponent(g2) ;
g2.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF
) ;
}
}
would do what you need.
I would intuitively expect that disposing the Graphics object would mean
that the UI would have to recreate it each time for the rest of the UI
to use . . . ?
From the JavaDocs for Graphics:
public abstract void dispose()
Disposes of this graphics context and releases any system resources
that it is using. A Graphics object cannot be used after dispose has
been called.
[snip]
Graphics objects which are provided as arguments to the paint and
update methods of components are automatically released by the system
when those methods return. *For efficiency, programmers should call
dispose when finished using a Graphics object only if it was created
directly from a component or another Graphics object.*
Check the sentence between *'s.
Rogan

Signature
Rogan Dawes
*ALL* messages to discard@dawes.za.net will be dropped, and added
to my blacklist. Please respond to "nntp AT dawes DOT za DOT net"
Morten Alver - 18 Aug 2004 08:44 GMT
> Looks to me like you are still not doing what the parent poster
> suggested. Where did you put the
>
> g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
> RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
I've also used antialiasing, specifically in a (large) JTable and some
JTextAreas, and I've never set the rendering hints back like suggested
here, because I thought I was free to mess with the graphics object sent
to the paint() method. I've seen a significant difference in performance
with and without antialiasing, but I sort of expected that.
I'd like to know more about why I should reset the rendering hints, and
how important it is. What are the consequences of not doing this?
--
Morten
Cid - 18 Aug 2004 16:05 GMT
>I'd like to know more about why I should reset the rendering hints, and
>how important it is. What are the consequences of not doing this?
It's because that Graphics context is used for all the components
being painted. If you turn on AA for one label by setting rendering
hints in its paint and leave them on, those hints remain in affect for
every component painted after your label. That could really slow
things down unintentionally.
More than that, it's a general contract in the Swing paint pipeline
that a component can paint itself however it wants but it shouldn't
force its painting environment on other non-child components. Thus as
a matter of politeness as much as efficiency, the Graphics context
should be in the same state after leaving your paintComponent as it
was when it entered.
That's why I like the create() / dispose approach since it leaves the
original Graphics completely untouched.
Cid - 18 Aug 2004 16:00 GMT
>>>> public void paintComponent(Graphics g) {
>>>> Graphics2D g2 = (Graphics2D) g ;
[quoted text clipped - 22 lines]
>> public void paintComponent(Graphics g) {
>> Graphics2D g2 = (Graphics2D) g.create() ;
...
>> super.paintComponent(g2) ;
>> g2.dispose() ;
[quoted text clipped - 3 lines]
>Looks to me like you are still not doing what the parent poster
>suggested. Where did you put the
Note the usage - a new graphics instance is created from the original
passed in (g). I set the rendering hints for that instance I created
and, paint with it, and dispose of it at the end. The original remains
in its original state on leaving the method - which is the desired
result. This is one of the two methods recommended in the Swing
tutorial. The other is as you described. I tried them both and both
performed comprably. I find this one to be cleaner code-wise so that's
why I use it.
The general rule for Graphics objects (as you quoted) is that if you
create it rather than recieving it as a parameter, you need to dispose
it.
Rogan Dawes - 19 Aug 2004 09:54 GMT
>>>I tried both the Graphics create/dispose route and setting/unsetting
>>>the original. Both performed the same.
[quoted text clipped - 29 lines]
> create it rather than recieving it as a parameter, you need to dispose
> it.
Right. I see that now.
Sorry.
Rogan

Signature
Rogan Dawes
*ALL* messages to discard@dawes.za.net will be dropped, and added
to my blacklist. Please respond to "nntp AT dawes DOT za DOT net"