Question

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");   
        }
    });
Was it helpful?

Solution

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.

In mousePressed(),

  • Initialize the last mouse location.

  • Optionally, mark the target as selected.

  • Invoke repaint() to display the selected appearance.

In mouseDragged(),

  • Update the target location by the difference between the new and old mouse locations.

  • Update the last mouse location to the current mouse location.

  • Invoke repaint().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top