Results 1 to 4 of 4
  1. #1
    5myl is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Question JLabel Dragging with Container as opposed to JLayeredPane

    As I am working on my chess GUI, i have reached the part of moving the pieces through the mouse clicking on a piece to select it, dragging it, and then releasing the mouse to drop it into the JPanel under it that functions as the board square. I have looked through some things online and have seen that most other people use JLayeredPane as opposed to Container. My program uses Container and I was wondering as how to produce the same results as using Container as with using JLayeredPane.DRAG_LAYER ?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: JLabel Dragging with Container as opposed to JLayeredPane

    The basic idea of dragging things around is:

    * detect mouse down and decide is something is being dragged. An instance variable should remember what is being dragged and where (the coordinates) the drag started.
    * detect mouse drag and decide, based on position, how to update your display. In your case you could update the position of the piece.
    * detect mouse up. If there is a drag in progress end it: reset the variable remembering what was being dragged, decide - based on the rules of chess - whether it was a valid move and, if so, "snap" the position of the piece into place. If it is invalid the position should probably be reset to its original position.

    -----

    Are you sure you don't want to use the drag layer? JLayeredPane *is* a container, after all. And it's one that has a notion of "z-position" (above/below). A problem with merely changing the position of a piece is that it may well disappear behind another as it is being dragged. JLayeredPane lets you solve this by:

    * putting the piece into the drag layer
    * dragging it as above
    * returning it to the container once the drag is done

    -----

    I have looked through some things online
    Other people's code is only so much good. (as you've found) As far as things online go I would recommend the How to Use Layered Panes page of Oracle's Tutorial to get a feel for how these things are used. They seem a good "fit" for what you're trying to do.

    -----

    Just an aside, but you might want to consider not shoving everything into one class. I'm thinking that the game state (which piece is in which position) might deserve a class of its own (in place of your array of panels). This class could be responsible for chess-related behaviour like deciding whether some position is a valid move for a given piece, independently of GUI stuff like painting and responding to mouse drags. As the program becomes more involved having classes with definite (and independent) responsibilities is a good way of managing the complexity.

  3. #3
    5myl is offline Member
    Join Date
    Aug 2011
    Posts
    9
    Rep Power
    0

    Default Re: JLabel Dragging with Container as opposed to JLayeredPane

    Alright, thanks for all the great info and advice :)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: JLabel Dragging with Container as opposed to JLayeredPane

    You're welcome.

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2011, 10:45 PM
  2. How to add JScrolPane to a JLayeredPane?
    By chyrl in forum Advanced Java
    Replies: 5
    Last Post: 08-30-2010, 04:28 AM
  3. Replies: 10
    Last Post: 11-27-2009, 10:52 AM
  4. Dragging JLabel
    By techbossmb in forum AWT / Swing
    Replies: 2
    Last Post: 09-22-2009, 07:19 PM
  5. Replies: 3
    Last Post: 04-21-2009, 11:01 PM

Posting Permissions

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