Rectangle Position?

Hi,
I have a rect class, which has four member variables(x, y, w, h).
I have five rect object which I have to position horixontally one after another. The five rect object are added in an array list and I have to position them with for loop.
The problem is, their hight are same but their width are different. I have to position the second rect on the width of the first rect.
Any input will be apreciated.
Thank you.

:open_mouth: I’m working on something similar atm. Basically it will be a UI kit. I’m just now making a List thingy, which would order things like you describe here :smiley:

No idea how I will do it yet.

Like this?

[tr][td] [/td][td] [/td][td] [/td][td] [/td][/tr]


List<Rectangle> list;

//set 1st rectangle position outside of loop

for(int i = 1; i < list.size(); i++) { //<-- note the i = 1, not 0
    list.get(i).x = list.get(i - 1).x + list.get(i - 1).w;
}

This will stack the rectangles horizontally, starting from the 1st rectangle.

This is an example window in java of 640x480:

http://php.scripts.psu.edu/djh300/cmpsc221/pixels.jpg

As you see, 0,0 is the top left, so if you place a button which 100 pixels wide and 50 high, its member variables are: (0, 0, 100, 50)
If you want to place a button next to it, you have to place it at 100,0 which gives: (100, 0, 100, 50)
If you place it at 0,0 as well, its two buttons at the same place.

I hope the image can make it clear for you.

@BurntPizza: I have tryied it, I am getting error, ArrayList.get(Unknown Source).

@Herzan: ok, did not know that. I will try it.

Thank you for the input.

@pizza

I don’t mean to be mean or anything, but this guy seems to be one of those university students who have no idea what they are doing and need to do their home work.

Hey Troll,
If you are thinking like that then let me clarify, I am not a student, I am just a hobbyist and trying to make something :slight_smile: thats why I have to came here.

Regards,
Sheo.

Your error is because you are trying to access a value outside the range of the list. Are you actually adding the rectangles to the array through the use of [icode].add(rectangle)[/icode]?

He just copied the code and expected 20 buttons in a horizontal line, I actually had to keep myself from quoting it :slight_smile: But SheoKa it would be smart to read a book first or so because if you don’t know about initializing you are far from making a game.

Actually I am trying to render texts on screen. I have a bitmap font which coordinates are saved in a xml file, I have to parse that xml and get the coordinates for all the characters and render the texts on screen. Anyways I have solved it. It is working as expected. Thank you for your helps.

Regards,
Sheo.