This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Move Wheels object with MouseMotionLIstener  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want a rectangle to move to a specific location when the mouse is moved. But my code don't respond to the movement of the mouse.
The line of code i am referring to is:
Any ideas on how to get it working?


Main.java

Bat.java
 
Rancher
Posts: 3316
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the code that sets the listener?
 
Marshal
Posts: 61721
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oving to our GUIs forum

Don't use a mouse motion listener. Your rectangle will follow the mouse wherever it goes, until you leave the area of your GUI. Unless you implement the method to put the rectangle at (0, 0), which will always move it to the top left.
Use a mouse listener and implement its clicked method to attract the rectangle to your click. If you simply use the coordinates of the click (obtainable from the mouse event object), that will attract the top left corner of your rectangle to your click. Use a mouse motion adapter or a mouse adapter and make sure to use the @Override annotation.
Why have you got a mouse moved method in the bat class? I don't think that is right.
 
Danielx Anderssonx
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Where is the code that sets the listener?


What does that mean?

Campbell Ritchie wrote:oving to our GUIs forum

Don't use a mouse motion listener. Your rectangle will follow the mouse wherever it goes, until you leave the area of your GUI. Unless you implement the method to put the rectangle at (0, 0), which will always move it to the top left.
Use a mouse listener and implement its clicked method to attract the rectangle to your click. If you simply use the coordinates of the click (obtainable from the mouse event object), that will attract the top left corner of your rectangle to your click. Use a mouse motion adapter or a mouse adapter and make sure to use the @Override annotation.
Why have you got a mouse moved method in the bat class? I don't think that is right.


Well I only wanted to move to a specific location to start with, to get something to happen.
But later on I intended to make it so the rectangle followed mouse wherever it goes (in x).
Yeah I might make a seperate move class later on.
 
Norm Radder
Rancher
Posts: 3316
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What does that mean?



Take a look at the tutorial: https://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html
 
Danielx Anderssonx
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

What does that mean?



Take a look at the tutorial: https://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html


Thanks (y)
 
Campbell Ritchie
Marshal
Posts: 61721
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Danielx Anderssonx wrote:. . . But later on I intended to make it so the rectangle followed mouse wherever it goes (in x). . . ..

You might need a mouse motion listener after all with the mouse dragged method implemented.
 
Danielx Anderssonx
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to read some of the MouseListenerExamples but I don't really get it to work in my program.
The code is still basically the same but I added a MouseListener class. Would be really awesome if someone could take a few minutes and give me some feedback on what I did wrong. :/

BatMouseListener.java
 
Norm Radder
Rancher
Posts: 3316
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read the tutorial at the link I provided?

Where does your code add a mouse listener to a GUI object?  A GUI object with the focus will call any listeners that have been added to it.

Can you post the GUI code where you have added the mouse listener?
 
Danielx Anderssonx
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Did you read the tutorial at the link I provided?

Where does your code add a mouse listener to a GUI object?  A GUI object with the focus will call any listeners that have been added to it.

Can you post the GUI code where you have added the mouse listener?


Hmm I have a code for that. I guess the GUI in my case would be: or
Yeah I maybe have to read some more if it's to complicated to explain. Thanks for the link.
 
Danielx Anderssonx
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Did you read the tutorial at the link I provided?

Where does your code add a mouse listener to a GUI object?  A GUI object with the focus will call any listeners that have been added to it.

Can you post the GUI code where you have added the mouse listener?


** I meant to say that I dont have a code for that. I tried to take some code from this example:

 
Norm Radder
Rancher
Posts: 3316
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 6 adds a mouse listener to a GUI class object (this refers to the Frame object).

You need to call the same method for your GUI class with a reference to your mouse listener class.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!