Jump to content

Java: Can't access mouseMoved() or keyPressed()

Go to solution Solved by fizzlesticks,

One last question: How do I do the same for the KeyListener()?

 

Here is the complete code: http://pastebin.com/uzqYPnXK

KeyListener should only fire events for whatever component is focused. Since you can't focus a JPanel you'll need to add it to the frame. Try adding the keylistener to game instead of canvas.

 

Edit: Also, arrow keys won't fire keyTyped events. That will only go off for keys that have some kind of display like a-z, 0-9, enter, backspace etc. You'll need to use keyPressed if you want to use the arrow keys.

We are programming pinball and have a problem with the event handling.

 

Our intention is to call the  mouseMoved()  and later the keyPressed() method to move the paddle in the game. 

However, the compiler never access these methods.

 

 

Mouse moved method:

 // Movement of the paddle when Mouse is moved    public void mouseMoved(MouseEvent e)    {         System.out.println("Mouse moved!");               paddle_posX = e.getX() - paddle_width / 2;         if (paddle_posX < 0 || e.getX() < 0)        {          paddle_posX = 0;        }         if (paddle_posX + paddle_width >= canvas.getWidth())        {          paddle_posX = canvas.getWidth() - paddle_width;        }    }

Here is the whole project code: http://pastebin.com/uzqYPnXK

How can we access these event methods from drawFrame()?

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

Its a listener and all listeners work the same way. In order to get the callback you need to register your listener to the thing you want to listen to.

 

So I suspect what you want to do is call addMouseListener with an object of your mouse listener implementation (probably "this") to an instance of your canvas object and then you will get called when the mouse moves over the canvas.

Link to comment
Share on other sites

Link to post
Share on other sites

Its a listener and all listeners work the same way. In order to get the callback you need to register your listener to the thing you want to listen to.

 

So I suspect what you want to do is call addMouseListener with an object of your mouse listener implementation (probably "this") to an instance of your canvas object and then you will get called when the mouse moves over the canvas.

 

