0

だから私はここでこのコードを見つけました、それは機能します、それはそれがドラッグされたときに画像にかなりのジャダリングを引き起こします-(あなたが速くドラッグするとより多くの画像が揺れます)-。OPはそれが最適化されていないと言いました、そしてそれは死んだ投稿なので、私はここの誰かが助けることができるかどうか見るだろうと思いました!スタックからのコードも試しましたが、何も実行できませんでした。誰かがこのコード、またはより良い解決策について何か提案があれば、私はそれを聞いてみたいです!

注目すべきは、ドラッグする必要のある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;  
   }  

これがジャダリングを示すサンプルプログラムです!

4

1 に答える 1

3

だから私はここでこのコードを見つけました、それは機能します、それはただかなり多くのジャダリングを引き起こします

それなら、うまくいかないと思います。また、投稿を注意深く読むと、OPはそれが機能しないことも示しています。

さらに、ボタンは厳密にはマウスに追従しないので、私の意見では恐ろしいだけです。

ここで、考慮すべき2つのことがあります。

  1. 使用しているevt.getPoint():そのメソッドの値は。に相対的JButtonです。移動するときJButtonに、そのメソッドの値を前のメソッドと比較することはできません(ボタンが移動しているため)。簡単な解決策の1つは、これらのポイントを、移動していない親などの固定パネルに相対的に変換することです。Tadaam:スムーズに動作し、ボタンはマウスに完全に追従します。
  2. LayoutManagerを使用する場合、これはLayoutManagerの仕事であるため、呼び出すことはできませんsetLocation(setBoundsまたはsetSize()も)。コンテナが再レイアウトされるとすぐに、ボタンは元の場所に戻されます(私はあなたが望んでいないと思います)。これを解決する方法はいくつかありますが、通常、最も簡単な方法は絶対配置を使用することです(つまり、レイアウトをに設定します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
}
于 2013-01-30T23:42:13.803 に答える