Scroll JScrollPane by dragging mouse (Java swing) -
i making map editor game working on. there jpanel in jscrollpane displays map edited. make when user holding down spacebar , dragging mouse in jpanel, jscrollpanel scroll along dragging. here have far:
panelmappanel.addmousemotionlistener(new mousemotionlistener(){ @override public void mousedragged(mouseevent e) { //gets difference in distance x , y last time listener called int deltax = mousex - e.getx(); int deltay = mousey - e.gety(); mousex = e.getx(); mousey = e.gety(); if(spacepressed){ //scroll scrollpane according distance travelled scrollpane.getverticalscrollbar().setvalue(scrollpane.getverticalscrollbar().getvalue() + deltay); scrollpane.gethorizontalscrollbar().setvalue(scrollpane.gethorizontalscrollbar().getvalue() + deltax); } } });
currently works scrolling not smooth @ all. moving mouse lot @ time fine doing small drags makes scrollpane go berserk.
any ideas how improve this?
for enjoy visual help, here editor:
addition notes (edit):
- i have tried
scrollpane.getviewport().setviewposition(new point(scrollpane.getviewport().getviewposition().x + deltax, scrollpane.getviewport().getviewposition().y + deltay));
- the dragging more fidgety when moving mouse slowly, while big movements more smooth
- i tried using scrollrecttovisible without luck
okay, ended been simpler though be...
first, don't mess jviewport
, instead, use jcomponent#scrollrecttovisible
directly on component acting contents of jscrollpane
, onto mouselistener
should attached.
the following example calculates difference between point @ user clicked , amount have dragged. applies delta jviewport
's viewrect
, uses jcomponent#scrollrecttovisible
update viewable area, simple :)
public class test { public static void main(string[] args) { new test(); } public test() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new testpane()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class testpane extends jpanel { private jlabel map; public testpane() { setlayout(new borderlayout()); try { map = new jlabel(new imageicon(imageio.read(new file("c:/treasuremap.jpg")))); map.setautoscrolls(true); add(new jscrollpane(map)); mouseadapter ma = new mouseadapter() { private point origin; @override public void mousepressed(mouseevent e) { origin = new point(e.getpoint()); } @override public void mousereleased(mouseevent e) { } @override public void mousedragged(mouseevent e) { if (origin != null) { jviewport viewport = (jviewport) swingutilities.getancestorofclass(jviewport.class, map); if (viewport != null) { int deltax = origin.x - e.getx(); int deltay = origin.y - e.gety(); rectangle view = viewport.getviewrect(); view.x += deltax; view.y += deltay; map.scrollrecttovisible(view); } } } }; map.addmouselistener(ma); map.addmousemotionlistener(ma); } catch (ioexception ex) { ex.printstacktrace(); } } @override public dimension getpreferredsize() { return new dimension(200, 200); } } }
Comments
Post a Comment