Adding MouseListener to an Object

S

sso

I'm new to Java so the answer to my question may be quite simple.
Basically my Main class is a JFrame. The JFrame contains an instance
of the "board" class. The board class contains several tiles
(Graphics). I need to be able to detect MouseListeners to the
tiles. I've done things with MouseListeners (adapters etc) with
standard swing components and not had trouble. Perhaps there is
something I don't know or I'm forgetting some of the basics.
Suggestions?

Thanks!

/
*********************************************************************************/
public class Main extends JFrame implements MouseListener{
public Main()
{
Container c = getContentPane();
board brd = new board();
c.add(brd);
setSize(500,500);
setVisible(true);
}

public static void main(String[] args) {
// TODO code application logic here
Main app = new Main();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
}

/
*********************************************************************************/
public class board implements MouseListener
{
// private Point points[] = new Point[500];
private Vector<tile> tiles = new Vector<tile>(500);
// HashMap tiles = new HashMap(500);
public board()
{

int xx = 0;
for(int i = 1; i < 35; i++)
{
for(int j = 0; j < 35; j++)
{
tile newtile = new tile(i * 24, j * 24);
tiles.add(newtile);
}
}
System.out.print(tiles.size());
}


@Override
public void paintComponent(Graphics g)
{
for(tile t : tiles)
{
g.drawRect(t.getX(),t.getY(),20,20);

}

}
}
/
*********************************************************************************/
public class tile {
private int x;
private int y;

public tile (int x, int y) {
this.x = x;
this.y = y;
}
}
 
S

sso

My first suggestion is to make sure when you post looking for an answer,  
that you've included a specific question.  :)

Suggestion number two is that you post a concise-but-complete code example  
that reliably demonstrates whatever issue you're having.

 From your post, it's hard to know what you're having trouble with.  If the  
code example is everything you've got, then there are several problems:

     -- You have two class declarations that promise to implement the  
MouseListener interface, but then never actually implement it
     -- You never call addMouseListener() on any component, so events for  
your (non-existent) MouseListener implementations would never be raised
     -- You try to call an add() method on an instance of Container with an  
argument of a type that is not compatible with any overload for add()  
found in Container
     -- You execute AWT/Swing code on a thread that is not the Event  
Dispatch Thread
     -- You override the paintComponent() method in a class that does not  
extend JComponent (or any class implementing paintComponent() for that  
matter)

And those are just the obvious ones I found.

Either the code that you posted isn't anywhere close to being useful, or  
it isn't anywhere close to being the code you're actually using.  In  
either case, since you haven't posted a specific question to go along with  
the code, it's not even possible to make some guesses as to what problem  
you might be having, never mind offer a specific solution to whatever  
problem you are actually having.

Try again?  :)

Pete

Well, lets go back to the beginning. :)
I'm trying to build a little game. Essentially I need to be able to
detect clicks on the tiles on the board, then I can go from there.
The reason the code looks useless is..... well... because it probably
is :) I was just trying different things to see if I could brute
force it.

So I guess only one class needs to implement MouseListener and it must
be a class that extends a JComponent (panel, label, etc).

I think it would be ideal if I could draw the board with Graphics (a
Vector of tile) and then determine which tile it was that the
MouseEvent occurred on. I'm not sure how to determine which tile the
MouseEvent occurred on.

I hope that is more clear :) and thank you :)

Regards.
 
K

Knute Johnson

sso said:
Well, lets go back to the beginning. :)
I'm trying to build a little game. Essentially I need to be able to
detect clicks on the tiles on the board, then I can go from there.
The reason the code looks useless is..... well... because it probably
is :) I was just trying different things to see if I could brute
force it.

So I guess only one class needs to implement MouseListener and it must
be a class that extends a JComponent (panel, label, etc).

I think it would be ideal if I could draw the board with Graphics (a
Vector of tile) and then determine which tile it was that the
MouseEvent occurred on. I'm not sure how to determine which tile the
MouseEvent occurred on.

I hope that is more clear :) and thank you :)

Regards.

Did you look at the Tic Tac Toe program I wrote for you? It shows you
exactly how to do that.
 
S

sso

[...]
So I guess only one class needs to implement MouseListener and it must
be a class that extends a JComponent (panel, label, etc).

You need not implement MouseListener in any named class if you don't want  
to.  I often use anonymous classes for various Swing "listener"  
implementations, often making the anonymous class extend MouseAdapter so  
that I only have to override the methods of interest to me.

