0

I am trying to calculate the distance I move a mouse within a box, I am using the MouseEvent.getX() and MouseEvent.getY() to calculate the start position of the mouse and then I have another method saving the position and calculating the distance the mouse has moved. Here is some code I wrote to demonstrate this.

BlankArea.addMouseMotionListener(new MouseMotionListener(){
            @Override
            public void mouseMoved(MouseEvent mm){
                System.out.println("Position (" + mm.getX() + ", " 
                                                + mm.getY() + ")");
                savePosition(mm.getX(), mm.getY());
            }

            @Override
            public void mouseDragged(MouseEvent md) {
                System.out.println("Position (" + mm.getX() + ", " 
                                                + mm.getY() + ")");
                savePosition(mm.getX(), mm.getY());
            }
        });

My Question is: The Java API states that getX() & getY() returns the horizontal & vertical coordinate relative to the source component respectively. What is the source component? What unit is the output coordinate measured in?

4

2 回答 2

4

欢迎来到 Stack Overflow... 源组件实际上是您为其编写侦听器的组件,输出坐标以像素为单位...

我希望这会有所帮助...如果您仍有疑问,请询问他们...

于 2013-02-10T17:48:19.280 回答
1

要回答您的问题:

1)什么是源组件?

就像 Mr.777 说的那样,它是您添加侦听器的组件,所以在您的示例中它是BlankArea.

2) 输出坐标的测量单位是什么?

正如 777 先生所说,它们以像素为单位,原点 (0, 0) 位于组件的左上角。

我建议在线阅读一些 Java 教程。这是查看 Java 坐标系Coordinates (Java Tutorial)的好页面。

于 2013-02-10T18:17:51.907 回答