Changing a components color with text buttons


#1

I am trying to write a simple application to play with the awesome graphics rendering code, which sets the color of my component based on user input, like pushing a text button. I wasn’t sure how to start this, can anyone please help? Am I thinking down the right path here with code sample below, or is this way off?

void ColorComponent::paint (Graphics& g)
{
       // Component initially painted with green and black checkerboard 
       g.fillCheckerBoard (0, 0, getWidth(), getHeight(), 50, 50, Colours::green, Colours::black);
}

void ColorComponent::buttonClicked (Button* buttonThatWasClicked)
{

    if (buttonThatWasClicked == textButton)
    {
       // would like to switch checkerboard colors upon the pressing of this button
       // but the compile fails
       g.fillCheckerBoard (0, 0, getWidth(), getHeight(), 50, 50, Colours::black, Colours::green);
    }
    else if (buttonThatWasClicked == textButton2)
    {
       // Upon pushing this text button, the component is painted white
       g.fillAll (Colours::white);
    }

}

#2

…yep, as your compiler would certainly tell you, it’s total gibberish.

All painting happens in your paint method - you can’t just scatter random bits of drawing code around all over your program! When it’s time to paint, the system creates a suitable Graphics object, and passes it to paint(), which draws with it, and returns. If someone presses a button and you need to repaint something, you call repaint(), which tells the system that it should call paint() again sometime in the future.


#3

Thanks for confirming that this was gibberish and I was going down the wrong path.

So I understand that repaint() tells the system to call paint() at some point in the future. How do I control what colours are used for the painting, when textButton1 versus textButton2 is clicked? Within my buttonlistener, I see that I can call repaint() but I don’t know how to set the colours so that they are painted correctly.


#4

When the button is pressed set the contents of a variable with a value that you then interpret in the paint() routine. Here is some psuedo-code to point you in the right direction:

in button handler:

if (button1 clicked)
    fillOperation = eDoCheckerBoard;
else if (button2 clicked)
    fillOperation = eDoWhite;

in paint:

switch (fillOperation)
{
    case eDoCheckerBoard : g.fillCheckerBoard(....); break;
    case eDoWhite : g.fillAll(...); break;
}

#5

Thanks, I can’t believe I missed that, I was thinking paint was static and that I couldn’t do checking in paint() for some kind of flag. This is what I came up with, I really appreciate the help.

void ColorComponent::buttonClicked (Button* buttonThatWasClicked)
{

    if (buttonThatWasClicked == textButton)
    {
        colorFlag = 1;
        repaint();
    }
    else if (buttonThatWasClicked == textButton2)
    {
        colorFlag = 2;
        repaint();
    }

}


void ColorComponent::paint (Graphics& g)
{

        if (colorFlag == 1) {

                g.fillAll (Colours::white);
        }
        if (colorFlag == 2) {

                g.fillAll (Colours::black);

        }
}