Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2004

Tip: Looking for answers? Try searching our database.

antialiasing VALUE_IRRITATED_ON

Thread view: 
Cid - 17 Aug 2004 01:36 GMT
Messin around with antialiasing. I filled up a JTextArea (see derived
variant below) with some text and did some resizing tests.

1. On initially loading and being shown, the frame is blank for about
3 seconds (on my 2Ghz machine) whereas it shows instantaneously w/o
antialiasing.

2. When resizing, occasionall (often but not always), there is a 1-2
second delay while it finishes the layout for the new size - again
only present with AA on.

Is there some other way to do this? For instance, notepad.exe on my
windows machine is AA'd and it I can resize it all day long without it
becoming unresponsive. Granted, it's native and compiled so it's not
exactly a fair contest. Is that what it comes down to? Swing just
isn't gonna be able to dance fast enough in its current incarnation?

(sample: my derived JTextArea with AA behavior)

class JTextAreaAA extends JTextArea {
public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g ;
    g2.setRenderingHint(
        RenderingHints.KEY_RENDERING,
        RenderingHints.VALUE_RENDER_SPEED
    ) ;
    g2.setRenderingHint(
        RenderingHints.KEY_TEXT_ANTIALIASING,
        RenderingHints.VALUE_TEXT_ANTIALIAS_ON
    ) ;
    super.paintComponent(g) ;
}
}
Mr.Cube - 17 Aug 2004 12:10 GMT
> class JTextAreaAA extends JTextArea {
>  public void paintComponent(Graphics g) {
[quoted text clipped - 8 lines]
>     ) ;
>     super.paintComponent(g) ;

Here you should set the RenderingHints like they were before you entered
this method !
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                    RenderingHints.VALUE_TEXT_ANTIALIAS_OFF)

>  }
> }
Cid - 17 Aug 2004 16:16 GMT
>>  public void paintComponent(Graphics g) {
>>     Graphics2D g2 = (Graphics2D) g ;
...
>>     super.paintComponent(g) ;
>
>Here you should set the RenderingHints like they were before you entered
>this method !
>g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
>                     RenderingHints.VALUE_TEXT_ANTIALIAS_OFF)

That helps a little. Cuts down the initial load lag dramatically but I
still see heavy lag when resizing (particularly, on my system it gets
into trouble when either dimension exceeds about 600).

I tried both the Graphics create/dispose route and setting/unsetting
the original. Both performed the same.

Full source for the sample at: http://www.geocities.com/anoidx

Relevant code:

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.dispose() ;
}
}
Rogan Dawes - 18 Aug 2004 07:56 GMT
>>> 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"



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.