点击时使用MouseListener移动图像

时间:2015-10-11 04:46:43

标签: java swing mouseevent mouselistener

当用户点击/按住鼠标按钮时,我试图用鼠标移动图像。当用户将鼠标按下(用鼠标实时更新)时,我已设法做到这一点,但是,当我点击图像时,图像将其位置调整到更新区域,这不是我的意思想要它做。如果用户点击它,我希望图像移动的唯一时间是用户第二次再次点击。因此,假设用户点击位于(0,0)的图像,如果用户再次点击屏幕上的其他位置,则该位置现在位于(x,y)。

这就是我所拥有的:

[UltiDev Web Server Explorer][1]

更新代码:

@Override
public void mouseClicked(MouseEvent e) {

    clickCount++;
    if(clickCount % 2 == 0){
        p.setLocation(e.getX(), e.getY());//p is just a panel that contains the img
        repaint();
    }
    System.out.println("mouse clicked...");

}

这更接近于工作,但是,如果您单击屏幕上的任何位置(在clickCount%2 == 0之后),则图像将移动。

1 个答案:

答案 0 :(得分:2)

调用mouseClicked时,确定以前是否点击了某些内容,如果是,请将对象移动到当前位置,如果不是,请检查用户是否点击了某些内容是否可移动将其分配给变量(稍后用于检查)。

移动对象后,将引用设置为null

private JPanel clicked;

@Override
public void mouseClicked(MouseEvent e) {

    if (clicked != null) {
        clicked.setLocation(e.getX(), e.getY());
        clicked = null;
    } else {
        // Figure out if any panel was clicked and assign
        // a reference to clicked
    }

}

可运行的示例......

所以,听起来你正试图支持点击和拖动重定位,这有点......很难,因为两者所需的鼠标操作都不同,所以你需要监控多个状态并制作关于你可能处于的状态的决定,例如......

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MouseTest {

    public static void main(String[] args) {
        new MouseTest();
    }

    public MouseTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(null);
            JPanel panel = new JPanel();
            panel.setBackground(Color.RED);
            panel.setSize(50, 50);
            panel.setLocation(50, 50);
            add(panel);

            MouseAdapter ma = new MouseAdapter() {
                private Point offset;
                private Point clickPoint;
                private JPanel clickedPanel;

                @Override
                public void mousePressed(MouseEvent e) {
                    // Get the current clickPoint, this is used to determine if the
                    // mouseRelease event was part of a drag operation or not
                    clickPoint = e.getPoint();
                    // Determine if there is currently a selected panel or nor
                    if (clickedPanel != null) {
                        // Move the selected panel to a new location
                        moveSelectedPanelTo(e.getPoint());
                        // Reset all the other stuff we might other was have set eailer
                        offset = null;
                        clickedPanel = null;
                    } else {
                        // Other wise, find which component was clicked
                        findClickedComponent(e.getPoint());
                    }
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    // Check to see if the current point is equal to the clickedPoint
                    // or not.  If it is, then this is part of a "clicked" operation
                    // meaning that the selected panel should remain "selected", otherwise
                    // it's part of drag operation and should be discarded
                    if (!e.getPoint().equals(clickPoint)) {
                        clickedPanel = null;
                    }
                    clickPoint = null;
                }

                @Override
                public void mouseDragged(MouseEvent e) {
                    // Drag the selected component to a new location...
                    if (clickedPanel != null) {
                        moveSelectedPanelTo(e.getPoint());
                    }
                }

                protected void findClickedComponent(Point p) {
                    Component comp = getComponentAt(p);
                    if (comp instanceof JPanel && !comp.equals(TestPane.this)) {
                        clickedPanel = (JPanel) comp;
                        int x = p.x - clickedPanel.getLocation().x;
                        int y = p.y - clickedPanel.getLocation().y;
                        offset = new Point(x, y);
                    }

                }

                private void moveSelectedPanelTo(Point p) {
                    if (clickedPanel != null) {
                        int x = p.x - offset.x;
                        int y = p.y - offset.y;
                        System.out.println(x + "x" + y);
                        clickedPanel.setLocation(x, y);
                    }
                }

            };

            addMouseListener(ma);
            addMouseMotionListener(ma);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}