Confused about Components and setOpaque


I noticed that when I created a custom Component in my plug-in and call setOpauqe(true) that other components from the window draw inside the Components bounds.  As a test I added a "do nothing" component to the Juce demo plug-in and it does the same thing.  I would expect that it would only redraw the area that is directly underneath the Component.

I attached a pic so you can see what I mean.  See how the text "Juce v3.0.4" duplicated in the other component.  If I adjust other widgets then they will redraw in the same spot.  Can anyone enlighten me how this is supposed to work?  Why doesn't it just redraw the grey blank area that is underneath?







setOpaque ist just the information for the juce renderer, that the component itself will repaint, its whole area, if it does not,  the result will be unpredictable.

May it should be renamed, something like setHasOpaqueBehavior 


    /** Indicates whether any parts of the component might be transparent.
        Components that always paint all of their contents with solid colour and
        thus completely cover any components behind them should use this method
        to tell the repaint system that they are opaque.
        This information is used to optimise drawing, because it means that
        objects underneath opaque windows don't need to be painted.
        By default, components are considered transparent, unless this is used to
        make it otherwise.
        @see isOpaque, getVisibleArea
    void setOpaque (bool shouldBeOpaque);


If you call SetOpaque(true) on a component, you are declaring that its paint() function will definitely render it opaque - i.e. no pixels within its bounds will have any transparency. You also need to make sure that this is indeed the case! The simplest way to ensure this is to start your paint() function with a 'g.fillAll()' using an opaque colour.

Why doesn't it just redraw the grey blank area that is underneath?

It doesn't redraw the area behind it because you've explicitly told the renderer that it doesn't need to! Perhaps you have the meaning of the word 'opaque' inverted in your head! :) if you want the background to be drawn behind it, then you clearly want it to be transparent (which it is, by default), so don't call setOpaque(true).


tldr; You should only set a component to be opaque if you know that you won't be able to see what is behind it.


Thanks guys.  I still don't understand why it draws a duplicate of the other redrawn components inside the component.  For example, let's say I adjust the Throughput Level control it will draw copy of the control inside the other component rect.  Of course the behvior is undefined so I guess I shouldn't expect otherwise...


It's just junk, probably the last clip rect to be drawn and as you've explicitly told the graphics context not to clear itself (by setting setOpaque (true)) it will just end up drawing whatever was last in the context.