gpt4 book ai didi

java - 优化拖动代码

转载 作者:行者123 更新时间:2023-12-02 00:02:15 25 4
gpt4 key购买 nike

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

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

   //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是一个显示抖动的示例程序!

最佳答案

So I found this code here, it works, it just causes quite a lot of juddering

那么我会说它不起作用。如果你仔细阅读这篇文章,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
}

关于java - 优化拖动代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14613871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com