用鼠标拖动时形状位置不会重绘 [英] Shape position wont redraw when dragged with mouse

查看:109
本文介绍了用鼠标拖动时形状位置不会重绘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在画布上绘制了一个实心圆,我正在尝试通过鼠标的单击和拖动方法使其移动.我已经设法检查了鼠标指针是否在圆的范围内,并且当我拖动鼠标时,存储圆位置的变量会按需更新,但是圆本身不会随着我的拖动而重绘(最多会闪烁).我的问题是最后我覆盖了mouseDragged() .

I have a filled circle drawn on a canvas and I'm trying to get it to move based on a click and drag method with the mouse. I've managed to checked whether the mouse pointer is within the bounds of the circle, and when I drag the mouse, the variable storing the position of the circle updates as it should, but the circle itself is not redrawing as I'm dragging (the most it will do is flicker). My problem is at the end where I'm overriding mouseDragged().

getCanvas().addMouseListener(new MouseAdapter()
    {   
        @Override
        public void mouseClicked(MouseEvent event)
        {
            super.mouseClicked(event);
            Point mousePosition = event.getPoint();

            if (_circle.getShape1().contains(mousePosition))
                Main.debugLabel.setText("Clicked"); 
        }

        @Override
        public void mouseReleased(MouseEvent event)
        {
            super.mouseReleased(event);

            _circle.isDraggable = false;
            Main.debugLabel.setText("Released");
        }

        @Override
        public void mousePressed(MouseEvent event)
        {
            super.mousePressed(event);

            int button = event.getModifiers();

            if (button == InputEvent.BUTTON1_MASK)
            {
                _circle.isDraggable = true;
                Main.debugLabel.setText("Pressed");
            }
        }       
    });


    getCanvas().addMouseMotionListener(new MouseAdapter()
    {
        @Override
        public void mouseDragged(MouseEvent event)
        {
            super.mouseDragged(event);
            Point mousePosition = event.getPoint();
            if (_circle.isDraggable)
            {   
                _circle.posX = mousePosition.x;
                _circle.posY = mousePosition.y;

                Main.debugLabel.setText("Dragging " + _circle.posX);
                getCanvas().repaint();
            }   
        }
        @Override
        public void mouseMoved(MouseEvent event)
        {
            super.mouseMoved(event);

            Point mousePosition = event.getPoint();
            if (_circle.getShape1().contains(mousePosition))
                    Main.debugLabel.setText("Within Bounds");

            else if (!_circle.getShape1().contains(mousePosition) && !_circle.isDraggable)
                Main.debugLabel.setText("Out of Bounds");   
        }
    });

推荐答案

如此示例所示,一种方法是维护两个 Point实例.一个保存鼠标的最后一个位置.另一个拥有所需的目标位置;两者都在组件相对坐标中.

As shown in this example, one approach is to maintain two Point instances. One holds the last mouse location; the other holds the desired target location; both are in in component-relative coordinates.

mousePressed()中,

  • 初始化最后一个鼠标位置.

  • Initialize the last mouse location.

(可选)将目标标记为选中.

Optionally, mark the target as selected.

调用repaint()以显示选定的外观.

Invoke repaint() to display the selected appearance.

mouseDragged()中,

  • 通过新旧鼠标位置之间的差异更新目标位置.

将最后一个鼠标位置更新为当前鼠标位置.

Update the last mouse location to the current mouse location.

调用repaint().

这篇关于用鼠标拖动时形状位置不会重绘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