Link to home
Start Free TrialLog in
Avatar of fivesigmaevent
fivesigmaevent

asked on

Can an event be user-defined and used in this situation?

Hi all,

I am practicing Java by developing a client/server connect4 project (not a homework project, I have graduated college years ago. just a project to help myself to learn Java).

The app is primarily composed of a Frame that contains a class representing the Gameboard, and a Status window that is a TextArea.

Problem: When a connect 4 occurs on the Gameboard I would like the Status TextArea to display "CONNECT 4" or something to that effect and not allow more mouse clicks on the Gameboard until the game is reset.

Q. When a player gets connect 4, could that be considered an Event and if so, is it possible to write user-defined events in Java and consequently it's handler? Any pointers on how this would be done?

I know this can be done without events, but think that if events can be used, it would be a more elegant solution. What do you think?

Thanks.

Avatar of JakobA
JakobA

Are you talking about the compiled language Java
or the script-language Javascript ?
Avatar of fivesigmaevent

ASKER

I'm using Sun Microsystems Java J2SE 1.4.1.

 
ASKER CERTIFIED SOLUTION
Avatar of warsql
warsql

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
warsql,

Thanks for your suggestions. I guess now I am running into an unforseen problem. In Java, to generate an event it looks like an event generator must be a component. I have a simple class, EvaluateBoard, that evaluates the board for a connect 4 but it is not a component. Would it be possible to make this class generate the desired Connect4 event?

Thanks again.
The event does not need to be generated by a component. The EvaluateBoard class can keep a list of listeners (using EventListenerList). Each of these listeners are classes which implements the EventListener interface. The class should have a method such as connect4Occurred(), which should be called when the connect4 event occurs. When the event occurs, an event object (a subclass of EventObject) should be created, and passed to connect4Occurred().
Ok. I think I understand. I'll try the following, but just wanted to run the algorithm by the forum for suggestions.

I'll pass the frame reference "this" to the Gameboard constructor.

public Gameboard( Frame f ){  frameref = f; }

The Gameboard consists of a 6x7 array of Slot canvases.  Slots are where the gamepieces have been drawn. There I will register each slot as a Connect4Event listener:

   for( i...
      for( j...
         slots[i][j].addAWTEventListener(frameref);

The Status TextArea, which is a member of the Frame, will also be registered as a Connect4Event listener:

   ta_status.addAWTEventListener( this );

When the EvaluateBoard object sees Connect 4, have the Frame call dispatchEvent(Connect4Event c):

   dispatchEvent( myConnect4Event );

To recognize the event, the Slot Canvases should implement AWTEventListener. Status TextArea should be extended to myTextArea to implement AWTEventListener. Make Connect4Event class return runtime representation of itself so event is known by event listener method.

class Connect4Event implements AWTEvent
{  // not sure if this is proper way to return runtime class type ???
   public synchronized Connect4Event class(){ return static Connect4Event;
...
}

class Slot extends Canvas implements MouseListener, AWTEventListener {
   void eventDispatched(AWTEvent e)
   {  // not sure this "if" statement is correct ???
      if( e.getClass() == Connect4Event.class() )
      {
         // somehow turn off mouse listener until game is reset
     
   }
}

class myStatusArea extends TextArea implements AWTEventListener {
   void eventDispatched(AWTEvent e)
   {  // not sure this "if" statement is correct ???
      if( e.getClass() == Connect4Event.class() )
      appendText( "Connect 4!" );
   }
}

Am I making sense and does this look ok? As per my code comments, does anyone know off hand how to turn off mouse listening temporarily, and return runtime class from an object and compare them?

Thanks for your thoughts. This is rather lengthy. I have increased the points to 30, but I don't have very many left. I would also like to split them between you, if possible.
This is more in depth than I thought, but I am closing in on the solution. Thanks for the pointers.