# How to create Graphics array?

**URL:** <https://forum.juce.com/t/how-to-create-graphics-array/48114>\
**Category:** General JUCE discussion\
**Created:** [October 4, 2021, 9:48pm UTC](https://forum.juce.com/t/how-to-create-graphics-array/48114 "2021-10-04T21:48:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [October 4, 2021, 9:48pm UTC](https://forum.juce.com/t/how-to-create-graphics-array/48114/1 "2021-10-04T21:48:22Z")

</div>

Hello,  
it’s probably question more about C++ than about Juce. But I can’t find the solution.

I have array of `Image`, declared like that:  
`Image curvesIMG[3];`

And now I need to have `Graphics` array to draw to my images. But of course declaring it like that:  
`Graphics curvesGraph[3];`  
gives me error.

I tried to define it in header with code:

```auto
MyClass() : curvesGraph{curvesIMG[0], curvesIMG[1], curvesIMG[2]}
{
}

```

But I get error: `No viable conversion from 'juce::Image' to 'juce::Graphics'`

So I started experimenting with `std::vector<Graphics> curvesGraph`, but for  
`push_back( Graphics(curvesIMG[0]) );`  
I get error: `Call to deleted constructor of 'juce::Graphics'`

So I tried:  
`curvesGraph.reserve(3);`  
`curvesGraph.emplace_back(curvesIMG[0]);`  
But I get the same error.

So now I have no idea how to solve it. Could anyone help me?

Great thanks in advance,  
Best Regards

---

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [October 4, 2021, 9:53pm UTC](https://forum.juce.com/t/how-to-create-graphics-array/48114/2 "2021-10-04T21:53:28Z")

</div>

Why are you using a `Graphics` context? Are you drawing into an Image? otherwise, we generally only access `Graphics` as it is passed into `Componentpaint (Graphics& g)`? You don’t store that, just use it. Maybe start out by explaining what you want to do, instead of how you are trying to do it. 🙂

---

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [October 4, 2021, 10:07pm UTC](https://forum.juce.com/t/how-to-create-graphics-array/48114/3 "2021-10-04T22:07:06Z")

</div>

Hmm… thanks for your suggestion.

I am creating graphical equaliser, and all curves (`Path`) I stroke with `Graphics curveGraph;` to one `Image allCurvesImg;`, and then I use inside

```auto
Component::paint(Graphics& g)
{
    g.drawImageAt(allCurvesImg, x, y);
}

```

But now I found out it is not optimal, because even if I change only one EQ parameter, I need to call `allCurvesImg.clear(allCurvesImg.getBounds())` and then stroke again all Paths. So I try to have all curves in separate `Images`, and then I can stroke only Paths that I really need but not all.

Of course the issue is much more complicated, but I explained the main issue as simple as possible.

Of course I could stroke Paths which I need inside `Component::paint()`, but actually I like the idea to keep them as Images and draw Images with `g.drawImageAt(allCurvesImg, x, y);`

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [October 4, 2021, 10:49pm UTC](https://forum.juce.com/t/how-to-create-graphics-array/48114/4 "2021-10-04T22:49:28Z")

</div>

The problem is, that the `juce::Graphics` constructor that takes an image is declared explicit. You could declare a simple helper class like this

```auto
template <size_t n>
class GraphicsArray : std::array<juce::Graphics, n>
{
public:

    GraphicsArray (std::array<juce::Image, n>& images)
        : GraphicsArray (images, std::make_index_sequence<n>())
    {}

private:
    // Calls the explicit constructor on each image in the array
    template <size_t... Is>
    GraphicsArray (std::array<juce::Image, n>& images, std::index_sequence<Is...>)
        : std::array<juce::Graphics, n> { (juce::Graphics (images[Is]))... }
    {}
};

```

Usage: In MyClass.h declare those two members

```auto
std::array<juce::Image, 3> curvesIMG;
GraphicsArray<3> curvesGraph { curvesIMG };

```

Now each array field of curvesGraph refers to the image with the same index, you can e.g. access `curvesGraph[1]` to write to `curvesIMG[1]` and so on.

Note 1: My solution is based on `std::array` rather than old school C arrays you used. I’d encourage you to use `std::array` in modern C++ code. You can still access it via `[]` like an old array.

Note 2: The helper class above is written down from memory and was not tested 😉

---

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [October 4, 2021, 11:14pm UTC](https://forum.juce.com/t/how-to-create-graphics-array/48114/5 "2021-10-04T23:14:43Z")

</div>

Thanks for the idea.  
Best regards