There is no requirement that your MouseListener implementation extend a  
JComponent.  What you do need is to make sure that once you've got a  
MouseListener implementation, you actually add it to a component via an  
addMouseListener() method.
I think it would be ideal if I could draw the board with Graphics (a
Vector of tile)  and then determine which tile it was that the
MouseEvent occurred on.  I'm not sure how to determine which tile the
MouseEvent occurred on.

Two obvious approaches come to mind:

     -- Make each tile a component (e.g. JComponent), and add a  
MouseListener to each one.  Then you simply need to look at the sender of  
the event to know which tile was clicked.

     -- Make each tile a simple part of the "board" implementation, adding  
a MouseListener to whatever part of the GUI presentation is actually a  
proper Swing component.  In the handler, mathematically determine based on  
the mouse coordinates which tile was actually clicked on.

Note that in the first approach, you'll probably want to make the "board"  
implementation a component as well.  Note also that you need not have  
multiple instances of your MouseListener implementation.  A single  
instance can do all of the appropriate things simply by checking to see  
what the actual sender of the event was.

Pete

I had some examples I was looking at that finally make sense.
So would it make sense when I click on the board to iterate through my
vector of tiles (assuming I have included attributes to the tile
class that indicate the coordinates of the tile) and see if the
mouseevent.getX() and .getY() make for a match?

That means I have to iterate through the entire vector every time
there is a mouse event, no very efficient...
 
K

Knute Johnson

sso said:
[...]
So I guess only one class needs to implement MouseListener and it must
be a class that extends a JComponent (panel, label, etc).
You need not implement MouseListener in any named class if you don't want
to. I often use anonymous classes for various Swing "listener"
implementations, often making the anonymous class extend MouseAdapter so
that I only have to override the methods of interest to me.

There is no requirement that your MouseListener implementation extend a
JComponent. What you do need is to make sure that once you've got a
MouseListener implementation, you actually add it to a component via an
addMouseListener() method.
I think it would be ideal if I could draw the board with Graphics (a
Vector of tile) and then determine which tile it was that the
MouseEvent occurred on. I'm not sure how to determine which tile the
MouseEvent occurred on.
Two obvious approaches come to mind:

-- Make each tile a component (e.g. JComponent), and add a
MouseListener to each one. Then you simply need to look at the sender of
the event to know which tile was clicked.

-- Make each tile a simple part of the "board" implementation, adding
a MouseListener to whatever part of the GUI presentation is actually a
proper Swing component. In the handler, mathematically determine based on
the mouse coordinates which tile was actually clicked on.

Note that in the first approach, you'll probably want to make the "board"
implementation a component as well. Note also that you need not have
multiple instances of your MouseListener implementation. A single
instance can do all of the appropriate things simply by checking to see
what the actual sender of the event was.

Pete

I had some examples I was looking at that finally make sense.
So would it make sense when I click on the board to iterate through my
vector of tiles (assuming I have included attributes to the tile
class that indicate the coordinates of the tile) and see if the
mouseevent.getX() and .getY() make for a match?

That means I have to iterate through the entire vector every time
there is a mouse event, no very efficient...

If you know how big your tiles are then you should be able to calculate
which one you are on so you wouldn't have to search. On the other hand,
you could make all of the tiles separate components with their own
MouseListeners and it wouldn't matter.
 
S

sso

sso said:
[...]
So I guess only one class needs to implement MouseListener and it must
be a class that extends a JComponent (panel, label, etc).
You need not implement MouseListener in any named class if you don't want  
to.  I often use anonymous classes for various Swing "listener"  
implementations, often making the anonymous class extend MouseAdapter so  
that I only have to override the methods of interest to me.
There is no requirement that your MouseListener implementation extend a  
JComponent.  What you do need is to make sure that once you've got a  
MouseListener implementation, you actually add it to a component via an  
addMouseListener() method.
I think it would be ideal if I could draw the board with Graphics (a
Vector of tile)  and then determine which tile it was that the
MouseEvent occurred on.  I'm not sure how to determine which tile the
MouseEvent occurred on.
Two obvious approaches come to mind:
     -- Make each tile a component (e.g. JComponent), and add a  
MouseListener to each one.  Then you simply need to look at the sender of  
the event to know which tile was clicked.
     -- Make each tile a simple part of the "board" implementation, adding  
a MouseListener to whatever part of the GUI presentation is actually a  
proper Swing component.  In the handler, mathematically determine based on  
the mouse coordinates which tile was actually clicked on.
Note that in the first approach, you'll probably want to make the "board"  
implementation a component as well.  Note also that you need not have  
multiple instances of your MouseListener implementation.  A single  
instance can do all of the appropriate things simply by checking to see  
what the actual sender of the event was.
Pete
I had some examples I was looking at that finally make sense.
So would it make sense when I click on the board to iterate through my
vector of tiles   (assuming I have included attributes to the tile
class that indicate the coordinates of the tile) and see if the
mouseevent.getX() and .getY() make for a match?
That means I have to iterate through the entire vector every time
there is a mouse event, no very efficient...

If you know how big your tiles are then you should be able to calculate
which one you are on so you wouldn't have to search.  On the other hand,
you could make all of the tiles separate components with their own
MouseListeners and it wouldn't matter.

There are spaces between the tile, I think it would be difficult to
calculate that, but they are all the same size. Also I don't know of
this tic tac toe example, but I do have a book with a tic tac toe
example.
 
L

Lew

sso said:
I had some examples I was looking at that finally make sense.
So would it make sense when I click on the board to iterate through my
vector of tiles (assuming I have included attributes to the tile
class that indicate the coordinates of the tile) and see if the
mouseevent.getX() and .getY() make for a match?

That means I have to iterate through the entire vector every time
there is a mouse event, no very efficient...

You'd be better off not using Vector, incidentally. It was largely supplanted
by ArrayList in 1998.

You don't really need the synchronization that Vector provides, and you
certainly don't need the Enumeration machinery. Go with modern types.
 
L

Lew

sso said:
There are spaces between the tile, I think it would be difficult to
calculate that, but they are all the same size. Also I don't know of
this tic tac toe example, but I do have a book with a tic tac toe
example.

If you go with the idea of each tile being implemented as a separate
JComponent, the spaces between won't matter.

If you use a LayoutManager, it's relatively easy to place the tiles where they
belong.
 
K

Knute Johnson

sso said:
sso said:
On May 3, 10:43 pm, "Peter Duniho" <[email protected]>
wrote:
[...]
So I guess only one class needs to implement MouseListener and it must
be a class that extends a JComponent (panel, label, etc).
You need not implement MouseListener in any named class if you don't want
to. I often use anonymous classes for various Swing "listener"
implementations, often making the anonymous class extend MouseAdapter so
that I only have to override the methods of interest to me.
There is no requirement that your MouseListener implementation extend a
JComponent. What you do need is to make sure that once you've got a
MouseListener implementation, you actually add it to a component via an
addMouseListener() method.
I think it would be ideal if I could draw the board with Graphics (a
Vector of tile) and then determine which tile it was that the
MouseEvent occurred on. I'm not sure how to determine which tile the
MouseEvent occurred on.
Two obvious approaches come to mind:
-- Make each tile a component (e.g. JComponent), and add a
MouseListener to each one. Then you simply need to look at the sender of
the event to know which tile was clicked.
-- Make each tile a simple part of the "board" implementation, adding
a MouseListener to whatever part of the GUI presentation is actually a
proper Swing component. In the handler, mathematically determine based on
the mouse coordinates which tile was actually clicked on.
Note that in the first approach, you'll probably want to make the "board"
implementation a component as well. Note also that you need not have
multiple instances of your MouseListener implementation. A single
instance can do all of the appropriate things simply by checking to see
what the actual sender of the event was.
Pete
I had some examples I was looking at that finally make sense.
So would it make sense when I click on the board to iterate through my
vector of tiles (assuming I have included attributes to the tile
class that indicate the coordinates of the tile) and see if the
mouseevent.getX() and .getY() make for a match?
That means I have to iterate through the entire vector every time
there is a mouse event, no very efficient...
If you know how big your tiles are then you should be able to calculate
which one you are on so you wouldn't have to search. On the other hand,
you could make all of the tiles separate components with their own
MouseListeners and it wouldn't matter.

There are spaces between the tile, I think it would be difficult to
calculate that, but they are all the same size. Also I don't know of
this tic tac toe example, but I do have a book with a tic tac toe
example.

