Graphics & Child question


#1

Hello all.

I'm rather confused by the way the gui building works

As the tutorial states, I have a child component within a parent instance, I draw a line using the function like so:

void paint (Graphics& g) override
    {
        g.setColour (Colours::yellow);
        g.drawLine (getWidth() * 0.1, getHeight() / 2, getWidth() * 0.9, getHeight() / 2, 1);
    }
 

Yet the line is drawn according to the resized function within the parent component:

void SectionControls::resized() 
{
    floor.setBounds (50, 50, 200, 10);
}

So, what's the point of drawing the line to a specified size in the first place? If you put in concrete values rather than relative to 'getWidth' and 'getHeight', the line isn't drawn at all, which is odd. Why does the placement and length of the line change, but not the width?

Apologies for the noob questions.


#2

Well I'm rather new to JUCE myself, but I believe I have the answer.

The best way to see the space your components are taking within each other is to add 
    g.setColour (Colours::red);
    g.drawRect (0,0,getWidth(),getHeight(), 2);

to the end of your paint methods, changing the colors if necessary.

The point of drawing the line in the first place is that it is "inside" of your child component. Your child component owns that line, so to speak. The line is drawn relative to where the child component is. That means you can move the child component around, and the line will not stay in the same place but rather will move with the child.

The line will be drawn to concrete values without a problem, try inserting a break point at g.drawLine() and checking the values of getWidth() and getHeight(), and then replacing them with those integer values.

The width of the line isn't changing because it is that last parameter you have set to 2. Set it to something else for a wider/thinner line. See [url=http://learn.juce.com/doc/classGraphics.php#a664643f9fef3c8b37b0620c28ec4bb76]this.[/url]


#3

I think that makes sense, still seems an odd way about it to me but I'm sure there's a damn good reason for it. I'm plunging into Juce & C++ as much as I can, most of the time it's like cudgelling yourelf round the head but it's amazing when things happen and make sense!


#4

I learned C++ a few years ago and I have to say it is the most powerful, but still high-level language. That means it is just, well, a little bit harder than languages like C# but you get a handsome performance reward.

If you want to learn more about the way GUI's work and why the component model is the easiest to use, well, there just isn't a simple way to explain. It all boils down to the component model working well with object-oriented programming.

For example lets say you want to model a tree in your application. Well, then you could add a few properties to your tree -- how tall it is, how wide it is, how many roots it has, and how many branches. You could add a timer to it and tell it to grow 10 pixels taller and grow an extra branch and root every 30 seconds.

Now let's say you wanted to view your tree. Well then, you'd have to add a paint method and call it from the main window (the parent). Let's say you add a whole bunch more stuff -- like clicking on a branch removes that branch, etc.

But now lets say you wanted to do something silly -- be able to move your tree. Well, you'd want it to just simply be painted in a different place -- but you also want all the logic and hitboxes to move with it. Well CRAP, everything is hard-coded, you have to change all those values too. Maybe we should just leave it where it was coded.

Here is where components step in. You make the tree a component, and all the logic inside of it becomes automatically relative to its upper left hand corner, which the parent can change. Now you can just call tree->setBounds(x,y,width,height); and put it anywhere! On top of that, you can even create another component of lets say, apples, and have the tree own and display them. Then the apples will move around with your tree too.

That's the idea at least. It's worked really well since 1989, so I've given up fighting it :)


#5

Indeed it does seem that the rewards will be great. I understand the concept and usefulness of the component object, it was just that particular implementation of it that confuddled me. Cracking on again now!


#6

I'm still finding the relationship between the settings of the originally drawn line (.drawLine), and the settings of the resize (.setBounds) very odd. Is there a specific way that they affect each other? I think I'm going to have to accept that I just don't get this bit and hope that it makes more sense later on. Thank you everyone for your all help on this.