javacanvasawtmousemotionlistener

MouseMotionListener does not transfer coordinates to the Canvas


I want the coordinates from the Mouse class to go to the Paint class in the paint method. And that the square moved with the movement of the mouse. But the coordinates are not transmitted.

g.drawRect(mouse.getX(), mouse.getY(), 10, 10);

mouse.getX(), mouse.getY() starts only at the beginning. and after does not change.

Class Draw Points

public class DrawPoints
{
    Frame f;
    KeyBoard key;
    Mouse mouse;
    Paint c;

    public void GUI()
    {
        f = new Frame("DrawPoints");
        f.setSize(300, 300);

        mouse = new Mouse(); // edit

        c = new Paint(mouse); // edit
        c.setSize(300, 300);
        c.setBackground(Color.WHITE);

        c.addMouseListener(mouse);
        c.addMouseMotionListener(mouse);

        key = new KeyBoard();
        c.addKeyListener(key);

        f.add(c);
        f.pack();
        f.setVisible(true);

        f.addWindowListener(new WindowAdapter()
        {
            @Override
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
    }
}

Class Paint

class Paint extends Canvas
{
    Mouse mouse; // edit

    public Paint() // added
    {
        mouse = new Mouse();
    }

    @Override
    public void paint(Graphics g)
    {
        g.drawRect(mouse.getX(), mouse.getY(), 10, 10);
        repaint(); // added
    }
}

Class Mouse

class Mouse implements MouseListener, MouseMotionListener
{   
    private int x;
    private int y;

    public int getX()
    {
        return this.x;
    }

    public int getY()
    {
        return this.y;
    }

    // MouseMotionListener
    @Override
    public void mouseMoved(MouseEvent e)
    {
        x = e.getX();
        y = e.getY();
        //System.out.println(e.getX() + " " + e.getY());
    }

    @Override
    public void mouseDragged(MouseEvent e)
    {
        System.out.println("mouseDragged");
    }

Solution

  • I dont know the other classes but i think problem is you are created different mouse objects. Move your new Mouse() to before new Paint() and change new Paint to Paint(mouse). DrawPoints class:

    public class DrawPoints
    {
        Frame f;
        KeyBoard key;
        Mouse mouse;
        Paint c;
    
        public void GUI()
        {
            f = new Frame("DrawPoints");
            f.setSize(300, 300);
    
            mouse = new Mouse();
    
    
            c = new Paint(mouse);
            c.setSize(300, 300);
            c.setBackground(Color.WHITE);
            c.addMouseListener(mouse);
            c.addMouseMotionListener(mouse);
    
            key = new KeyBoard();
            c.addKeyListener(key);
    
    
    
            f.add(c);
            f.pack();
            f.setVisible(true);
    
            f.addWindowListener(new WindowAdapter()
            {
                @Override
                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }
            });
        }
    }}
    

    Paint class: Change Paint() to Paint(Mouse mouse) and new Mouses() line to this.mouse = mouse;

    class Paint extends Canvas
    {
        Mouse mouse;
    
        public Paint(Mouse mouse)
        {
            this.mouse = mouse;
        }
    
        @Override
        public void paint(Graphics g)
        {
            g.drawRect(mouse.getX(), mouse.getY(), 10, 10);
        }
    }