0

I'm making a game in which we have to use 'Catchers' to catch balls which fall from the top of the window. Catches can only move left/right.

Example: http://puu.sh/xeq8

Which direction should I look/head into if I want to make it so that I can move a 'catcher' with a mouse?

Right now, I have a catcher that uses the keyboard - I used KeyListener however I am uncertain for the mouse.

Ideally, I'd like the catcher to move left/right when the mouse moves in the JPanel? Or something of that sort would be ideal.

Andrew Thompson
169k42 gold badges224 silver badges441 bronze badges
asked May 28, 2012 at 5:33

2 Answers 2

5

Use a MouseMotionListener :

myPanel.addMouseMotionListener(new MouseAdapter() { 
 public void mouseMoved(MouseEvent me) { 
 //move the catcher
 //use me.getX() to have the horizontal position of the mouse
 //eg : catcher.setX(me.getX())
 } 
}); 
answered May 28, 2012 at 5:36
Sign up to request clarification or add additional context in comments.

Comments

3

Use MouseMotionListener to be notified of the position of mouse, I think you also need to click to capture the falling balls for that use MouseListener or MouseAdapter.

When your clicks your MouseListener will be notified and when they move their mouse poistion your MouseMotionListener callback will be triggered.

  1. How to work with MouseListener
  2. How to work with MouseMotionListener
answered May 28, 2012 at 5:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.