Results 1 to 4 of 4
  1. #1
    ankitmcgill is offline Member
    Join Date
    Nov 2008
    Posts
    22
    Rep Power
    0

    Default Snapping algorithm

    ok heres what i am trying to do.

    i have a Jpanel is which i load an image. then the user presses 'generate' button to generate several instances of transparent JPanels with a white border. the user can drag these panels to position them over the image.

    What i want to do is make a snap mode in which when activated will automatically snap these transparent panels to each others edges.

    off the top of my head one idea that came was to x and y coordinate of all the 4 edges of each panel in an some sort of data structure and whenever the user drags a panel , constantly keep checking its x,y with the stored values and snap when required. however this seems like a not so elegant approach and with a lot of panels would be cumbersome.

    Just wondering if anyone has any ideas/suggestions that could do it in a better way.

    thanks
    ankit

  2. #2
    ankitmcgill is offline Member
    Join Date
    Nov 2008
    Posts
    22
    Rep Power
    0

    Default

    bumping....

    anyone ?

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    you could probably set it up so when the item being dragged is released it would only check then and move it to the closest edges. I'm not familiar with JPanel so I can't give too solid an example.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    What i want to do is make a snap mode in which when activated will automatically snap these transparent panels to each others edges.
    There are always a lot of options in building something like this. Are these panels to be in a straight line or a rectangluar shape when snapped?

    one idea that came was to x and y coordinate of all the 4 edges of each panel in an some sort of data structure and whenever the user drags a panel , constantly keep checking its x,y with the stored values and snap when required. however this seems like a not so elegant approach and with a lot of panels would be cumbersome.
    Agreed. The components will keep the coordinates for you: you can call getBounds on each one and get the origin with the x, y fields. You can get the ne corner x value with either getMaxX or r.x + r.width (x and width fields of Rectangle).
    The fun part is to design for fluid resizing. At the snap you could compute the size and location of the desired layout (line, rectangle of rows/columns or other layout) of the panels and adjust their locations relative to this rectangle with either the setBounds or the setLocation method called on each panel.

Similar Threads

  1. Help of Thread and some Algorithm
    By raghuylin in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-13-2008, 02:07 PM
  2. O(log n) algorithm help !!!!!!
    By itseeker87 in forum New To Java
    Replies: 8
    Last Post: 09-09-2008, 05:12 PM
  3. Help with algorithm
    By susan in forum New To Java
    Replies: 1
    Last Post: 07-13-2007, 10:26 PM
  4. Help me with this algorithm
    By Marcus in forum Advanced Java
    Replies: 3
    Last Post: 07-02-2007, 01:30 PM
  5. Help with Algorithm
    By Daniel in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 05:51 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
  •