Java mechanism for events on a game board?

J

jim.hunt533

New to Java, I'm looking for a general way to set up a game board as a
grid of cells and communicate with them. So far I have defined a new
class, Cell, which extends JTextField and has some extra fields like
rowNo and columnNo, then populated a JPanel with a GridLayout(3,3)
with nine instances of Cell(i,j).

The problem then is how to code an event handler (like
KeyTyped(KeyEvent e)) in the Class to know which instance of Cell has
received the event.

The closest I have got so far finding ActionCommand and defining that
to be a row+column id string for the instance as it is constructed.
This is then visible in a System.out.println of the Event but I have
not found a way to extract it. I'm sure that is an abuse of
ActionCommand anyway.

Any and all ideas on better/correct techniques will be most welcome.
 
J

Joshua Cranmer

New to Java, I'm looking for a general way to set up a game board as a
grid of cells and communicate with them. So far I have defined a new
class, Cell, which extends JTextField and has some extra fields like
rowNo and columnNo, then populated a JPanel with a GridLayout(3,3)
with nine instances of Cell(i,j).

The problem then is how to code an event handler (like
KeyTyped(KeyEvent e)) in the Class to know which instance of Cell has
received the event.

The closest I have got so far finding ActionCommand and defining that
to be a row+column id string for the instance as it is constructed.
This is then visible in a System.out.println of the Event but I have
not found a way to extract it. I'm sure that is an abuse of
ActionCommand anyway.

Any and all ideas on better/correct techniques will be most welcome.

Try adding a KeyListener to every Cell object, which should know its
position.
 
K

Knute Johnson

New to Java, I'm looking for a general way to set up a game board as a
grid of cells and communicate with them. So far I have defined a new
class, Cell, which extends JTextField and has some extra fields like
rowNo and columnNo, then populated a JPanel with a GridLayout(3,3)
with nine instances of Cell(i,j).

The problem then is how to code an event handler (like
KeyTyped(KeyEvent e)) in the Class to know which instance of Cell has
received the event.

The closest I have got so far finding ActionCommand and defining that
to be a row+column id string for the instance as it is constructed.
This is then visible in a System.out.println of the Event but I have
not found a way to extract it. I'm sure that is an abuse of
ActionCommand anyway.

Any and all ideas on better/correct techniques will be most welcome.

In your KeyListener, get the source of the event, your Cell component
and take the row and column from that.

public void keyPressed(KeyEvent ke) {
Cell cell = (Cell)ke.getSource();
// then you have cell.row or cell.getRow() whatever
 
R

Roedy Green

The problem then is how to code an event handler (like
KeyTyped(KeyEvent e)) in the Class to know which instance of Cell has
received the event.

You can use a Canvas, then analyse the x,y (just divide by a constant)
to get the cell number. This will be considerably faster than having
a layout of components.

See http://mindprod.com/jgloss/event11.html
 
J

jim.hunt533

In your KeyListener, get the source of the event, your Cell component
and take the row and column from that.

public void keyPressed(KeyEvent ke) {
Cell cell = (Cell)ke.getSource();
// then you have cell.row or cell.getRow() whatever

--

Knute Johnson
email s/nospam/knute/- Hide quoted text -

- Show quoted text -

That works! That's great - many thanks - Jim
 
J

jim.hunt533

You can use a Canvas, then analyse the x,y (just divide by a constant)
to get the cell number. This will be considerably faster than having
a layout of components.

Seehttp://mindprod.com/jgloss/event11.html

Thanks Roedy - I'll keep an eye on performance and maybe go to this
technique if necessary - Jim
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top