0

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");   
        }
    });
  • @AndrewThompson SO doesn't require SSCCE (probably because other user's projects vary in size and complexity to replicate a problem for others to test themselves). –  Nov 14 '13 at 09:55
  • @SpicyWeenie, until a problem is solved you don't know what part of the code is causing the problem. If you aren't willing to show that you have made an effort to solve your problem by creating a SSCCE then most of us are not willing to waste time guessing what you might be doing wrong. – camickr Nov 14 '13 at 16:19
  • @camickr I understand what you are saying, but I'm also not posting my entire program over one problem. It's also quite unfair to assume that I didn't didn't research myself. Lastly, SSCCE is not a requirement and if it is a problem that have stumped me, I only post the relevant code where the error occurred and any other relevant code that shows that I've attempted to take some steps to reach. If you have to guess, then sorry, but the question may not really for you, because part of programming is making educated guesses whether through conceptional theory or programmatically. –  Nov 14 '13 at 19:46
  • Nobody asked your for your entire program. That is the whold point of a SSCCE, to isolate the error and only post the relevant code that demonstrates the problem. This is part of basic problem solving. Majority of time when you do this you will find the problem yourself. You job is the make the question as simple as possible for use to help you. – camickr Nov 14 '13 at 21:08
  • @camickr Then by your own statement, I've already isolated to problem to within the above stated code as I've stated that the values (x and y) updates upon dragging, but the circle graphically isn't moving. But a gentleman has already helped me resolved the issue which was that I wasn't calculating the previous mouse position and offsetting the circle based on mouse movement. If you don't know the answer or have never really worked in this area of programming, just leave the question/problem for someone else who may understand it. Lets agree to disagree. –  Nov 14 '13 at 21:30
  • So you are saying you should NOT make it as easy as possible for people to answer your questions. Again, until an actual answer is given, you don't now what information it needed. You got lucky this time. Until you actually spend time answering some questions, you don't have the same perspective as those who have done it for a while. I wouldn't be spending time answering questions if I didn't have experience regarding the question. My suggestions are designed to get you the best possible help. – camickr Nov 14 '13 at 21:55

1 Answers1

1

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().

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045