Solved

use action-listeners on userdefined objects in a canvas

Posted on 1998-08-18
7
320 Views
Last Modified: 2008-03-03
I have a canvas containing objects ( which are ludo game
pieces here). For drawing these objects on the screen, they
contain x and y coordinates relative to the convas. What I need is actionListeners on every object, so that its not needed to verify the position of a mouse click, but directly  
jump to the actionPerformed method.

This easily done for buttons, menuItems and such but how do I make one for a component?
0
Comment
Question by:moonlight
  • 4
  • 2
7 Comments
 
LVL 4

Expert Comment

by:evijay
ID: 1222040
are these objects extend from Component ?

If so, you can make these objects implement MouseListener and then in the mouseClicked method of it, fire a actionEvent.
You need to add the methods addActionListener and removeActionListener to these objects and maintain listeners in a vector. In the mouse click event, you need to invoke a fireActionEvent method (which you implement). In the fireActionEvent method, you create a ActionEvent, traverse thru the listener vector and invoke the actionPerformed method for each element in vector.

vijay

0
 
LVL 3

Accepted Solution

by:
mjenkins earned 100 total points
ID: 1222041
You need to derive your Object from Component, of course. Once that is done, make sure the objects implements

addActionListener()
removeActionListener()
protected void processEvent(AWTEvent e)
protected void processActionEvent(ActionEvent e)

0
 
LVL 3

Expert Comment

by:mjenkins
ID: 1222042
Here is a complete example just to make it easier to understand:

import java.awt.*;
import java.awt.event.*;
 
public class Act
        extends Canvas
{
    private ActionListener actionListener;
 
        public static void main( String args[] )
        {
                Frame f = new Frame( "Act" );
                f.setSize( 400,400 );
                Act a = new Act( );
                f.add( a );
                a.addActionListener( new ActionListener()
                        {
                                public void actionPerformed( ActionEvent e )
                                {
                                        System.out.println( "DONE" );
                                }
                        }
                );
                f.setVisible(true);
        }
 
        public Act()
        {
                setSize( 100,100 );
                enableEvents( AWTEvent.MOUSE_EVENT_MASK | AWTEvent.ACTION_EVENT_MASK );
        }
 
    public synchronized void addActionListener(ActionListener l)
        {
                actionListener = AWTEventMulticaster.add(actionListener, l);
    }
 
    public synchronized void removeActionListener(ActionListener l)
        {
                actionListener = AWTEventMulticaster.remove(actionListener, l);
    }
 
    protected void processEvent(AWTEvent e)
        {
        if (e instanceof MouseEvent)
                {
                        if( e.getID() == MouseEvent.MOUSE_RELEASED )
                processActionEvent( new ActionEvent(e.getSource(),ActionEvent.ACTION_PERFORMED, "ACTION!") );    
        }
                super.processEvent(e);
    }
 
    protected void processActionEvent(ActionEvent e)
        {
        if (actionListener != null)
                {
            actionListener.actionPerformed(e);
        }
    }
}

Notice that it is our responsibilty to convert the MouseEvent into an ActionEvent. I hope this puts the matter in perspective.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Author Comment

by:moonlight
ID: 1222043
Hi,
I've tested your sample code, but it doesnt respond the way I want it to. It doesnt matter where you click and I want it to. Isn't it what you should achieve with setSize(100,100) in Act () ?

How do I make an invisible area on the screen which will generate the object's actionPerformed() and send the object to it, when clicked on? as opposed to what I see now when it runs actionPerformed() on all clicks everywhere?
0
 
LVL 3

Expert Comment

by:mjenkins
ID: 1222044
The reason that it responds no matter where you click, is that the layout manager is resizing the component (even though I set it to 100,100). The default layout for Frame is BorderLayout. BorderLayout expands the "Center" component to the size of the container. If you insert:

f.setLayout( null );

right after

f.setSize(400,400);

You will see the behavior you are expecting. The app will print "DONE" only while clicking in the area bounded by 0,0,100,100.

You need to manage the size of your components in your actual application.
0
 
LVL 3

Expert Comment

by:mjenkins
ID: 1222045
Well, did it work? Are you satisfied with your answer?
0
 
LVL 3

Author Comment

by:moonlight
ID: 1222046
thank you for the help. sorry for dealy.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now