Changing the fillALL colour from another .cpp file

Hello All,

 

I'm stumped on this...

So I created a new GUI compnent useing the Introjucer by clicking "Files"-> Right Clicking "Source"-> "Add new GUI Component". So then I name the component "test" and the Introjucer creates the "test.cpp and test.h" files. I select the test.cpp file and draw a rectangle. I get it to be visible and then create multiple instances of them in the private section of MainComponent.h and paint them using the

MainContentComponent::MainContentComponent(){} in the .cpp file.

So all is well but now I want to change the default color which i set to red in the compnents test::paint(Graphics &g) by useing

g.fillAll(Colours::red)

I want to change them from say the

MainContentComponent::MainContentComponent(){}

I can't seem to get this figured out. Do I need to add a new function to my test.h/test.cpp file?

I'm drawing a bit of a blank here but there must be a way I can do it by say doing

 

test.paint(g.fillAll(Colours::blue) or something like that but that of course doesnt work. Any tips or points in the right direction would be of great help.

By the way I know how I can say change the colour of slider components and buttons would a rectangle not provide this functionality? If I want to do this woudl I have to add this functionality?

 

-Joe.

 

If I understand you correctly, you could just create a member variable of type Colour in the test class. Then you could either access this variable directly or make getter/setters for it. So maybe a good name for the variable is Colour rectColour and a setter could be something like setRectColour(). 

Then in test::paint(), you can use that variable in the Graphics::fillAll() member function. So that would look something like this: g.fillAll(rectColour).

This sort of thing can apply in many cases. If you want to change something from another class, you probably want a variable so you can change it and use it. 

The easiest way is to use setColour and findColour.

First add an enum to the public section of your Test components declaration, in the  header file, for example

enum ColourIds
{
    fillColourId
};

Give your colour a default value in the constructor of your test component, like so

Test::Test() { setColour (fillColour, Colours::red) }

​Then In the paint method you could have something like

void Test::paint (Graphics& g)
{   
    g.fillall (findColour (fillColourId));
}

Now in the MainContentComponent you can set the colour like so

myTestComponent.setColour (Test::fillColourId, Colours::blue)

You may also need to call repaint() on myTestComponent, if you want the colour to be updated immediately, or if you just set the colour before you call addAndMakeVisible() that will do the trick to.

Cool! I like this way a lot more. That makes it very similar to how Components in JUCE work, like Slider for example. Thanks for suggesting that. Idk why, but I never thought of changing color that way. :)

Jordan, Anthony,

 

Thank you very much for you suggestions/solutions.

 

Anthony, thanks it was good to be refreshed on enumerations!

One other question....

 

If I then wanted to make my rectangle resizeable would I have to implement the ReziaebleBorderComponent or is there an easier method?

 

Thanks.

 

-Joe

Do you mean resizing it with the mouse? If yes, then that class does make that a lot easier. If you do, you should probably just make a Component that is nothing but that rectangle. So just create a Component where you draw the rectangle as the bounds the Component. I'm pretty sure ResizableBorderComponent works on a Component, do that's why you'd need to do that. It might even just be beneficial to inheret from either DrawableShape, DrawablePath, or DrawableRectangle. That way, you wouldn't need to create any extra functions to change the color or the fill out the shape. That's the kind of thing that Drawables are used for, I believe. 

(Just a quick note that you should never inherit from the Drawable classes.. I might actually add a C++11 'final' keyword to them now that such a thing exists!)

So then just to confirm if I'm useing this wrong. Going into the introjucer, createing a new GUI component and then drawing a rectangle which in turn creates a drawable class and making new objects from that class is not the intended way to use the rectangle?

Drawing a rectangle in the introjucer is/isn't a drawable shape?

Also, within the class I noticed there's no code for drawing the rectangle. Where is that code?

 

Drawing a rectangle in the GUI editor won't create a Drawable object, it just adds code to the paint() method to fill the rectangle.

If I for instance used DrawableRectangle () I would not want to create seperate variables off a single class then?  

Not clear what you mean by that.. (?)

Hi jules,

can you please add a small hint of "why" to these kind of suggestions? And also can we have these hints please in the api docs rather than in the forum? Otherwise we will never learn to use JUCE in it's full value, or at least to use it how it was intended...

Thanks

Sorry! It's just that those aren't "normal" Components, they're designed as primitive objects that each do a specific task, and shouldn't need to be extended. And if you start overriding their methods, you'll almost certainly break some aspect of their behaviour, which is quite complex in some cases.

TBH I'm not sure why anyone would want to inherit from them, I can't think of a situation where that'd be a useful thing to do.

I think I understand now, I need to create a rectangle specify it as a component then do my work that way.

 

What I was trying to communicate is the current method is I draw the rectangle in the introjucer which in turn creates a class. So if I want to make another instance of that rectangle I would for instance, create a private member for that object.

private:

my_rectangle newRectangle;

 

Which you say is simply adding code for a drawable shape to the paint method which is not the intent of a drawable shape. The intent is to simply draw graphics quickly. So we wouldnt want to use this.

What I want to do is make a component that is a rectangle that I can specify colour, redraw in size with the mouse and add other components to it. So I should use something else (drawableRectangle() ).

 

Huh? This doesn't make any sense..

I don't understand why you think the gui editor would add a Drawable of any kind? Nothing in the introjucer will add a class to draw a rectangle - like I said, it just adds code to the paint() method that calls fillRect. There are no member variables or classes or objects involved!

Ok, sorry let me reiteriate my process here. I go into the introjucer, create a new gui component and then add a rectangle in graphics.

Yes. And like I said, it adds a command to your paint() method to draw it. I don't understand what you mean about it adding a DrawableRectangle?

Ok, but there is no technical reason like you get undefined multiple inheritance problems or anything like, that I don't know...

TBH I'm not sure why anyone would want to inherit from them, I can't think of a situation where that'd be a useful thing to do.

I do those things e.g. to add personal properties to existing classes when the class represents something in a specialized context. Just wanted to know if it's dangerous or just not intended use, and your comments always make me curious and eager for explanation.

I see in the api docs no virtual functions, so I can't break anything anyway, can I?

Side note: who thought that a cell phone might become handy as torch, but it's probably one of the most downloaded apps ;-)

 

Ok, I understand that now thanks I don't want to waste too much of your time. Thanks for clearing this up.