Results 1 to 4 of 4
- 01-02-2012, 01:18 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 9
- Rep Power
- 0
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 ?
- 01-02-2012, 02:16 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
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
-----
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.
- 01-02-2012, 06:31 AM #3
Member
- Join Date
- Aug 2011
- Posts
- 9
- Rep Power
- 0
Re: JLabel Dragging with Container as opposed to JLayeredPane
Alright, thanks for all the great info and advice :)
- 01-02-2012, 06:35 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
Similar Threads
-
Getting Child Nodes With XPath.evaluate on an Element as Opposed to a Node
By baiguai in forum XMLReplies: 5Last Post: 12-21-2011, 11:45 PM -
How to add JScrolPane to a JLayeredPane?
By chyrl in forum Advanced JavaReplies: 5Last Post: 08-30-2010, 05:28 AM -
what is java logically as opposed to syntactically
By katon in forum New To JavaReplies: 10Last Post: 11-27-2009, 11:52 AM -
Dragging JLabel
By techbossmb in forum AWT / SwingReplies: 2Last Post: 09-22-2009, 08:19 PM -
[SOLVED] Coordinates container --> in parent container.
By Cleite in forum AWT / SwingReplies: 3Last Post: 04-22-2009, 12:01 AM
Bookmarks