【问题标题】:Optimizing Drag code优化拖拽代码
【发布时间】:2013-01-14 20:25:50
【问题描述】:

所以我找到了这段代码here,它有效,它只是在拖动时在图像中引起相当多的抖动(拖动越快,图像抖动越多)。 OP说它没有优化,因为它是一个死帖,我想我会看看这里是否有人可以提供帮助!我也尝试过来自stack 的代码,但我无法让它做任何事情。如果有人对此代码有任何建议,或者更好的解决方案,我很乐意听到!

请注意,我需要拖动的 Jpanel(paintComponent 绘制对象)位于滚动窗格内!

   //initial reference point  
   private Point mouseLocation;  
   public void mousePressed(MouseEvent evt){  
      mouseLocation = evt.getPoint();  
   }  
   public void mouseDragged(MouseEvent evt){  
      //current mouse location  
      Point newLoc = evt.getPoint();  
      //deltas  
      int deltaX = newLoc.x-mouseLocation.x;  
      int deltaY = newLoc.y-mouseLocation.y;  
      p.setLocation(p.getX()+deltaX,p.getY()+deltaY);  
      //move the reference point to the current location  
      this.mouseLocation = newLoc;  
   }  

Here 是一个显示抖动的示例程序!

【问题讨论】:

  • 您可以使用插值或高通滤波器..但是您可以将整个代码放在我的计算机上测试抖动吗?
  • 6个类700行代码!但是我昨天确实把paintcomponent 放在了here 并且在原帖中另一张海报说它引起了和我一样的问题,所以我不认为它是特定于程序的。即,如果您构建类似的东西,应该会发生抖动。我将致力于创建一个小型测试程序来展示它。

标签: java swing mouseevent draggable


【解决方案1】:

所以我在这里找到了这段代码,它有效,只是引起了很多抖动

那么我会说它不起作用。如果您仔细阅读该帖子,OP 还会声明它不起作用。

此外,按钮并没有严格跟随鼠标,所以在我看来这太可怕了。

现在,需要考虑两件事:

  1. 您正在使用evt.getPoint():该方法的值与JButton 相关。当您移动JButton 时,您无法将该方法的值与前一个方法的值进行比较(因为您的按钮正在移动)。一种简单的解决方案是将这些点相对于固定面板(例如不移动的父面板)转换。 Tadaam:现在运行流畅,按钮完美跟随鼠标。
  2. 当你使用 LayoutManager 时,你不能调用 setLocation(也不能调用 setBounds 或 setSize()),因为这是 LayoutManager 的工作,一旦他们重新布局你的容器,按钮就会被设置回来到它的原始位置(我猜你不想要)。有几种方法可以解决这个问题,但通常,最简单的一种是使用绝对定位(即,将布局设置为null)。最后,这意味着您必须执行 LayoutManager 之前所做的任何事情。

这是一个小演示(有缺陷,但演示了基本原理):

import java.awt.Component;
import java.awt.Point;

import javax.swing.SwingUtilities;

/**
 * 
 * @author Stuart.Bradley
 */
public class NewJFrame extends javax.swing.JFrame {
    private Point mouseLocation;

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this
     * method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("jButton1");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButton1MousePressed(evt);
            }
        });
        jButton1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            @Override
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jButton1MouseDragged(evt);
            }
        });
        setLayout(null);
        jButton1.setSize(jButton1.getPreferredSize());
        add(jButton1);
        setSize(300, 300);
    }// </editor-fold>

    private void jButton1MouseDragged(java.awt.event.MouseEvent evt) {
        // current mouse location
        Point newLoc = SwingUtilities.convertPoint(evt.getComponent(), evt.getPoint(), jButton1.getParent());
        // deltas
        int deltaX = newLoc.x - mouseLocation.x;
        int deltaY = newLoc.y - mouseLocation.y;
        jButton1.setLocation(jButton1.getX() + deltaX, jButton1.getY() + deltaY);
        // move the reference point to the current location
        this.mouseLocation = newLoc; // TODO add your handling code here:
    }

    private void jButton1MousePressed(java.awt.event.MouseEvent evt) {
        mouseLocation = SwingUtilities.convertPoint(evt.getComponent(), evt.getPoint(), jButton1.getParent()); // TODO add your
    }

    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        // <editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        // </editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    // End of variables declaration
}

【讨论】:

  • 谢谢你的充分解释,我不会再犯同样的错误了!
  • (仅供参考 MouseEvent#getComponent 返回事件的源组件——因此您无需进行盲投)+1
  • @GuillaumePolet 我自己通常会忘记它:P