简体   繁体   English

图形对象上的MouseListener

[英]MouseListener on a graphics object

I'm feeling quite stupid. 我感觉很傻。 But what is the reason why this simple piece of code doesn't change the ellipse's color? 但是,为什么这段简单的代码不改变椭圆的颜色是什么原因呢?

Basically I want to add a mouse listener to the oval - a graphic object. 基本上,我想将鼠标侦听器添加到椭圆形-图形对象。 when the mouse cursor is in oval, the oval changes its color. 当鼠标光标位于椭圆形时,椭圆形会更改其颜色。 But this code doesn't change at all... This code is for testing only. 但是这段代码根本不会改变...该代码仅用于测试。

public class Help extends JFrame{

    public static void main(String [] agrs){
        Help h = new Help();
        h.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        h.add(new Help_Option());
        h.setSize(2000, 1000);
        h.setVisible(true);
    }
}

class Help_Option extends JComponent implements MouseListener{
    Ellipse2D ellipse = new Ellipse2D.Double(0, 0, 1000, 500);
    Color c = Color.BLACK;    

    public Help_Option(){
        this.addMouseListener(this);
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(Color.BLUE);
        g2d.draw(ellipse);

        g2d.setColor(c);
        g2d.fill(ellipse);        
    }

    public void setColor(Color c){
        this.c = c;
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {}

    @Override
    public void mouseReleased(MouseEvent e) {}

    @Override
    public void mouseEntered(MouseEvent e) {
        if (ellipse.contains(e.getX(), e.getY()) ) {
            setColor(Color.GREEN);
            repaint();
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}

you are adding a MouseListener and waiting for mouseEntered events. 您正在添加MouseListener并等待mouseEntered事件。 These are fired when the mouse enters a Component , not a region of it. 当鼠标进入Component而不是其区域时,将触发它们。 Try entering the component's boundary where the ellipse is shown and observe. 尝试输入显示椭圆的组件边界并观察。

What you need is a MouseMotionListener , so that you can observe the mouse pixel by pixel; 您需要一个MouseMotionListener ,以便您可以逐像素观察鼠标。 use the mouseMoved or mouseDragged events. 使用mouseMovedmouseDragged事件。

You might still need to listen for mouseEntered or mouseExited events, as MouseMotionEvent s are only fired while inside the component's boundary, so you might miss the mouse exiting the component while still inside the ellipse. 您可能仍然需要侦听mouseEnteredmouseExited事件,因为MouseMotionEvent仅在组件边界之内触发,因此您可能会错过在椭圆形内部退出组件的鼠标。

A good and simple way for debugging this is adding prints inside the event handler. 调试此错误的一种好方法是在事件处理程序中添加打印内容。 You would then see that the handler was called, but only once or a few times, and not when you move the mouse within the component. 然后,您将看到只调用了一次或几次处理程序,而不是在组件内移动鼠标时调用了该处理程序。


class Help_Option extends JComponent implements MouseListener, MouseMotionListener {
    Ellipse2D ellipse = ...;

    public Help_Option() {
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
    }

    public void mouseMoved(MouseEvent e) {
        if (ellipse.contains(e.getX(), e.getY()) ) {
            //mouse is inside the ellipse
        } else {
            //mouse is outside the ellipse
        }
    }

    public void mouseExited(MouseEvent e) {
        //mouse is outside the ellipse
    }

    //more method stubs
}

..if i use the boundary of the ellipse, it would be a rectangle, so whenever my mouse enter the rectangle-but-not-in-ellipse, the color will change ..如果我使用椭圆的边界,那将是一个矩形,因此,每当我的鼠标进入矩形而不是椭圆形时,颜色都会改变

See: 看到:

  • Shape.contains(x,y) : Tests if the specified coordinates are inside the boundary of the Shape , as described by the definition of insideness. Shape.contains(x,y) :测试指定的坐标是否在Shape的边界内,如内部定义所描述。
  • Shape.contains(Point2D) : Tests if a specified Point2D is inside the boundary of the Shape , as described by the definition of insideness. Shape.contains(Point2D) :测试指定的Point2D是否在Shape的边界内,如内部定义所描述。

See also this answer for a demo showing collisions between 2 shapes. 另请参阅此答案以获取演示2个形状之间发生碰撞的演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM