addMouseListener

C

Ce

Hello.

I'm new to Java programming, so forgive me if I'm asking stupid questions.

I wrote my own class "Sprite" because I'm writing a little java game. Now,
I
was trying to made my sprites aware of mouse clicks.
I tried to implement the MouseListener interface in my class and tried to
override mouse access methods. All ok, but when I try to call the

addMouseListener(this)

in the contrsuctor of my Sprite class, I get a compilation error "cannot
find
symbol". By looking in docs and internet I understood that this is
happening becuase
I'm not extending any AWT object here, but just using my own class. Is
there any
way to have addMouseListener available in a "custom class" (so, wrote from
scratch without
extending other java classes)?

Thanks,

Ce
 
V

Vincent van Beveren

I'm not extending any AWT object here, but just using my own class. Is
there any
way to have addMouseListener available in a "custom class" (so, wrote from
scratch without
extending other java classes)?

Hi Ce,

No, not by default. A mouselistener is typically something of a
component architecture. So, unless your sprite extends component, you
can not just add a mouse listener. You'll have to write those methods
yourself (addMouseListener), but even then, its not enough, because
you'll also have to write the event handling and stuff.

The best thing would be to create a GameCanvas object (or the like)
which extends JPanel for example. This GameCanvas has a method
addSprite() (or the like :) ). Every sprite has a method (protected?,
empty? abstract?) clicked(int x, int y). GameCanvas itself implements
MouseListener. Now, when a click is done on the gamecanvas, it iteratres
though its list of sprite objects and selects the one on which is
clicked. You can then invoke that sprites clicked method.

For example

public abstract class Sprite {
... your stuff
abstract void clicked(int x, int y);
}


public class Bunny extends Sprite {
void clicked(int x, int y) {
System.out.println("You clicked the bunny!");
}
}

You'll have to write the routine in the MouseListener which will take
the event and look up the right sprite, and invoke the clicked method.

Good luck,
Vincent
 
C

Ce

Vincent van Beveren ha scritto:
You'll have to write the routine in the MouseListener which will take
the event and look up the right sprite, and invoke the clicked method.
Good luck,
Vincent

Thanks a bunch Vincent!

I'm not sure if I got you at 100%, but this is what I tried:

I tried to create my Sprite class extending JPanel (=:-|)....then in my
PlaneSprite
class (which extends Sprite) I implemented MouseListener, I overridden the
mouse
methods and added addMouseListener(this) in the constructor...all compile
without errors

BUT

