Search code examples
c++juce

How to round only one corner of a button in JUCE C++?


Having a hard time to round a single corner because our Designer insists.

Current code...

graphics.fillRoundedRectangle(bounds.toFloat(), cornerRadius);

Solution

  • You can use juce::Path::addRoundedRectangle() for this. Here is an example:

    class RoundedButton : public juce::Button
    {
    public:
        explicit RoundedButton (const juce::String& buttonName) : Button (buttonName) {}
    
    protected:
        void paintButton (
            juce::Graphics& g,
            [[maybe_unused]] bool shouldDrawButtonAsHighlighted,
            [[maybe_unused]] bool shouldDrawButtonAsDown) override
        {
            static constexpr float cornerSize = 10.f;
    
            juce::Path p;
            p.addRoundedRectangle (
                0,
                0,
                static_cast<float> (getWidth()),
                static_cast<float> (getHeight()),
                cornerSize,
                cornerSize,
                false,
                false,
                true, // Only curve bottom left
                false);
    
            g.setColour (juce::Colours::red);
            g.fillPath (p);
        }
    };