Search code examples
javamousemotionlistener

MouseMotionListener is not working with Canvas


This is my code so far, what I want to do is to add a new Object every time the mouse is moved, but the system is not even accessing the MouseEvent class after hours of thinking, I still am not able to figure the problem. Please Help!!

My main class:

  package testing;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.Random;

public class Wincall extends Canvas implements Runnable {

public static final int HEIGHT = 640, WIDTH = 1080;

private WinTest w;              
private Handler handler;
private ME me = new ME(this);

public Wincall(){

    handler = new Handler();
    w = new WinTest(WIDTH, HEIGHT, "Test", this);

}

public synchronized void run(){

    while(true){

        long now = System.currentTimeMillis();

        this.tick();  
        this.render();

        long after = System.currentTimeMillis();
        int tt = (int) (after-now);
        if(tt>5)
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        System.out.println("Time Taken in millisecs : " + tt);

    }

}

public void tick(){
    handler.tick();
}

public void render(){

    BufferStrategy bs = this.getBufferStrategy();
    if(bs == null)
    {
        this.createBufferStrategy(3);
        return;
    }
    Graphics g = bs.getDrawGraphics();

    //render

    g.setColor(Color.BLACK);
    g.fillRect(0 ,0 ,WIDTH, HEIGHT);


    handler.render(g);

    //render end

    g.dispose();
    bs.show();
}

public void addStuff(){

    handler.addObject(new TestGO(me.getX(), me.getY(), 32, 32));

}

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

My MouseEvent class:

package testing;

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class ME implements MouseMotionListener{

private int mx = 0, my = 0;
private Wincall game;

public ME(Wincall game){
    this.game = game;
}

public void mouseDragged(MouseEvent e){}

public void mouseMoved(MouseEvent e) {

    game.addStuff();
    mx = e.getX();
    my = e.getY();

    System.out.println(mx);
    System.out.println(my);

}

public int getX(){
    return mx;
}

public int getY(){
    return my;
}

}

My window class:

package testing;

import java.awt.Canvas;

import javax.swing.JFrame;

public class WinTest {

private static final long serialVersionUID = -369751247370351003L;

public WinTest(int h, int w, String title, Wincall game){

    JFrame f = new JFrame(title);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(h, w);
    f.add(game);
    f.setVisible(true);
    f.requestFocus();
    f.setResizable(false);
    f.setFocusable(true);

    game.addMouseMotionListener(new ME());

    game.run();
}

}

Solution

  • Though its not clear what you are trying to do. But you need to add the MouseMotionListener to canvas not to the JFrame. Since you are adding canvas to the JFrame, it is the canvas which should capture MouseEvents. Hence, your Wintest should probably look like this :

    public class WinTest extends Canvas {
    
    private static final long serialVersionUID = -369751247370351003L;
    
    public WinTest(int h, int w, String title, Wincall game, ME me) {
    
        JFrame f = new JFrame(title);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(h, w);
        f.add(game);
        f.setVisible(true);
        f.requestFocus();
        f.setResizable(false);
        f.setFocusable(true);
        // f.addMouseMotionListener(me);
        game.addMouseMotionListener(me);
        game.run();
     }
    }
    

    Update :

    Wincalll Class:

    public class Wincall extends Canvas implements Runnable {
    
    public static final int HEIGHT = 640, WIDTH = 1080;
    
    private WinTest w;
    // private Handler handler;
    private ME me = new ME(this);
    
    public Wincall() {
    
        // handler = new Handler();
        w = new WinTest(WIDTH, HEIGHT, "Test", this, me);
    
    }
    
    public synchronized void run() {
    
        while (true) {
    
            long now = System.currentTimeMillis();
    
            this.tick();
            this.render();
    
            long after = System.currentTimeMillis();
            int tt = (int) (after - now);
            if (tt > 5)
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
            // System.out.println("Time Taken in millisecs : " + tt);
    
        }
    
    }
    
    public void tick() {
        // handler.tick();
    }
    
    public void render() {
    
        BufferStrategy bs = this.getBufferStrategy();
        if (bs == null) {
            this.createBufferStrategy(3);
            return;
        }
        Graphics g = bs.getDrawGraphics();
    
        // render
    
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, WIDTH, HEIGHT);
    
        // handler.render(g);
    
        // render end
    
        g.dispose();
        bs.show();
    }
    
    public void addStuff() {
        System.out.println("addStuff");
        // handler.addObject(new TestGO(me.getX(), me.getY(), 32, 32));
    
    }
    
    public static void main(String[] args) {
        new Wincall();
    }
    }