Solved

Adding a mouse listener to a Canvas

Posted on 2004-09-05
23
399 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

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

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

747 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

11 Experts available now in Live!

Get 1:1 Help Now