How should I link two items with MouseMovementListeners?

Posted by user1708810 on Stack Overflow See other posts from Stack Overflow or by user1708810
Published on 2012-09-29T19:33:24Z Indexed on 2012/09/29 21:37 UTC
Read the original article Hit count: 207

Filed under:
|
|
|
|

I'm working on a program that will display two "views" of the same set of items. So I need to have something set up so that when the top down view is changed, the side view is updated (and vice versa).

Here's a brief outline of the relevant code so you can get an idea of my structure so far:

public class DraggableComponent extends JComponent
{
    //Contains code for MouseMovementListener that makes the item draggable
}

public class ItemGraphic extends DraggableComponent
{
    //Code to render the graphic
}

public class Item
{
    private ItemGraphic topGraphic;
    private ItemGraphic sideGraphic;
}

I'm able to get each graphic to display fine in my GUI. I can also independently drag each graphic. I'm missing the "linking." Some ideas I've been thinking about:

  • Have one listener for the whole GUI. Loop through each Item and if the cursor is within the bounds of either graphic, move the other graphic. I'm concerned about the efficiency of this method.
  • Multiple "paired" listeners (not quite sure how this would work, but the idea is that each graphic would have a listener for the other paired graphic)

© Stack Overflow or respective owner

Related posts about java

Related posts about swing