?
Solved

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

Posted on 2003-02-23
7
Medium Priority
?
219 Views
Last Modified: 2010-03-31
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
Comment
Question by:fivesigmaevent
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 15

Expert Comment

by:JakobA
ID: 8006115
Are you talking about the compiled language Java
or the script-language Javascript ?
0
 

Author Comment

by:fivesigmaevent
ID: 8006185
I'm using Sun Microsystems Java J2SE 1.4.1.

 
0
 
LVL 2

Accepted Solution

by:
warsql earned 90 total points
ID: 8006460
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:fivesigmaevent
ID: 8006646
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
 
LVL 9

Expert Comment

by:yongsing
ID: 8007282
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
 

Author Comment

by:fivesigmaevent
ID: 8009309
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
 

Author Comment

by:fivesigmaevent
ID: 8012398
This is more in depth than I thought, but I am closing in on the solution. Thanks for the pointers.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question