Drawing charts

OK when I make iteration i<=100, there are much less lagging, works much better. But still doesn’t clear the screen

Do you ever do envGraphWindow.myPath.clear() ?

1 Like

Hah… man!!! Now I am God. Better to say you are my God.
Works perfect, grat thanks.

You’re welcome…:grin:

OK, but I am not finished :slight_smile:
How can I make my envelope graph clck/drag able, that I can set attack time and shape by dragging graph, instead of useing sliders?

I am afraid, there is a misunderstanding how the framework works:

Last line: addAndMakeVisible: you should only call this once in the constructor, where you create the subcomponents, e.g. sliders or even your custom sub components

You are not calling any of these (except addAndMakeVisible, see above). It is the opposite way. The OS will call them, and as programmer you define, what should happen if the OS needs to call them. It is called “callbacks”.

Whenever the OS thinks, it would be good to paint, it will call your implementation of void paint (Graphics& g) override; and it gives you as argument a Graphics context so you can paint on.

When your model changed (i.e. the slider was moved in your case), it is sufficient to call repaint(); This will signal the component (via the MessageThread/OS methods), that it should call paint ASAP (filling in the Graphics context, as said before).

So a usual paint would look like that:

void paint (Graphics& g) override
{
    g.fillAll (Colours::black);
    auto frame = getLocalBounds();        // adjust here, if you want to paint in a special location
    const int numPix = frame.getWidth();  // you might experiment here, if you want less steps to speed up

    float value  = 0;                               // minimum value for your graph, e.g. 0
    const float valInc = (100.0 - value) / numPix;  // e.g. 100.0 as  maximum value for your graph
    float pos = frame.getX();
    const float posInc = frame.getWidth() / numPix;

    Path p;
    p.startNewSubPath (pos, frame.getBottom() - frame.getHeight() * f(value));  // replace f(x) with your function
    for (int i=1; i < numPix; ++i)
    {
        value += valInc;
        pos += posInc;
        p.lineTo (pos, frame.getBottom() - frame.getHeight() * f(value));  // replace f(x) with your function
    }
    g.setColour (Colours::green);
    g.drawRect (frame, 1);
    g.strokePath (p, PathStrokeType (1.0));
}

Hope that helps…

1 Like

Great thanks Daniel,
now I understand much more, and finally it works. Great.