[Solved] setBufferedToImage not working?


#1

The function “setBufferedToImage” is not working for me.
I have a component, which is opaque. It has the name “TestComp”. I also set “setBufferedToImage(true)” on this component.
Inside the component I have a TextButton. But still, every time the mouse enters the TextButton, the paint() routine for TestComp is called. I expected it to use its buffered image instead. But it is not doing that. What am I doing wrong?

Here is my code:

class TestComp : public Component
{
public:
    TestComp()
    {
        setOpaque(true);
        setBufferedToImage(true);
        button.setButtonText("Hello!");
        addAndMakeVisible(button);
    }

    void paint(Graphics& g) override
    {
        g.fillAll(Colours::darkgrey);
        static int ctr = 0;
        DBG("Repainting whole background: " + String(++ctr));
    }

    void resized()
    {

        button.setBounds(100, 100, 100, 30);
    }

    TextButton button;
};

I am on Windows 10 and JUCE 5.3.2.

See also attached image.


#2

While the main component may be opaque, your test button component isn’t which will cause repainting for parent components or any components that may overlap it


#3

I understand that the main component (=TestComp) needs to repaint. But I told it to use the buffered image. So it should us the buffered image for repainting. Why does it not use that?

I must be misunderstanding something here, I guess. But isn’t this the typical situation, a buffered image is made for?


#4

The image is reclaculated any time the component has its repaint() function called, which in this case happens every time the child button is interacted with.

For example if your button was set as buffered to an image instead and the parent was repainted, the button internally should use the buffered image instead of running its whole paint() function. If you were to call button.repaint() then the entire buffered image would be recalculated


#5

AFAIK you can create a background-class, where you put the contents of your paint method you want to buffer, and use this background class as a member of your TestComp class. When the background is bufferedToImage, it should work.


#6

Thanks for your explanations and help, @TonyAtHarrison and @danielrudrich .
Yes, when using a separate Background component, it works. :slight_smile:

Note to others: you have to se the Backround component to be in the background with “Component::toBack()”


#7

For completeness: every component not only has a paint() method you can override but also a paintOverChildren() method: https://docs.juce.com/master/classComponent.html#aa15dc77c06b5a0a4192090b4a323f79c
Also very handy when using a background component as a child.