Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用鼠标侦听器拖动对象_Java_User Interface_Mouseevent_Mouselistener - Fatal编程技术网

Java 使用鼠标侦听器拖动对象

Java 使用鼠标侦听器拖动对象,java,user-interface,mouseevent,mouselistener,Java,User Interface,Mouseevent,Mouselistener,我正在尝试创建一个程序,使用户能够在空间中拖放椭圆。我可以拖放,但在第二次跑步时我又试了一次之后,到处都是椭圆形的跳跃。我想知道是否有人知道为什么会这样?我错过什么了吗?多谢各位 public class MoveOval extends JFrame { private Ellipse2D node = new Ellipse2D.Float(200,200,80,120); private Point offset; private int preX,preY; private Image

我正在尝试创建一个程序,使用户能够在空间中拖放椭圆。我可以拖放,但在第二次跑步时我又试了一次之后,到处都是椭圆形的跳跃。我想知道是否有人知道为什么会这样?我错过什么了吗?多谢各位

public class MoveOval extends JFrame {

private Ellipse2D node = new Ellipse2D.Float(200,200,80,120);
private Point offset;
private int preX,preY;
private Image dbImage;
private Graphics dbg;
Adapter ma = new Adapter();

public static void main(String args[]){
    JFrame frame = new MoveOval();
    frame.setSize(600,600); 
    frame.setVisible(true);
}

public MoveOval(){
    super("Move Oval");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    addMouseListener(ma);
    addMouseMotionListener(ma);

}
private class Adapter extends MouseAdapter{
    public void mousePressed(MouseEvent e){
        if(node.contains(e.getPoint())){
            preX = node.getBounds().x-e.getX();
            preY = node.getBounds().y-e.getX();
            offset = new Point(preX, preY);
        }
    }
    public void mouseDragged(MouseEvent e){
        if(node.contains(e.getPoint())){
            updateLocation(e);
        }
    }
    public void mouseReleased(MouseEvent e) {
           offset=null;
      }

}

public void updateLocation(MouseEvent e){
    Point to = e.getPoint();
    to.x += offset.x;
    to.y += offset.y;

    Rectangle bounds = node.getBounds();
    bounds.setLocation(to);
    node.setFrame(bounds);

    repaint();
}

public void paint(Graphics g){
    dbImage=createImage(getWidth(), getHeight());
    dbg = dbImage.getGraphics();
    paintComponent(dbg);
    g.drawImage(dbImage, 0, 0, this);
}
public void paintComponent(Graphics g){
    Graphics2D gd = (Graphics2D)g.create();
    gd.setColor(Color.blue);
    gd.fill(node);

    }
}

实际上,这是一个非常简单的错误,很容易纠正

public void mousePressed(MouseEvent e){
        if(node.contains(e.getPoint())){
            preX = node.getBounds().x-e.getX();
            preY = node.getBounds().y-e.getX(); // <- That's the bad guy.
            offset = new Point(preX, preY);
        }
    }
public void鼠标按下(MouseEvent e){
if(node.contains(例如getPoint())){
preX=node.getBounds().x-e.getX();

pore=node.getBounds().y-e.getX();//如果您创建并发布一个有效的(请查看链接以了解重要的详细信息)则更好。另外,你不应该直接在JFrame的绘画方法中画画,因为这会带来麻烦,特别是当你不调用任何超级方法来打破绘画链条时。谢谢你!!我不敢相信我没有看到。:)这种事情偶尔也会发生在我身上,当我真的很累的时候。有时,你会错过一些简单的错误。无论如何,如果您将此标记为正确答案,我将不胜感激。@HuyTrinh:您将希望接受并支持Mark的答案。有关详细信息,请阅读帮助网站的本节:。请注意,您仍然不应使用JFrame,而应使用JPanel的paintComponent方法进行绘制,并且您仍应记住调用super的绘画方法。1+这个答案。