Setopaque (true / false); Java

In Java 2D, when you use setopaque, I'm confused about some true and false

For example, I know that swing opera means that when painting, swing won't draw anything behind the component Or this retrogression? Which one?

thank you

Solution

The short answer to your question is "opaque", which is defined in English as completely opaque Therefore, the opaque component is the component that draws the entire rectangle, and each pixel is not translucent to any extent

However, swing component opacity API is one of the most frequently misused APIs because of its wrong design

It is important to know that opaque is a contract between swing system and specific components If it returns true, the component guarantees to draw each pixel of its rectangular area non semitransparent The API should be abstract to force all component authors to consider The isopaque API is used by swing's painting system to determine whether the area covered by a given component must be painted for the components that overlap it and the components behind it (including the container and ancestor of the component) If a component returns true to this API, the swing system can optimize the drawing so that nothing in the area is drawn before calling the paint method of a specific component

Due to the contractual meaning of isopaque, API setopaque should not exist, because in fact, it is incorrect to call setopaque externally, because external things cannot know whether the component honors it or not Instead, isopaque should be overridden by each concrete component to return whether it is actually opaque because it is the current property

Since the setopaque API does exist, many components have implemented it incorrectly (understandably) to drive whether they draw their "background" (such as jlabel and JPanel fill their background color) The result is to create an impression for the API as to whether the setopaque driver should draw the background, but not

In addition, if you want to draw a jlabel with a translucent background, you need to set the background color of an alpha value and do setopaque (true), but it is not opaque - it is translucent; The components behind it still need to be drawn to make the components render normally

New nimbus look & amp; amp; amp; for Java 6; Feel Many error reports on transparent components submitted against nimbus (see stack overflow problem Java nimbus LAF with transparent text fields) The nimbus development team responded:

So, in short, you shouldn't use setopaque If you use it, remember that some look & feel and some components may "surprise" things Moreover, in the end, there is actually no correct answer

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>