GPT-chat and JUCE

I asked ChatGPT to write that juce::MeterComponent class, thats what I got:

Please write the juce::MeterComponent class for the JUCE C++ framework, which is an animated level meter.

#include <JuceHeader.h>

class MeterComponent : public juce::Component
{
public:
    MeterComponent()
    {
        setBufferedToImage(true);
    }

    void paint(juce::Graphics& g) override
    {
        const auto width = getWidth();
        const auto height = getHeight();

        g.fillAll(juce::Colours::black);

        const auto level = getLevel();

        const auto barWidth = width / 10.0f;
        const auto barHeight = height * level;

        for (int i = 0; i < 10; ++i)
        {
            const auto x = i * barWidth;
            const auto y = height - barHeight;

            g.setColour(juce::Colour::fromHSV(i / 10.0f, 1.0f, 1.0f, 1.0f));
            g.fillRect(x, y, barWidth, barHeight);
        }
    }

    void setLevel(float newLevel)
    {
        if (newLevel != level)
        {
            level = newLevel;
            repaint();
        }
    }

    float getLevel() const noexcept
    {
        return level;
    }

private:
    float level = 0.0f;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MeterComponent)
};

This MeterComponent class displays a horizontal level meter with 10 bars. The level is set with the setLevel() method and the current level is returned by the getLevel() method. The meter is animated by calling repaint() whenever the level changes.

I compiled the code and set the level to 0.5, this is the result

Screenshot 2022-12-03 at 12.59.46

6 Likes