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.

  1. you never want make call getgraphics() custom painting. graphics object used paint should 1 provided in paint method.
  2. you should never call paintxxx try , "force" repaint of component. should call repaint() , allow repaintmanager handle update , paint cycle

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -