Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help dragging a jpanel please...

 
brenda stewart
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i finally got my jpanel to be draggable. my problem is when i click inside the panel to drag it, the panel's x,y start point jumps to my cursor .... i can only drag the panel from the upper left corner. (if i click in the middle of the panel, the entire panel jumps over to where my cursor is on its x,y start point...then i can drag it) how
can i drag the panel from wherever i click on it, instead of it jumping like that?

here is the code i'm using in my mouse motion listener to drag it...
void mypanel_mouseDragged(MouseEvent e) {
mypanel.setLocation(mypanel.getX()+e.getX(),
mypanel.getY()+e.getY());
}
any help appreciated, thanks...
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply, you need to have a reference point, like the point where the mouse has been pressed the first time. Then on every mousevent, you can compute the deltas between the actual mouse location and the original one and move the panel's origin by that delta. Just after that move the reference point to the current one. In terms of code:

Of course this code is not optimized at all, improvements are possible too. But you get the idea...
[ March 29, 2002: Message edited by: Valentin Crettaz ]
 
brenda stewart
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much for the info... i can now drag the panel from anywhere i click on it but now it redraws very very slow. the mouse cursor starts out on the jpanel when i drag it but when i move it the cursor moves 2 inches to every 1 inch the jpanel moves. it also flickers really bad. i do have the panel double buffered.
any ideas on how to make it smooth when it moves?
thanks again...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic