# How to draw a Drawable

**URL:** <https://forum.juce.com/t/how-to-draw-a-drawable/19300>\
**Category:** General JUCE discussion\
**Created:** [September 29, 2016, 1:22pm UTC](https://forum.juce.com/t/how-to-draw-a-drawable/19300 "2016-09-29T13:22:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![oxxyyd](https://avatars.discourse-cdn.com/v4/letter/o/e47c2d/32.png) [@oxxyyd](https://forum.juce.com/u/oxxyyd)\
**Post date:** [September 29, 2016, 1:22pm UTC](https://forum.juce.com/t/how-to-draw-a-drawable/19300/1 "2016-09-29T13:22:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [September 30, 2016, 3:12pm UTC](https://forum.juce.com/t/how-to-draw-a-drawable/19300/2 "2016-09-30T15:12:07Z")

</div>

Just do `addAndMakeVisible (semibrave);`

---

<div class="post-metadata">

**Author:** ![oxxyyd](https://avatars.discourse-cdn.com/v4/letter/o/e47c2d/32.png) [@oxxyyd](https://forum.juce.com/u/oxxyyd)\
**Post date:** [October 11, 2016, 3:55pm UTC](https://forum.juce.com/t/how-to-draw-a-drawable/19300/3 "2016-10-11T15:55:22Z")

</div>

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

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

> void Score::paint(Graphics& g)  
> {  
> //clear previous chord/score  
> scoreComponents.clearQuick(true);

> for each(int staffYPos in notes)  
> {  
> Component \*c = new Component();  
> c-\>addAndMakeVisible(halfNote);  
> addAndMakeVisible(c);  
> scoreComponents.add(c);

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

instead of

> void Score::paint(Graphics& g)  
> {  
> Rectangle\<float\>rc;

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

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

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [October 12, 2016, 7:47am UTC](https://forum.juce.com/t/how-to-draw-a-drawable/19300/4 "2016-10-12T07:47:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![oxxyyd](https://avatars.discourse-cdn.com/v4/letter/o/e47c2d/32.png) [@oxxyyd](https://forum.juce.com/u/oxxyyd)\
**Post date:** [October 12, 2016, 12:43pm UTC](https://forum.juce.com/t/how-to-draw-a-drawable/19300/5 "2016-10-12T12:43:34Z")

</div>

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”
