Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

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.

0
fivesigmaevent
Asked:
fivesigmaevent
1 Solution
 
JakobACommented:
Are you talking about the compiled language Java
or the script-language Javascript ?
0
 
fivesigmaeventAuthor Commented:
I'm using Sun Microsystems Java J2SE 1.4.1.

 
0
 
warsqlCommented:
You could create your own extended AWTEvent class Connect4Event, then call dispatchEvent(yourConnect4Event) on the Frame.  It would be up to each ui component to register itself as a listener to the frame to pick up this event and respond accordingly.  I would suggest that you also have a ResetEvent to use in the same manner to reenable the ui components.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
fivesigmaeventAuthor Commented:
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.
0
 
yongsingCommented:
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().
0
 
fivesigmaeventAuthor Commented:
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.
0
 
fivesigmaeventAuthor Commented:
This is more in depth than I thought, but I am closing in on the solution. Thanks for the pointers.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now