Detect a mouse click on specific entity in an arrayList
Hi,
Im a beginner in Java and I'm creating a simple game. I have an arrayList of entities (cars in my case), I need to be able to detect a mouse click on an individual object (car) in the array. I need to do this so I can change the speed of the car if it has been pressed. Im not sure how to go about doing this, hope somebody can help me out here.
Thanks.
Re: Detect a mouse click on specific entity in an arrayList
Hello Gnasher,
Your cars each need a "collision region". It can be a rectangular area or a circular area. Once you have that, it is quite easy to compare the mouse coordinate with a car "collision region" to determine if the click was inside the region or outside of it. I would say it's easier with a rectangular region as you simply need some comparison.
Hope it helps
JeffB
Re: Detect a mouse click on specific entity in an arrayList
JeffB is correct.
If you want more help please provide more details of your Car class eg does it extend a Swing component.
Re: Detect a mouse click on specific entity in an arrayList
Thanks for the help both.
So far I have a base entity class, that is used for collisions and movement. Then, I have a car entity class that extends the base entity. In the base entity class I already have a collision region because the aim of the game is to stop the cars colliding, which is working. I just dont understand how to go about getting it to work with mouse position and that, because I've just started with Java and dont understand that much.
Im not sure what my next step is...
Re: Detect a mouse click on specific entity in an arrayList
How are you drawing your cars on the screen. For example: Are these car objects Swing/AWT objects which you are manually positioning or are you overriding a JPanel's paintComponent method to draw them yourself?
Re: Detect a mouse click on specific entity in an arrayList
Oh sorry. Im using AWT to draw the images.
Re: Detect a mouse click on specific entity in an arrayList
So what AWT component does your car class (or whatever draws your car object) inherit from?
Re: Detect a mouse click on specific entity in an arrayList
Sorry, the game class extends Canvas.
Re: Detect a mouse click on specific entity in an arrayList
So do you override the paint method in canvas and draw your cars?
If that is the case add a MouseListener to the Canvas (to whatever you've called your class that extends Canvas). When the mouse listener is notified of a mouse click the mouse event will have the location of the click so it's just a matter of determining which car is at that location.
It is easier if each object that needs to be drawn on the screen is a Component or sub class of it. Then you get a lot of the click detection handling for free as you can add a MouseListener to each car component. You can also override the contains method to limit the detection to the shape of your vehicle as opposed to a rectangle. This is also a more OO approach to solving the problem.
Re: Detect a mouse click on specific entity in an arrayList
Thanks.
Could you please explain how I would go about adding a mouse listener to each component?
At the moment I create each car in a separate method (one for each lane of the roads), I create the car in the method and add it to an array to be drawn. In the method that creates the car, could I add a mouse listener there, like addMouseListener((MouseListener) this);? And then have a mouse clicked event that would alter the speed of the car that was clicked?
Am I along the right lines here?
Re: Detect a mouse click on specific entity in an arrayList
Yes that's about the sum of it.
Quote:
could I add a mouse listener there, like addMouseListener((MouseListener) this)
You could do this, personally I'm not a big fan of this approach as it doesn't expand well. I prefer to have an anonymous MouseListener class to handle the event.
If all the cars use the same logic for changing their speed (and I guess they do) then have a single mouse listener object and add the same object to each Car. If they have different logics ie Lorry's may be different to Cars etc then you may need different MouseListeners.
The mouse event that is passed to the listener has a getSource() method that will return the particular object that was clicked on. Cast the returned object to a Car and call it's setSpeed method (or whatever you do to it).
Re: Detect a mouse click on specific entity in an arrayList
Ok, im having a few problems now. Ive added a mouse listener class like this:
Code:
private class mouseInputHandler implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
Object carClicked = e.getSource();
if (carClicked == "????") {
}
}
}
But how do I get the specific car that was clicked? And, when I have got the car that was clicked, how would I alter the speed of it, as I choose the speed of the car when it was made.
Also, when I use this: addMouseListener((MouseListener) this); to add a mouse listener to the object, the game crashes "GameName.Game cannot be cast to java.awt.event.MouseListener".
Im unsure as of what to do next now. Confused.
Re: Detect a mouse click on specific entity in an arrayList
Quote:
But how do I get the specific car that was clicked?
I've already told you how to do this.
Quote:
And, when I have got the car that was clicked, how would I alter the speed of it, as I choose the speed of the car when it was made.
Well, presumably each Car object knows the speed it is travelling at and has methods to get the speed and set the speed. If not how are you controlling the speed of the cars?
Quote:
Also, when I use this: addMouseListener((MouseListener) this); to add a mouse listener to the object, the game crashes "GameName.Game cannot be cast to java.awt.event.MouseListener".
You need to pass an instance of that class that implements MouseListener and not an instance of the Game class.
Re: Detect a mouse click on specific entity in an arrayList
Ok. again, thanks for helping. Very useful.
Im a little confused, however. My apologies if I get things wrong here, but im very new to java. Is this better? I'm adding this to each method
Code:
addMouseListener(new mouseInputHandler());
Then im doing this in the mouse clicked event:
Code:
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
Object carClicked = e.getSource();
if (carClicked.equals(VerCar)) {
VerCar.setVerticalMovement(VerCar.dy * 2);
}
}
It does not work though, what am I doing wrong?
Re: Detect a mouse click on specific entity in an arrayList
As I explained before if the event handling logic is the same for all Cars then you only need to create 1 mouseInputHandler() and add that 1 instance to each of the cars. BTW class names should by convention always start with an upper case letter so mouseInputHandler should be MouseInputHandler.
Quote:
Then im doing this in the mouse clicked event:
Whats 'VerCar' and why are you testing if the car object is the same as it? And why are you setting the speed on VerCar and not your car object? And if VerCar is a variable why does it start with an uppercase letter?