So I’ve started using the repaint(Rectangle<int>) method to solve this, by repainting a small area around the moving Component. It works, but I can’t help feeling like it’s a bit of a hack. I’d still love to know how the pro’s approach this problem.
class MoveableObject : public Component
{
void paint(Graphics& g) override { g.fillAll(Colours::aqua); }
};
class MainComponent : public Component
{
public:
MainComponent()
{
setBounds(100, 100, 500, 500);
object.setBounds(10, 10, 50, 50);
addAndMakeVisible(object);
}
void mouseDown(const MouseEvent& e) override
{
dragger.startDraggingComponent(&object, e);
}
void mouseDrag(const MouseEvent& e) override
{
dragger.dragComponent(&object, e, nullptr);
repaint(object.getBoundsInParent().expanded(25)); // <<< whenever the Component is dragged, the parent is repaints 25 pixels around it.
}
private:
ComponentDragger dragger;
MoveableObject object;
};
Question, is your MoveableObject setOpaque (true) by any chance?
I think if you keep it non opaque, it will trigger a repaint of the parent when being moved, so the background should be ok.
But needs to be tested to verify…
I’ve tried setOpaique(), and it doesn’t alter the behavior. If I compile the above code without the repaint(object.getBoundsInParent().expanded(25)) line, I get this when I drag the object:
I’m surprised that I still haven’t found a robust solution to this issue. Every time I move a component, it leaves behind a mark. Do other people not experience this? I’ve grown so used to this in debug code that I don’t notice it any more, but I’m still very curious to know what others do to solve the issue of visual artifacts when Components move.
setOpaque is false by default, you don’t have to set it. Because I don’t have access to you whole code, I don’t see what exactly you are doing, I guess if you a using a DocumentWindow with no other modifications, normally is filled with a background colour and should be repainted correctly.
I suggest updating JUCE first. If this doesn’t help, maybe post the whole example.