Results 1 to 8 of 8
  1. #1
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

    Default 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. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

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

    Quote Originally Posted by Snake006 View Post
    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
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

    Default 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. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Snake006 is offline Senior Member
    Join Date
    Jun 2013
    Posts
    129
    Rep Power
    0

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

    nevermind I got it

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

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

    Of course. All it took was a little math.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Having Trouble Setting Position of Objects on Canvas
    By kathmandu in forum New To Java
    Replies: 15
    Last Post: 07-15-2013, 10:39 PM
  2. Closing secondary window from button in primary window
    By KoncentriK in forum New To Java
    Replies: 4
    Last Post: 02-26-2012, 02:24 AM
  3. Mouse position in applet window not screen
    By 0circle0 in forum Java Applets
    Replies: 1
    Last Post: 07-22-2011, 01:33 PM
  4. Getting window position
    By bison in forum New To Java
    Replies: 3
    Last Post: 11-23-2010, 02:01 AM
  5. get position in string from caret position
    By helloworld111 in forum AWT / Swing
    Replies: 5
    Last Post: 02-19-2009, 02:36 AM

Posting Permissions

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