java - JPanel flickering with mouseMoved method -
i have simple jpanel i'm using display image. want functionality i'm able pan image dragging it. have (note code have compiles , runs properly; code below heavily truncated idea of i'm doing):
public class data2dpanel extends jpanel { public data2dpanel(data2d data) { // set image this.image = data2d.data2dtobufferedimage(data); // set mouse listener data2dmouseadapter data2dmouseadapter = new data2dmouseadapter(); this.addmouselistener(data2dmouseadapter); this.addmousemotionlistener(data2dmouseadapter); } private class data2dmouseadapter extends mouseadapter { @override public void mousedragged(mouseevent e) { if (swingutilities.isleftmousebutton(e)) { switch (actionstate) { case pan: xoffset = xoffsetinit + e.getx()-xposinit; yoffset = yoffsetinit + e.gety()-yposinit; paintcomponent(getgraphics()); break; } } } } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2 = (graphics2d)g; g2.setrenderinghint(renderinghints.key_interpolation, renderinghints.value_interpolation_nearest_neighbor); // paint image g2.drawimage(image,x,y,width,height,this); } } } the problem there lot of flickering. i've checked stackoverflow/google , seems lot of flickering problems because people override paint method instead of paintcomponent method. i've checked isdoublebuffered , returns true. intuitively, feel maybe mousedragged method updating paintcomponent() keep up, figured double buffering should still prevent flickering occuring. question if there's inherently wrong approach , if there's standard or elegant solution this.
paintcomponent(getgraphics()); should repaint(). compounded problems.
- you never want make call
getgraphics()custom painting.graphicsobject used paint should 1 provided in paint method. - you should never call
paintxxxtry , "force" repaint of component. should callrepaint(), allowrepaintmanagerhandle update , paint cycle
Comments
Post a Comment