Solved

Adding a mouse listener to a Canvas

Posted on 2004-09-05
23
406 Views
Last Modified: 2008-02-01
Hi all,

I am trying to add a listener to a Canvas so that when I move the mouse it triggers a function.

Could people please check my line of thought to see if I have done anything stupid:

1) First of all I simply used mouseMove(), which worked fine but the documentation says it is now depretiated.  So I decided to do as the documentation recomends and use processMouseMotionEvent()

2) To do this I put the following line into the constructor:
this.addMouseMotionListener(this);

But it seems Canvas does not implement MouseMotionListener, so I created a subclass of Canvas which also implements MouseMotionListener

3)  Then it complains that I also need to add some new functions, even though I don't want them:
  public void mouseDragged(MouseEvent event){}
  public void mouseMoved(MouseEvent event){}

It looks to me like mouseMoved() is very similar to mouseMove(),  which is what I was using in the first place!

Am I doing this all wrong here?
0
Comment
Question by:agrees
  • 11
  • 8
  • 2
  • +1
23 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 11985078
Probably easiest to use an inner adapter class:


yourCanvas.addMouseMotionListener(new YourMouseMotionAdapter());

      // Inner class of canvas

      class YourMouseMotionAdapter extends MouseMotionAdapter {
            public void mouseMoved(MouseEvent e)  {}
      }
      
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 11986411
1) Wrong method, see MouseMotionListener class

2) what you have done is fine

3) Adding the extra methods is no problem, and they must be ikmplemented somewhere.
To avoid defining them you'll need to use a seperate class as your MouseListener, but as I said its not a problem.

0
 
LVL 92

Expert Comment

by:objects
ID: 11986433
You can also use processMouseMotionEvent(), in which case you don't need any listeners.
Simply add that method to your subclass.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Assisted Solution

by:Emilda
Emilda earned 25 total points
ID: 11987114
See the following code snippet, where c is a Canvas object.

As shown, you can use an abstract adapter, MouseMotionAdapter class for receiving mouse motion events. This class exists as convenience for creating listener objects.  Or you can implement the inferface, MouseMotionListener( but in that case you need to implement all the methods in that inferface. You can leave the unwated methods as empty) and add it as listener to Canvas object.

c.addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseMoved (MouseEvent e) {
                      // do your process here.
      }
    });

or if your class has extended from a Canvas, then you can implement processMouseMotionEvent() method include the necessary actions inside.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11988461
>>3) Adding the extra methods is no problem

It is if you don't want to use them

>>To avoid defining them you'll need to use a seperate class as your MouseListener

(as my example shows)
0
 

Author Comment

by:agrees
ID: 11988627
>> You can also use processMouseMotionEvent(), in which case you don't need any listeners.

That seems like the most straight forward thing to do, but I tried adding this method but it is never triggered.  Is there something else I need to do?
0
 
LVL 92

Expert Comment

by:objects
ID: 11988632
> It is if you don't want to use them

like I said its no problem :-D

> (as my example shows)

It doesn't need to be an inner class, you can use whatever class you want.

In fact you don't even need to use a seperate class at all.

0
 
LVL 92

Expert Comment

by:objects
ID: 11988657
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11988675
>> You can also use processMouseMotionEvent(), in which case you don't need any listeners.

NOT a good idea. You don't need to (and shouldn't) revert to an earlier component model in order to get the functionality you need
0
 
LVL 92

Expert Comment

by:objects
ID: 11988713
> NOT a good idea.

why exactly ???

> You don't need to (and shouldn't) revert to an earlier component model in order to get the functionality you need

if that was the case then surely it would be deprecated


0
 
LVL 92

Expert Comment

by:objects
ID: 11988720
> Is there something else I need to do?

make sure you call enableEvents, eg.

this.enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11988844
>>why exactly ???

Reasons such as

a. it's error prone (e.g. that code you posted last could easily cause errors with the rest of the program)
b. it produces tight coupling with event listening, which is undesirable in nearly all cases

>>if that was the case then surely it would be deprecated

Not much point deprecating it when it is central to an earlier event model. That doesn't mean the earlier model should be perpetuated in Swing though
0
 
LVL 92

Expert Comment

by:objects
ID: 11988879
Its not error prone at all, it fact it's very stable.

> (e.g. that code you posted last could easily cause errors with the rest of the program)

agrees,

Try it out, and look at the example I posted earlier :)

> That doesn't mean the earlier model should be perpetuated in Swing though

Swings got nothing to do with it
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11988913
>>Its not error prone at all, it fact it's very stable.

Stability has nothing to do with it. It's simply error prone. The reason? - agrees, you will have to OR all the constants for all events you want to handle, or you program will simply not work properly
0
 
LVL 92

Expert Comment

by:objects
ID: 11992800
> you will have to OR all the constants for all events you want to handle, or you program will simply not work properly

Of course you would, thats what the method is for :D
Certainly doesn't make it error prone.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12004641
I shall email you privately about the above
0
 

Author Comment

by:agrees
ID: 12004949
>> make sure you call enableEvents, eg.
>> this.enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);

Doesn't that do the same as adding a listener?

>> CEHJ, I do not see ANYTHING constructive in your comments on the objects

Well disagreement is good but unfortunately I am not qualified to judge whose answer is the best!  So I'll have to split the points if no third party can give further insights.

Currently I am using the inner adapter class solution and it seems to work.
0
 
LVL 92

Expert Comment

by:objects
ID: 12004963
> Doesn't that do the same as adding a listener?

other way around, adding a listener will enable events.
0
 
LVL 92

Expert Comment

by:objects
ID: 12004967
>  I am not qualified to judge whose answer is the best

just explaing the options to you :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12004992
>>  I am not qualified to judge whose answer is the best

Try them both, you'll soon find out when it comes to writing, maintaining or extending the code (or someone else will ;-))
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12035118
:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12036046
let me know if you need further help in the future :)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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:
The viewer will learn how to implement Singleton Design Pattern in Java.

776 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