Thread: how to position 8 objects even on a window

1. Senior Member
Join Date
Jun 2013
Posts
129
Rep Power
0

how to position 8 objects even on a window

Hi there. I am trying to draw add flowers on a graphics window. I can do this just fine, however the position starts from the far left and goes to the far right. I would like to position the 8 flowers in the center of the screen but can't seem to get the right formula to do so. Here is what I have so far to draw the flower, which is simply a GOval nothing complex:
Java Code:
```box.flowers=8;
for (int xpos1 = 10; xpos1 < getWidth() - 40; xpos1 += getWidth() / box.flowers) {
box.drawFlowers(xpos1, 400);
}```
The box size of the window is 800 by 600. Ideally I would like to place x amount of flowers spread across the middle of the screen. How might I do this???

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

Re: how to position 8 objects even on a window

Your starting X position should be something like (screenWidth - flowerWidth*8)/2. So if screenWidth is 800 and flowerWidth is 50
you start drawing at x = 200. Adding the total flowerWidth of 400 to starting x leaves 200 at the end. But I haven't tried this so you
may need to play around with it.

Regards,
Jim

3. Re: how to position 8 objects even on a window

It helps to take a piece of paper and draw out what you want to do. Mark the gaps between the shapes with lines. Then do some math to compute the location of the first shape and the distances from shape to shape.

4. Re: how to position 8 objects even on a window

Originally Posted by Snake006
but can't seem to get the right formula to do so.
What happened to math 101? Suppose the width of one flower is w, the width of n flowers is n*w; Suppose you have a window width W, so there is spare room of W-n*w; you have to divide that spare room to the left and right of the n flowers, so that width is (W-n*w)/2 and that is the position of the leftmost flower; the next flower starts at (W-n*w)/2+w etc.

kind regards,

Jos

5. Senior Member
Join Date
Jun 2013
Posts
129
Rep Power
0

Re: how to position 8 objects even on a window

Ok I am trying use a formula like that and I can manage to get the objects pretty much in the middle. The problem I am running into now is that when I have 8 flowers (the width of each flower is always 20). Instead of getting 8 flowers printed onscreen I am only getting 5. I would assume that the screen isn't big enough to display all of them, except that in previous attempts I have been able to see other objects by maximizes the screen(even though the rest of the screen is just white space).

6. Re: how to position 8 objects even on a window

How wide is the area where you are displaying the shapes?
How wide is each shape?
Where is the first shape shown?
What is the gap between the shapes?

7. Senior Member
Join Date
Jun 2013
Posts
129
Rep Power
0

Re: how to position 8 objects even on a window

nevermind I got it

8. Re: how to position 8 objects even on a window

Of course. All it took was a little math.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•