Reputation: 11752
I have a Ball class which I want to have extend JComponent and implement mouseListener.
public class Ball extends JComponent implements MouseListener {
Int x, y, radius;
public Ball(int X, int Y, int Radius){
//contains only three ints and redefines x,y,radius
x=X;
y=Y;
radius=Radius;
}
public void draw(Graphics g){
//draw oval using x,y,radius
}
//5 mouselisteners undefined yet
}
So ball is the constructor which is used by a panel which is within a frame.
Sorry I have not yet entered all the code. I will submit my complete code soon.
So what I would have to do is use MouseEntered listener in the ball class so that when the mouse enters the component (the ball/oval) . But I don't know how to define the component so that it knows it has been entered. Does it need some dimensions? Because all I am doing is using the draw function in a panel.
Upvotes: 2
Views: 2214
Reputation: 36621
Besides the remarks already made by Hovercraft Full Of Eels, I think you confuse the concept of a being a listener, and adding a listener to something.
It is not by implementing MouseListener
that those methods will be called. A listener is the interested party and you add it to the object in which you are interested. So in this case you want to add a MouseListener
to your Ball
class, which is completely different from letting your Ball
class implement MouseListener
.
More information can be found on Wikipedia: Observer pattern or a more simple and more Swing oriented document can be found in the Swing tutorials
Upvotes: 0
Reputation: 285450
If this were my class, I wouldn't have it extend JComponent and wouldn't give it a MouseListener or MouseMotionListener especially if I wanted to display multiple balls in a single JComponent. Instead I would give it public methods that allow other classes to get its boundaries (such as is available from the Shape interface), and whether something is contained in the shape or not (again the Shape interface works well for this), and other public methods that allow outside classes to change the state (appearance?) of this object.
I would then have a JComponent hold one Ball or an ArrayList<Ball>
, and in the MouseListener/MouseMotionListener/MouseAdapter for this JComponent, iterate through the ArrayList<Ball>
seeing if the mouse is inside of any ball, and if so, change that ball's state. Then in the JComponent's paintComponent method, I'd iterate through the ArrayList<Ball>
calling draw(g)
on each Ball contained.
Upvotes: 3
Reputation: 1218
I would prefer that you implement the whole MouseListener. It could be possible that you want later more from your Ball-Component, like for example that it moves if you click on it or something. If you implement the interface you are applicable for later changes.
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JComponent;
public class Ball extends JComponent implements MouseListener {
int x, y, radius;
public Ball(int x, int y, int radius){
//contains only three ints and redefines x,y,radius
this.x= x;
this.y= y;
this.radius= radius;
}
public void draw(Graphics g){
//draw oval using x,y,radius
}
@Override
public void mouseClicked(MouseEvent arg0) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
//your code to do things, when the mouse entered your ball
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent arg0) {
}
@Override
public void mouseReleased(MouseEvent arg0) {
}
}
Upvotes: 0
Reputation: 10352
You should call
this.addMouseListener(new MouseAdapter() {
....
});
in the constructor. Override mouseEntered()
and mouseExited()
inside of the adapter.
Upvotes: 1