getBounds() - Rectangle<int / float> issue

getBounds() always return Rectangle<int>, even if component was defined by floats, like that:
myComboBox.setBounds(0.5f, 0.05f, 10.5f, 10.5f);

Of course there is method Rectangle<int>::toFloat() but it rounds the coordinates and size.

Why is that? Do I need to write my own class to handle that?
I mean class that stores float values of fX=0.5f, fY=0.5f etc. Because getX(), getY() etc. also always gives me int value.

Or is there any solution? I can’t find anything in documentation.

For any help thanks in advance.

There is no Component::setBounds method which takes floats… they’re converted to integers by the compiler in your example.


1 Like

If you use setBounds with float values, they will be casted to int anyways, as there’s only a method which takes ints:

Edit: Rail was faster :wink:

Heh! That’s true. Component::setBounds() doesn’t take floats as parameters. I have not noticed that, sorry.
But still void Rectangle< ValueType >::setBounds() takes ValueTypes which can be floats.
And I can use such Rectangle<float> for example to draw elements in paint() method. So why can’t I draw whole component with such rectangle?
It’s easy to get mess in code if you forgot which elements are defined by Rectangle<int> and which one by <float>.
Have you got any advices how to avoid such miss mess? Of course I can try to avoid using <float>. But sometimes it’s realy handy for me.

Use this:

Rectangle<float> yourRectangle;

Component just doesn’t take Rectangle<float>.

If you’re using rectangle for abstract positioning/layout/computation/etc, use floats!

If you’re using rectangle to set the bounds of a component, it needs to be ints. Screens are usually defined by an integer number of pixels (or points), so that makes sense. There’s nothing wrong with using Rectangle with floats to compute bounds accurately, then round it to int when actually setting component bounds, so long as you know it is being cast/rounded.

OK. Great thanks for your support.