• Post Reply Bookmark Topic Watch Topic
  • New Topic

Draggable graphics

 
Patrick Mala
Greenhorn
Posts: 9
Android Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a simple app that should emulate a desktop with some icons that you can drag around. I was instructed to use the MouseListener and MouseMotionListener. The icons should drag starting at the point where the mouse originally clicked down on it (ie not dragging from the top right corner).

I am drawing my icons to a jpanel, and I seem to be having two problems:
  • The hitboxes are off
  • The icon is being dragged from the top right corner, despite having setup up a mouse offset at the beginning of the drag


  • Here are the relevant classes, the Desktop.draw() method is being called from my DrawPanel




    I'll post my other classes if need be, I feel like the error is glaring at me but I have been trying to find it for two days so hopefully, I fresh pair of eyeballs might help.
     
    Tony Docherty
    Saloon Keeper
    Posts: 3137
    72
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Shouldn't your updatePosition() method be subtracting the mouse offset values.
     
    Patrick Mala
    Greenhorn
    Posts: 9
    Android Linux Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Tony Docherty wrote:Shouldn't your updatePosition() method be subtracting the mouse offset values.

    Yes, thanks! I knew it would be something silly like that.
    Also, I found that the mouse position was off because it was relative to the frame (which counted the border/titlebar). I moved the listeners to the panel itself and everything's working.
     
    Tony Docherty
    Saloon Keeper
    Posts: 3137
    72
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    My pleasure and congratulations for solving the other issue yourself.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!