Search code examples
javaswinggraphicsawtgraphics2d

MouseDragging still firing even after releasing


Hello and thanks in advance,
i am working with Graphics2D for a casino game (Roulette), so i am trying to add motion to the chips of the casino (The money), so for that i am using MouseDragged events and as a test i am working with only 1 ellipse.

code below

package roulette;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.swing.JPanel;
import javax.swing.Timer;

public class RouletteInterface extends JPanel{

    private List<Shape> money = new ArrayList<>();


    public RouletteInterface() {

        createEllipseGrap();
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }



    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g4d = (Graphics2D) g.create();
        paintEllipseGrap(g4d, g);
        g4d.dispose();
    }

    protected void createEllipseGrap() {
        Ellipse2D elipse = new Ellipse2D.Double(100, 100, 30, 30);
        money.add(elipse);
        addMouseListener(new moneyMouseListener());
        addMouseMotionListener(new moneyMouseListener());
    }

    protected void paintEllipseGrap(Graphics2D g3d, Graphics g) {

        g3d.setColor(Color.BLUE);
        g3d.fill(money.get(0));
    }

    private class moneyMouseListener extends MouseAdapter {
        int dragging;
        private int x;
        private int y;

        @Override
        public void mousePressed(MouseEvent e) {
            if(money.get(0).contains(e.getPoint())) {
                x = e.getX();
                y = e.getY();
                dragging = 0;
            } else {
                return ; 
            }

        }

        @Override
        public void mouseDragged(MouseEvent e) {
            if(dragging == 0) {
            x = e.getX();
            y = e.getY();
            Ellipse2D elipse = new Ellipse2D.Double(x, y, 30, 30);
            money.set(0, elipse);
            repaint();
            } else {

            }
        }

        @Override
        public void mouseReleased(MouseEvent m) {
            dragging = 1;
            repaint();
        }


    }

}



public class principal{



    public static void main(String[] args) {
        new principal();

    }

    public principal() {
        JFrame frame = new JFrame();
        frame.add(new RouletteInterface());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

What's the problem?
The MouseDragged event is still firing even after i release the mouseclick so the circle is still moving with my cursor when i click and drag on another side of the window


Solution

  • Your problem is that you are adding two different instances of your moneyMouseListeners as MouseListener and as MouseMotionListener:

        addMouseListener(new moneyMouseListener());
        addMouseMotionListener(new moneyMouseListener());
    

    You would have to do it like that:

        moneyMouseListener mListener = new moneyMouseListener();
        addMouseListener(mListener);
        addMouseMotionListener(mListener);
    

    PS.: When using a variable like your "dragging" variable that is only used to assign a "1" or a "0" you should use a boolean variable with "true" and "false" ;)