:) it looks the event is not captured: when I click nothing happens (even
if I created

public void mouseClicked(MouseEvent e){
System.out.println("Click!!!!Plane");
}

I'm sure you was probably saying somehting more that I didn't catch....

Ce
 
V

Vincent van Beveren

I'm sure you was probably saying somehting more that I didn't catch....

Hey Ce,

Well, what I meant was make the game canvas a component, which is
basically your screen, and make the sprite object just a simple class
(not a compontent). When using component the screen might display minor
glitches while redrawing the objects in the game. However, when you want
it more sophisticated you'll need to use double buffering and stuff, and
i won't want to do that to you right now.

What you said you did seems to be correct. If you add the sprite to a
container or window the mouse events should propagate to your component
and the mouseClicked should be triggered. Could you show me a part of
your PlaneSprite and the part where the PlaneSprite is added to the
window/container.

Vincent
 
C

Ce

What you said you did seems to be correct. If you add the sprite to a
container or window the mouse events should propagate to your component
and the mouseClicked should be triggered. Could you show me a part of
your PlaneSprite and the part where the PlaneSprite is added to the
window/container.

Vincent, you are very kind. This is the code portion (don't blame me, this
is
my first Java program...I'm sure it is quite ugly...).

the class PlaneManager instantiate the PlaneSprite objects (where I added
the
mouse listener).

Ce

--------------------CODE START-----------------------

abstract class Sprite extends JPanel {
protected boolean visible; // is sprite visible
protected boolean active; // is sprite updateable

/* abstract methods */
abstract public void paint(Graphics g);

abstract public void update();

.... some other code here....
}

abstract class Sprite2D extends Sprite {

protected int locx;
protected int locy;
protected String tpId;

Color color;
boolean fill;

public boolean getFill() {
return fill;
}

....some other code here....
}

class PlaneSprite extends Sprite2D implements Moveable, MouseListener {

protected static final int planeDim = 10; // dimensions of circle
representing the plane

public PlaneSprite(int x, int y, Color c) {
locx = x;
locy = y;
tpId = planeName();

color = c;
fill = true;

addMouseListener(this);

restore(); // restore the sprite

}

/* provide implementation of abstract methods */

// FIXME
public void update() {// does nothing
}

/* check if sprite's visible before painting */
public void paint(Graphics g) {
if (visible) {
g.setColor(color);

if (fill) {
g.fillOval(locx, locy, planeDim, planeDim);
g.drawString(tpId, locx - 17, locy + 25);
} else {
g.drawOval(locx, locy, planeDim, planeDim);
g.drawString(tpId, locx - 17, locy + 25);
}
}
}

/************************************
Implements the methods for movement
*************************************/
... some code here ...

/* Define plane Name */
private String planeName() {
.... some code here ....
}

// Override the MouseListener methods
public void mouseClicked(MouseEvent e){
System.out.println("Click!!!!Plane");
}

public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
}

class PlaneManager {

static int width, height; // applet dimensions
private PlaneSprite plane[];
static final int NUM_PLANES = 4;

int planeX, planeY; // Plane coordinates
int vplaneX, vplaneY; // Plane velocity
int maxVelocity = 1;
String planeID; // Plane name

public PlaneManager(int width, int height, Applet a) {
this.width = width;
this.height = height;

plane = new PlaneSprite[NUM_PLANES];
for (int i = 0; i < plane.length; i++) {

setDirections(width, height, maxVelocity);

if (jControlTower.debug >= 2) {
System.out.println(
"Plane - X:" + planeX + " Y:" + planeY + " VX: "
+ vplaneX + " VY: " + vplaneY);
}

plane = new PlaneSprite(planeX, planeY, Color.gray);
plane.setVelocity(vplaneX, vplaneY);
}
}

.... some code here ....
}

--------------------CODE END-----------------------
 
V

Vincent van Beveren

Vincent, you are very kind. This is the code portion (don't blame me, this
is
my first Java program...I'm sure it is quite ugly...).

the class PlaneManager instantiate the PlaneSprite objects (where I added
the
mouse listener).

Ce

Well, it all looks good to me. The only thing I didn't find was where
you added the sprite to the applet, or container object. You'll need to
add the sprite component to a parent container/applet in order for
events to be captured. So, after this line:
> plane.setVelocity(vplaneX, vplaneY);


do something like

a.add(plane);


Vincent
 
C

Ce

events to be captured. So, after this line:
plane.setVelocity(vplaneX, vplaneY);

do something like
a.add(plane);


Great Vincent, you put me on the right way!

I solved it: I passed to PlaneSprite the applet reference, and then I wrote

applet.addMouseListener(this)

in the constructor of PlaneSprite. Now the event is captured (even
removing the JPanel extension of my Sprite class).

:) ...thanks so much!

Ce
 
V

Vincent van Beveren

Ce schreef:
events to be captured. So, after this line:
plane.setVelocity(vplaneX, vplaneY);

do something like
a.add(plane);


Great Vincent, you put me on the right way!

I solved it: I passed to PlaneSprite the applet reference, and then I wrote

applet.addMouseListener(this)

in the constructor of PlaneSprite. Now the event is captured (even
removing the JPanel extension of my Sprite class).

:) ...thanks so much!


Yeah, np, I hope your game works out

Vincent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top