在Swing Panel当中,通常情况下要重载paintComponent方法来重绘Panel上面的内容。 现在遇到一个特殊情况,我想让Panel在 mouseMoved事件当中重绘Panel的部分区域。由于绘制整个Panel的代价比较大,如果刷新整个Panel,界面会闪烁的特别严重而且CPU资源损耗严重,所以,我想只重绘Panel的特定部分。我试过两种方法,1)不通过paintComponent来重绘,而是另外写一个方法来完成。结果可想而之是Panel根本不能重绘。2)通过paintComponent来重绘,在paintComponent方法当中,用标志位来判断只做部分重绘。结果Panel其他部分空白,这是因为程序没有重绘其他部分。请教高手,如何让Java Swing Panel一部分重绘,同时还要显示其他部分。 万分感激!!

解决方案 »

  1.   

    在paintComponent中的第一句调用super.paintComponent(g);
      

  2.   

     void repaint(int x, int y, int width, int height) 
              重绘组件的指定矩形区域。  void repaint(long tm, int x, int y, int width, int height) 
              在 tm 毫秒内重绘组件的指定矩形区域。
    看看这两个方法!是Component类中的
      

  3.   

    参考:http://topic.csdn.net/u/20080315/22/c4277753-613d-48e4-9b46-27bebe8d8000.html
      

  4.   

    也可用setClip方法public abstract void setClip(int x,
                                 int y,
                                 int width,
                                 int height)
    Sets the current clip to the rectangle specified by the given coordinates. This method sets the user clip, which is independent of the clipping associated with device bounds and window visibility. Rendering operations have no effect outside of the clipping area.