How to draw a Drawable


I first did things like

paint (Graphics& g)

Drawable *semibrave;

semibrave->drawAt (g, x, y, 1.0f);

But then in drawable.h I read

Note that the preferred way to render a drawable in future is by using it as a component and adding it to a parent, so you might want to consider that before using this method.

So I guess my question is, have the future arrived yet, and if so, what does it mean to use it as a component and what’s the (future) benefit of that? Any coding examples?


Just do addAndMakeVisible (semibrave);


Hmm… that looks fine for more… stationary drawables. But if I want to draw a live score of say chords being played, it then would look something like this if done your way

Array<int> notes;
static Drawable *halfNote = Drawable::createFromImageFile(File(“WhiteNotehead.svg”));

void Score::paint(Graphics& g)
//clear previous chord/score

for each(int staffYPos in notes)
Component *c = new Component();

  c->setBounds(x, staffYPos, noteWidth, linePitch);	


instead of

void Score::paint(Graphics& g)

for each(int staffYPos in notes)
rc.setBounds(x, staffYPos, noteWidth, linePitch);
halfNote->drawWithin(g, rc, RectanglePlacement::flags::yBottom, 1.0f);

Looks a bit more complicated to me. Or am I missing something?


Might be better to keep a stash of svg drawables around and rather hide/un-hide the drawables and move them around.


Yeah, but what’s the drawback with just using drawWithin()?

Using a stash of drawables as you suggest raises a bunch of deep philosophical questions like how many notes can there be in a chord, should putting your forearm on the piano keyboard be considered as legitimate music etc etc. And then there’s the filled notes, and quavers… and you end up with creating n*128 drawables just for some stupid end cases that with 99.999% certainty never gonna happen but where draw the line, does 99% suffice?

Anyway, I suggest to change the comment from “the preferred way in future…” to something more like “For a stationary drawable you might want to consider using it as a component and adding it to a parent”