Results 1 to 8 of 8
Thread: AddMouseListeners();
- 04-09-2009, 11:27 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 8
- Rep Power
- 0
AddMouseListeners();
Hi,
I'm trying to follow along with the Stanford CS106A course available on iTunes U (introduction to programming and Java).
The time has come to event-driven programming, and we are using addMouseListeners(); and addKeyListeners(); as listeners for user input. However, I can not get this to work at all with Eclipse.
The program extends GraphicsProgram that has these methods, but Eclipse does not seem to find them?
Code like this:
is exactly what the professor is using, but I get two errors. The first because it does not seem to find the addMouseListeners(); method, and second that can not use a method called init (Eclipse just says "cannot override the final method from Program").Java Code:import acm.graphics.*; import acm.program.*; import java.awt.*; import java.awt.event.*; public class Blahaha extends GraphicsProgram{ public void init() { addMouseListeners(); } private void mouseMoved(MouseListener e){ //Some code that is executed if the mouse is moved } }
What is going on??Last edited by xelo; 04-09-2009 at 11:51 PM.
- 04-09-2009, 11:37 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,560
- Rep Power
- 11
Try adding an import for the GraphicsProgram class:
A couple of things about classes and packages. First Eclipse will add the import statements for you (Ctrl+Shift+O). This is not only saves time, but it means you get just the classes that you're actually using which makes the code more "self documenting".Java Code:import acm.program.GraphicsProgram;
Secondly the acm package may not be known to others here. (I wasn't familiar with it, for example.) If you use it you might like to include a link to its documentation.
- 04-09-2009, 11:42 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,560
- Rep Power
- 11
D@mn: I've just realised that you import acm.program.*! This should have picked up the GraphicsProgram class.
Make sure you have the jar file with this class in it and that you've added it to Eclipse's "build path". Right click on the project and select "Properties". Select "Java Build Path" on the right and then the "Libraries" tab. Then click on the "Add External JARs" button to add the jar file containing the library.
- 04-09-2009, 11:45 PM #4
Member
- Join Date
- Jan 2009
- Posts
- 8
- Rep Power
- 0
Thanks for your reply! However, nothing changed when I added your import statement. I have previously succeded to use the GraphicsProgram-class (and used methods inside that class), but it is now when I try to use events that I encounter these errors.
As a matter of fact, I tried to include a link but it said that I had to have posted at least 20 posts to be able to post links, so it did not accept my post :)
- 04-09-2009, 11:48 PM #5
Member
- Join Date
- Jan 2009
- Posts
- 8
- Rep Power
- 0
Hmm, how do I find the Jar-file?
- 04-10-2009, 12:07 AM #6
Member
- Join Date
- Jan 2009
- Posts
- 8
- Rep Power
- 0
I found the jar and I have added it as a referenced library in Eclipse.
Now I can actually see a method that is called AddMouseListeners(); if I expand the acm.jar file in Eclipse. The old library (called Karel.jar, downloaded from the Stanford website) doesn't seem to have that method.
However, my code is still underlined with red, still gives me an error when compiling. Is ther anything more I need to do?
- 04-10-2009, 12:17 AM #7
Member
- Join Date
- Jan 2009
- Posts
- 8
- Rep Power
- 0
Nevermind, I removed the karel.jar-file, and now it is working!!
Thanks for your help! :D
- 04-10-2009, 12:49 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,560
- Rep Power
- 11


LinkBack URL
About LinkBacks

Bookmarks