Do you mean like this:

    public void drawFrame(int frame_drawSizeX, int frame_drawSizeY)     {        System.out.println("Entering drawFrame();");        //<editor-fold defaultstate="collapsed" desc=" Declaration and Definition of JFrame ">        game.setType(Window.Type.UTILITY);        game.setResizable(false);        game.setSize(frame_drawSizeX, frame_drawSizeY);        game.setLocationRelativeTo(null);        game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        game.setVisible(true);                canvas.addMouseListener((MouseListener) canvas);        //</editor-fold>

We want to use the mouse and the keyboard as input for our game.

How should we implement these listeners?

public class Game implements KeyListener, MouseListener{

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

canvas.addMouseListener((MouseListener) canvas);

I think it would be

 

game.addMouseListener(this);

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

I think it would be

 

game.addMouseListener(this);

    public void drawFrame(int frame_drawSizeX, int frame_drawSizeY)     {        System.out.println("Entering drawFrame();");        //<editor-fold defaultstate="collapsed" desc=" Declaration and Definition of JFrame ">        game.setType(Window.Type.UTILITY);        game.setResizable(false);        game.setSize(frame_drawSizeX, frame_drawSizeY);        game.setLocationRelativeTo(null);        game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        game.setVisible(true);                game.addMouseListener(this);        //</editor-fold>

Still nothing happens...

Do you need to call it once or in a loop?

If we move the code to the actionPerformed() we get an error because of an event in an event (eventception  :lol: )..

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

Sorry, it should be

 

canvas.addMouseMotionListener(this);

 

You'll need to make Game implement MouseMotionListener and add the mouseDragged method.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Sorry, it should be

 

canvas.addMouseMotionListener(this);

 

You'll need to make Game implement MouseMotionListener and add the mouseDragged method.

 

First, why MouseMotionListener() and not MouseListener()? What's the difference between the two?

 

It still doesn't work, I implemented MouseMotionListener and added all abstract methods...

public class Game implements KeyListener, MouseListener, MouseMotionListener{    //Declaration of the variables [...]            public void drawFrame(int frame_drawSizeX, int frame_drawSizeY)     {        System.out.println("Entering drawFrame();");        //<editor-fold defaultstate="collapsed" desc=" Declaration and Definition of JFrame ">        game.setType(Window.Type.UTILITY);        game.setResizable(false);        game.setSize(frame_drawSizeX, frame_drawSizeY);        game.setLocationRelativeTo(null);        game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        game.setVisible(true);                canvas.addMouseListener(this);        //</editor-fold>
    (at)override    public void mouseDragged(MouseEvent me) {        System.out.println("New method");    }

The console output:

run:Entering drawFrame();BUILD SUCCESSFUL (total time: 11 seconds)

The new code: http://pastebin.com/uzqYPnXK

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

First, why MouseMotionListener() and not MouseListener()? What's the difference between the two?

 

It still doesn't work, I implemented MouseMotionListener and added all abstract methods...

public class Game implements KeyListener, MouseListener, MouseMotionListener{    //Declaration of the variables [...]            public void drawFrame(int frame_drawSizeX, int frame_drawSizeY)     {        System.out.println("Entering drawFrame();");        //<editor-fold defaultstate="collapsed" desc=" Declaration and Definition of JFrame ">        game.setType(Window.Type.UTILITY);        game.setResizable(false);        game.setSize(frame_drawSizeX, frame_drawSizeY);        game.setLocationRelativeTo(null);        game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        game.setVisible(true);                canvas.addMouseListener(this);        //</editor-fold>
    (at)override    public void mouseDragged(MouseEvent me) {        System.out.println("New method");    }

The console output:

run:Entering drawFrame();BUILD SUCCESSFUL (total time: 11 seconds)

The new code: http://pastebin.com/uzqYPnXK

MouseMotionListener listens for mouse movement, MouseListener listens for button pushes.

And you need to change addMouseListener to addMouseMotionListener

Or just do both since you'll probably want the other one later anyway.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

You only want to do this once, not every frame.

 

Then the position it is placed now should be ok...

The loop is not used for the MouseMotionListener()...

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

MouseMotionListener listens for mouse movement, MouseListener listens for button pushes.

And you need to change addMouseListener to addMouseMotionListener

Or just do both since you'll probably want the other one later anyway.

 

Thank you (again)!!

 

Now everything works :lol:

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

MouseMotionListener listens for mouse movement, MouseListener listens for button pushes.

And you need to change addMouseListener to addMouseMotionListener

Or just do both since you'll probably want the other one later anyway.

 

One last question: How do I do the same for the KeyListener()?

 

Here is the complete code: http://pastebin.com/uzqYPnXK

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

One last question: How do I do the same for the KeyListener()?

 

Here is the complete code: http://pastebin.com/uzqYPnXK

public class Game implements KeyListener, MouseMotionListener{    //Declaration of the variables        public void drawFrame(int frame_drawSizeX, int frame_drawSizeY)     {        System.out.println("Entering drawFrame();");        //<editor-fold defaultstate="collapsed" desc=" Declaration and Definition of JFrame ">        game.setType(Window.Type.UTILITY);        game.setResizable(false);        game.setSize(frame_drawSizeX, frame_drawSizeY);        game.setLocationRelativeTo(null);        game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        game.setVisible(true);                canvas.addMouseMotionListener(this);        canvas.addKeyListener(this);        //</editor-fold>
(at)Override    public void keyTyped(KeyEvent ke)     {        System.out.println("Key Pressed!");        if(ke.getKeyCode() == KeyEvent.VK_RIGHT)         {          paddle_posY += 3;           }        if(ke.getKeyCode() == KeyEvent.VK_LEFT)         {          paddle_posX -= 3;           }    }

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

One last question: How do I do the same for the KeyListener()?

 

Here is the complete code: http://pastebin.com/uzqYPnXK

KeyListener should only fire events for whatever component is focused. Since you can't focus a JPanel you'll need to add it to the frame. Try adding the keylistener to game instead of canvas.

 

Edit: Also, arrow keys won't fire keyTyped events. That will only go off for keys that have some kind of display like a-z, 0-9, enter, backspace etc. You'll need to use keyPressed if you want to use the arrow keys.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

KeyListener should only fire events for whatever component is focused. Since you can't focus a JPanel you'll need to add it to the frame. Try adding the keylistener to game instead of canvas.

 

It works! :D

 

Thanks for your help!

Of course, it is completly normal to have a GPU at 90°c all the time <_< 

 

Java, Sysadmin, Network Engineer, Project Management

 

 

Love you all - thanks for helping me! ^_^

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×