participate


Swing - non-opaque color for an opaque component?
<<   Back to Forum  |   Give us Feedback
This topic has 2 replies on 1 page.
hiwa
Posts:5,008
Registered: 3/29/99
non-opaque color for an opaque component?   
Jul 8, 2008 4:40 AM

 
Below is a quote from JComponent.paintComponent() method doc:

<quote>
Further, if you do not invoker super's implementation you must honor the opaque property, that is if this component is opaque, you must completely fill in the background in a non-opaque color.
</quote>

I doubt the 'non-opaque color' part might be a typo. If it is a correct sentence, then why do you use a 'non-opaque color' background for an opaque component as a substitute for the super.paintComponent() call? What on earth do I misunderstand in this context of things?

W, w, w, wait, 'invoker' is also a typo, I believe.

Edited by: hiwa on 2008/07/08 20:39
 
camickr
Posts:32,726
Registered: 2/27/98
Re: non-opaque color for an opaque component?   
Jul 8, 2008 8:19 AM (reply 1 of 2)  (In reply to original post )

 
you must completely fill in the background in a non-opaque color.
I doubt the 'non-opaque color' part might be a typo

I think it is a typo, otherwise, like you, I don't know what they are trying to say.

Just using setOpaque(true), does NOT make a component opaque. It simply tells the the RepaintManger not to search the ancestor tree to find an opaque component. This makes painting more efficient if it doesn't need to search all the way up to the content pane of the window. By setting the opaque property you are guaranteeing that the component will paint its own background in an opaque color.

For example a JPanel is opaque and its paintComponent() method will invoke the fillRect(...) method to paint the background whatever color is specified by the setBackground() method.

So if you do panel.setBackground(Color,RED) this is valid.

However, if you do panel.setBackground( new Color(255, 0, 0, 128) ); this is invalid because you have set a background with an alpha value that results in a transparent color. In this case you will generally see artifacts in the background of the panel. If you want to do this, then you must also use setOpaque(false), so that the background of the ancestor component is painted first.

Edited by: camickr on Jul 8, 2008 11:18 AM

Fixed an incorrect reference from "non-opaque" to "opaque".
 
Andre_Uhres
Posts:867
Registered: 5/17/05
Re: non-opaque color for an opaque component?   
Jul 8, 2008 9:26 AM (reply 2 of 2)  (In reply to #1 )

 
Excellent explanation by camickr, worth to be bookmarked, thanks!
I like also the article "Painting in AWT and Swing" by Amy Fowler.
Here is his description of the opaque property:
http://java.sun.com/products/jfc/tsc/articles/painting/#props
 
This topic has 2 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 26
  • Guests : 129

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums