Java 优化拖动代码
所以我发现这段代码很有效,它只会在拖动图像时在图像中引起相当多的抖动(拖得越快,图像抖动越大)。OP说它没有经过优化,因为它是一个死柱,我想我会看看这里是否有人能帮忙!我也尝试了来自的代码,但我无法让它做任何事情。如果有人对此代码有任何建议,或者有更好的解决方案,我很乐意听到 需要注意的是,我的Jpanel(paintComponent绘制对象)需要在滚动窗格中拖动Java 优化拖动代码,java,swing,mouseevent,draggable,Java,Swing,Mouseevent,Draggable,所以我发现这段代码很有效,它只会在拖动图像时在图像中引起相当多的抖动(拖得越快,图像抖动越大)。OP说它没有经过优化,因为它是一个死柱,我想我会看看这里是否有人能帮忙!我也尝试了来自的代码,但我无法让它做任何事情。如果有人对此代码有任何建议,或者有更好的解决方案,我很乐意听到 需要注意的是,我的Jpanel(paintComponent绘制对象)需要在滚动窗格中拖动 //initial reference point private Point mouseLocation;
//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;
}
是一个显示抖动的示例程序
所以我在这里发现了这个代码,它是有效的,它只会引起相当多的抖动
那么我会说它不起作用。如果你仔细阅读这篇文章,OP也会说它不起作用
而且,这个按钮并没有严格地跟随鼠标,所以在我看来,这太可怕了
现在,需要考虑两件事:
evt.getPoint()
:该方法的值相对于JButton
。移动JButton
时,无法将该方法的值与上一个方法的值进行比较(因为按钮正在移动)。一个简单的解决方案是将这些点相对于固定面板(例如不移动的父面板)进行转换。塔达姆:它现在工作得很顺利,而且按钮完全跟随你的鼠标setLocation
(也不能调用setBounds或setSize()),因为这是LayoutManager的工作,一旦他们重新布局容器,按钮将被设置回其原始位置(我猜您不需要)。有几种方法可以解决这个问题,但通常最简单的方法是使用绝对定位(即,将布局设置为null
)。最后,这意味着您必须执行LayoutManager之前所做的任何操作导入java.awt.Component;
导入java.awt.Point;
导入javax.swing.SwingUtilities;
/**
*
*@作者斯图尔特·布拉德利
*/
公共类NewJFrame扩展了javax.swing.JFrame{
私人点鼠标定位;
/**
*创建新表单NewJFrame
*/
公共NewJFrame(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。警告:不要修改此代码。此方法的内容
*方法始终由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
jButton1=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setText(“jButton1”);
jButton1.addMouseListener(新java.awt.event.MouseAdapter(){
@凌驾
public void mousePressed(java.awt.event.MouseEvent evt){
按下JButton1Mouse(evt);
}
});
jButton1.addMouseMotionListener(新java.awt.event.MouseMotionAdapter(){
@凌驾
public void mouseDragged(java.awt.event.MouseEvent evt){
JButton1MooseDrawed(evt);
}
});
setLayout(空);
jButton1.setSize(jButton1.getPreferredSize());
添加(jButton1);
设置大小(300300);
}//
私有void jbutton1MouseDrawed(java.awt.event.MouseEvent evt){
//当前鼠标位置
Point newLoc=SwingUtilities.convertPoint(evt.getComponent()、evt.getPoint()、jButton1.getParent());
//三角洲
int deltaX=newLoc.x-鼠标位置.x;
int deltaY=newLoc.y-mouseLocation.y;
setLocation(jButton1.getX()+deltaX,jButton1.getY()+deltaY);
//将参照点移动到当前位置
this.mouseLocation=newLoc;//要在此处添加处理代码:
}
私有void jButton1MousePressed(java.awt.event.MouseEvent evt){
mouseLocation=SwingUtilities.convertPoint(evt.getComponent()、evt.getPoint()、jButton1.getParent());//要添加
}
/**
*@param args
*命令行参数
*/
公共静态void main(字符串参数[]){
/*设置Nimbus的外观和感觉*/
//
/*
*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。有关详细信息,请参阅
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
/*创建并显示表单*/
invokeLater(new Runnable()){
@凌驾
公开募捐{
新建NewJFrame().setVisible(true);
}
});
}
//变量声明-不修改
私有javax.swing.JButton jButton1;
//变量结束声明
}
所以我找到了这个
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
}