I posted this sample program in response to your post last week. I
never saw a reply.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TTT extends JPanel {
enum Mark { X, O, BLANK, DRAW };
Mark[] mark = new Mark[9];
boolean player = true;

public TTT() {
setPreferredSize(new Dimension(400,300));

for (int i=0; i<mark.length; i++)
mark = Mark.BLANK;

addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
int row = 0;
int col = 0;
if (me.getX() < getWidth()/3)
col = 0;
else if (me.getX() < getWidth()*2/3)
col = 1;
else
col = 2;
if (me.getY() < getHeight()/3)
row = 0;
else if (me.getY() < getHeight()*2/3)
row = 1;
else
row = 2;
int index = row * 3 + col;
if (mark[index] == Mark.BLANK) {
if (player)
mark[index] = Mark.X;
else
mark[index] = Mark.O;
player = !player;
repaint();

Mark m = checkForWin();
if (m != Mark.BLANK) {
String msg = null;
if(m == Mark.DRAW)
msg = "Game is a Draw";
else
msg = "Winner is " + m.toString();
JOptionPane.showMessageDialog(TTT.this,msg);
for (int i=0; i<mark.length; i++)
mark = Mark.BLANK;
player = true;
repaint();
}
}
}
});
}

public void paintComponent(Graphics g) {
Font f = new Font("Monospaced",Font.PLAIN,getHeight()/4);
g.setFont(f);
FontMetrics fm = g.getFontMetrics();

g.setColor(Color.WHITE);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.BLACK);
g.drawLine(getWidth()/3,10,getWidth()/3,getHeight()-10);
g.drawLine(getWidth()*2/3,10,getWidth()*2/3,getHeight()-10);
g.drawLine(10,getHeight()/3,getWidth()-10,getHeight()/3);
g.drawLine(10,getHeight()*2/3,getWidth()-10,getHeight()*2/3);

for (int i=0; i<mark.length; i++) {
String c = null;
switch (mark) {
case X: c = "X"; break;
case O: c = "O"; break;
case BLANK: c = " "; break;
}
g.drawString(c,i%3*getWidth()/3+fm.stringWidth(c),
i/3*getHeight()/3+fm.getHeight()*4/5);
}
}

Mark checkForWin() {
if (mark[0] == mark[1] && mark[1] == mark[2] &&
mark[0] != Mark.BLANK) {
return mark[0];
} else if (mark[3] == mark[4] && mark[4] == mark[5] &&
mark[3] != Mark.BLANK) {
return mark[3];
} else if (mark[6] == mark[7] && mark[7] == mark[8] &&
mark[6] != Mark.BLANK) {
return mark[6];
} else if (mark[0] == mark[4] && mark[4] == mark[8] &&
mark[0] != Mark.BLANK) {
return mark[0];
} else if (mark[2] == mark[4] && mark[4] == mark[6] &&
mark[2] != Mark.BLANK) {
return mark[2];
} else if (mark[0] == mark[3] && mark[3] == mark[6] &&
mark[0] != Mark.BLANK) {
return mark[0];
} else if (mark[1] == mark[4] && mark[4] == mark[7] &&
mark[1] != Mark.BLANK) {
return mark[1];
} else if (mark[2] == mark[5] && mark[5] == mark[8] &&
mark[2] != Mark.BLANK) {
return mark[2];
}

for (int i=0; i<mark.length; i++)
if (mark == Mark.BLANK)
return Mark.BLANK;

return Mark.DRAW;
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TTT ttt = new TTT();
f.add(ttt);
f.pack();
f.setVisible(true);
}
});
}
}
 
J

John B. Matthews

Knute Johnson said:
sso wrote: [...]
There are spaces between the tile, I think it would be difficult to
calculate that, but they are all the same size. Also I don't know
of this tic tac toe example, but I do have a book with a tic tac
toe example.

I posted this sample program in response to your post last week. I
never saw a reply.

Knute: Sweet! This is the first I've see it, though. It may not have
propagated from NewsDemon previously.

[...]

OP: For clarity, Knute's mouseClicked() method shows an explicit
conversion from mouse coordinates to row & column coordinates. For
another approach, consider a linear proportion between row number and
vertical mouse position within the panel:

row number : number of rows :: mouse y-coordinate : panel height

Substituting and rearranging gives this:

int row = 3 * me.getY() / getHeight();

A similar analysis allows finding the column:

int col = 3 * me.getX() / getWidth();

The inverse transformations just solve the proportion above for the
corresponding mouse coordinate. A similar example may be found in this
tile-based game:

<http://robotchase.svn.sourceforge.net/viewvc/robotchase/trunk/src/org/gc
s/robot/RCTile.java>
 

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

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top