This week's book giveaway is in the XML and Related Technologies forum.
We're giving away four copies of Java XML & JSON and have Jeff Friesen on-line!
See this thread for details.
Win a copy of Java XML & JSON this week in the XML and Related Technologies 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Move Wheels object with MouseMotionLIstener  RSS feed

 
Ranch Hand
Posts: 85
  • 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: 3353
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the code that sets the listener?
 
Marshal
Posts: 62803
203
  • 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.
 
Daniel Andersson
Ranch Hand
Posts: 85
  • 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: 3353
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
 
Daniel Andersson
Ranch Hand
Posts: 85
  • 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: 62803
203
  • 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.
 
Daniel Andersson
Ranch Hand
Posts: 85
  • 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: 3353
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?
 
Daniel Andersson
Ranch Hand
Posts: 85
  • 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.
 
Daniel Andersson
Ranch Hand
Posts: 85
  • 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: 3353
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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!