Results 1 to 8 of 8
  1. #1
    xelo is offline Member
    Join Date
    Jan 2009
    Posts
    8
    Rep Power
    0

    Default 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:
    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
    	}
    			
    }
    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").

    What is going on??
    Last edited by xelo; 04-10-2009 at 12:51 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Try adding an import for the GraphicsProgram class:

    Java Code:
    import acm.program.GraphicsProgram;
    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".

    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.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  4. #4
    xelo is offline Member
    Join Date
    Jan 2009
    Posts
    8
    Rep Power
    0

    Default

    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 :)

  5. #5
    xelo is offline Member
    Join Date
    Jan 2009
    Posts
    8
    Rep Power
    0

    Default

    Hmm, how do I find the Jar-file?

  6. #6
    xelo is offline Member
    Join Date
    Jan 2009
    Posts
    8
    Rep Power
    0

    Default

    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?

  7. #7
    xelo is offline Member
    Join Date
    Jan 2009
    Posts
    8
    Rep Power
    0

    Default

    Nevermind, I removed the karel.jar-file, and now it is working!!

    Thanks for your help! :D

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Sorry about not replying earlier...

    I'm glad you've found the file and got it sorted. The Stanford documentation that I saw referred to the karel classes rather than the ones you were using, so I was a bit confused.

    Good luck with the course!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •