【问题标题】:Flickering while dragging in Swing在 Swing 中拖动时闪烁
【发布时间】:2011-11-30 16:50:31
【问题描述】:

我有这样的代码:

public class DpDropTargetListener implements DropTargetListener {
   public void dragOver(final DropTargetDragEvent dtde) {
      ...
      if (dtde.getLocation().equals(container.getLastLocation())) {
         return;
      }
      ...     
      Rectangle visRect = container.getVisibleRect();
      container.paintImmediately(visRect.x, visRect.y, visRect.width, visRect.height);

      //prepare the image to paint, and paint it
      ...
      Graphics2D gr = (Graphics2D) container.getGraphics();            
      gr.drawImage(container.getDragImage(), AffineTransform.getTranslateInstance(
         x, y), null);
      ...
   }
}

它应该在拖动时绘制指定的图像。但是当我拖动它时,这个图像会闪烁。 我应该如何纠正才能停止闪烁?

【问题讨论】:

    标签: java swing drag-and-drop graphics2d


    【解决方案1】:

    覆盖容器的paintComponent() 方法。在拖动期间设置容器中的图像和位置并调用普通repaint();

    【讨论】:

    • 我将paintImmediately() 替换为repaint()。覆盖容器的paintComponent() 方法。没有任何改变。
    猜你喜欢
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 2023-03-25
    • 1970-01-01
    • 2011-11-25
    • 2014-07-12
    相关资源
    最近更新 更多