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?


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

fivesigmaeventAuthor Commented:

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().
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...

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.
fivesigmaeventAuthor Commented:
This is more in depth than I thought, but I am closing in on the solution. Thanks for the pointers.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.