Repaint() function doesn't run paint(Graphics& g)


#1

Hi all,
I have a screen, I draw something in

void paint(Graphics& g) override 
{
   // Paint something here
}

function. I want is every time the size of the screen is changed, the screen will be pain a gain. So, I called repaint(); in the function resized() like this:

void resized() override
{
   // Do something
   repaint();
}

But the paint(Graphics& g) wasn’t called after the resized() has been called? Can you help me solve this problem?


#2

The components should be repainting themselves automatically when they are resized. Why are you trying to manually call repaint() from resized()?


#3

Thank you @Xenakios
I want to draw some text like this “This is the link to th…” but when I zoom in my screen, the text isn’t changed. My expected result is the text should be “This is the link to the audio website” but it’s not.
My code in paint() here:

void paint(Graphics& g)
{
        g.fillAll(m_backgroundColor);

	g.setColour(m_drawColor);

	g.setFont(m_font);

       g.drawFittedText(m_content, 0, 0, m_height, getLocalBounds().getWidth());
      
      ...
}

#4

A repaint is triggered when a Component’s bounds are changed… not the size of the display. How are you zooming?

You can see how this all works by looking at the JUCE source code.

Rail


#5

Have you ensured resized() itself is even called when zooming in the screen? Maybe there’s some other Component callback for that? Is this about a touchscreen device?

virtual void Component::mouseMagnify

Seems like a candidate…“Called when a pinch-to-zoom mouse-gesture is used”


#6

I’m sure resized(); is called, I put a breakpoint inside the resized() to debug it


#7

Component::resized() doesn’t trigger a repaint (check the source).

void Component::resized()                       {}
void Component::moved()                         {}
void Component::childBoundsChanged (Component*) {}
void Component::parentSizeChanged()             {}

Look at:

 Component::sendMovedResizedMessages

and

 Component::sendMovedResizedMessagesIfPending

and search for when those are called in the JUCE code.

Rail


#8

The poster’s problem appears to be that even if repaint() is called in his overridden resized() method, the repaint is apparently not being done. IMHO the repaint() call is not even necessary though, when the component has been resized, repaint() likely has been called by JUCE already. I wonder if the poster’s resized() implementation’s “do something” part is doing something relevant to the problem? :slight_smile:


#9

Yes, there’s no point calling repaint() inside resized(), it’s already been done.

In my experience, if you’re calling repaint and the component isn’t repainting, then you’re probably calling it on the wrong component.


#10

Like others pointed out: don’t put a repaint() in resized(). It will happen automatically, when the bounds are changed.
If you call repaint() on your Component, but no paint() is happening, there could be one of these situations:

  • the component is not visible at all (size > 0 and setVisible (true) or addAndMakeVisible not called)
  • the Component is hidden behind another Component, that is marked opaque but is not painting itself
  • what Jules just mentioned: confusion with the instances

or some more…

Good luck


#11

Thank all of you so much for your help. I tested and removed repain() from resized(). I’ll debug again to find out my problem here. :slight_smile: