Results 1 to 18 of 18
  1. #1
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default Why cant i use addMouseListeners() in Eclipse

    Hi Folks,
    I am doing CS106A course and I can 't run this code from the book(Art and science of Java) on Eclipse.Only problem is with addMouseListeners().There is a red mark underneath it.I have googled about this problem and found out problem is with import.Someone has even fixed the same problem with changing the class?. Would be grateful if anybody could help me plz.cheers.


    This is the full code:
    Java Code:
    /*
    * File: DragFace.java
    * -------------------
    * This program creates a GFace object and allows the user to drag
    * it around the canvas.
    */
    import java.awt.event.*;
    import acm.graphics.*;
    import acm.program.*;
    public class DragFace extends GraphicsProgram {
    /** Width of the face */
    private static final double FACE_WIDTH = 200;
    /** Height of the face */
    private static final double FACE_HEIGHT = 300;
    /** Runs the program */
    public void run() {
    GFace face = new GFace(FACE_WIDTH, FACE_HEIGHT);
    double x = (getWidth() - FACE_WIDTH) / 2;
    double y = (getHeight() - FACE_HEIGHT) / 2;
    add(face, x, y);
    addMouseListeners();
    }
    /** Called on mouse press to record the coordinates of the click */
    public void mousePressed(MouseEvent e) {
    lastX = e.getX();
    lastY = e.getY();
    gobj = getElementAt(lastX, lastY);
    }
    /** Called on mouse drag to reposition the object */
    public void mouseDragged(MouseEvent e) {
    if (gobj != null) {
    gobj.move(e.getX() - lastX, e.getY() - lastY);
    lastX = e.getX();
    lastY = e.getY();
    }
    }
    /** Called on mouse click to move this object to the front */
    public void mouseClicked(MouseEvent e) {
    if (gobj != null) gobj.sendToFront();
    }
    /* Private state */
    private GObject gobj; /* The object being dragged */
    private double lastX; /* The last mouse X position */
    private double lastY; /* The last mouse Y position */
    }
    This is error message:
    Java Code:
    java.lang.Error: Unresolved compilation problem: 
        The method addMouseListeners() is undefined for the type DragObjects
    
        at DragObjects.run(DragObjects.java:19)
        at acm.program.Program.runHook(Program.java:1182)
        at acm.program.Program.startRun(Program.java:1169)
        at acm.program.Program.init(Program.java:834)
        at sun.applet.AppletPanel.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Your error is in DragObjects.java, and you have shown us DragFace.java.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,853
    Rep Power
    19

    Default

    Have you not heard of indentation?
    That's a wall of code...
    But:
    The method addMouseListeners() is undefined for the type DragObjects
    Why is the class you posted called DragFace?

    ETA: That's what happens when I get distracted!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    A key skill you will want to cultivate is to know how to look this sort of problem up. You need to look in your documentation for GraphicsProgram and see if there is in fact an addMouseListeners method that takes no parameters. I'll bet that the method doesn't exist named exactly like that, and that the real method requires a MouseListener parameter (at least that makes the most sense to me).

    Edit: also, this has nothing to do with Eclipse not allowing this or that. This is the Java compiler complaining about an error in your program pure and simple.

  5. #5
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    Oh sorry dudes.Here is the correct code:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import acm.graphics.*;
    import acm.program.*;
    
    /** This class displays a mouse-draggable rectangle and oval */
    public class DragObjects extends GraphicsProgram {
    /** Runs the program */
    public void run() {
    GRect rect = new GRect(100, 100, 150, 100);
    rect.setFilled(true);
    rect.setColor(Color.RED);
    add(rect);
    GOval oval = new GOval(300, 115, 100, 70);
    oval.setFilled(true);
    oval.setColor(Color.GREEN);
    add(oval);
    addMouseListeners();
    }
    /** Called on mouse press to record the coordinates of the click */
    public void mousePressed(MouseEvent e) {
    lastX = e.getX();
    lastY = e.getY();
    gobj = getElementAt(lastX, lastY);
    }
    /** Called on mouse drag to reposition the object */
    public void mouseDragged(MouseEvent e) {
    if (gobj != null) {
    gobj.move(e.getX() - lastX, e.getY() - lastY);
    lastX = e.getX();
    lastY = e.getY();
    }
    }
    /** Called on mouse click to move this object to the front */
    public void mouseClicked(MouseEvent e) {
    if (gobj != null) gobj.sendToFront();
    }
    /* Instance variables */
    private GObject gobj; /* The object being dragged */
    private double lastX; /* The last mouse X position */
    private double lastY; /* The last mouse Y position */
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I still stand by the recommendations made in my post above yours. You can't try to use methods that don't exist, and so it is necessary for you to find out what methods actually do exist and use them. Again, the GraphicsProgram API (which you have access to, and most of us don't since it's not standard Java), will answer this for you.
    Last edited by Fubarable; 10-20-2010 at 06:05 PM.

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Actually, the method does exist. API Documentation - GraphicsProgram/addMouseListeners()

    @ OP: Does this error occur in DragFace.java as well? Or just DragObjects.java?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm sorry, I appear to be wrong. The GraphicsProgram does have a method addMouseListeners listed in its API which can be found here: The acm.program.GraphicsProgram Class
    [edit: as Zack also points out above]

    OK, now I'm stumped. OP, can you post your class with indentation?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Do you have another class in the same package named "GraphicsProgram" that may be shadowing the original GraphicsProgram class?

  10. #10
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    [QUOTE]@ OP: Does this error occur in DragFace.java as well? Or just DragObjects.java? [QUOTE]

    Yes mate. DragFace.java has not got addMouseListeners but same issue with DrawRectangle.java which has got addMouseListeners method.

    OK, now I'm stumped. OP, can you post your class with indentation?
    post class with identation?

  11. #11
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    Do you have another class in the same package named "GraphicsProgram" that may be shadowing the original GraphicsProgram class?
    No, I do not think so mate
    Last edited by ccie007; 10-20-2010 at 07:37 PM. Reason: I didn't find any thing like that.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ccie007 View Post
    post class with identation?
    Indentation; those are the leading spaces or tab characters in code blocks that make your code (more or less) readable.

    jeez ...

    Jos

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    I'm sorry, I appear to be wrong. The GraphicsProgram does have a method addMouseListeners listed in its API which can be found here: The acm.program.GraphicsProgram Class
    [edit: as Zack also points out above]

    OK, now I'm stumped. OP, can you post your class with indentation?
    You weren't wrong; the signature of that method is (according to the API docs):

    Java Code:
    public void addMouseListeners(EventListener listener)
    which is not what the OP typed without reading the documentation ...

    kind regards,

    Jos

  14. #14
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    [QUOTE=ccie007;150659]
    post class with identation?
    as Jos said, indentation is adding whitespaces to code to make it much clearer to the reader.
    http://java.sun.com/docs/codeconv/ht...ions.doc3.html

    Convention is to add four spaces for each block of code - i.e. indent a block every time you use a "{" and go back a block every time you use a "}"

    The following code is much easier to read, no?

    berkeleybross

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import acm.graphics.*;
    import acm.program.*;
    
    /** This class displays a mouse-draggable rectangle and oval */
    public class DragObjects extends GraphicsProgram {
        /** Runs the program */
        public void run() {
            GRect rect = new GRect(100, 100, 150, 100);
            rect.setFilled(true);
            rect.setColor(Color.RED);
            add(rect);
            GOval oval = new GOval(300, 115, 100, 70);
            oval.setFilled(true);
            oval.setColor(Color.GREEN);
            add(oval);
            addMouseListeners();
        }
    
        /** Called on mouse press to record the coordinates of the click */
        public void mousePressed(MouseEvent e) {
            lastX = e.getX();
            lastY = e.getY();
            gobj = getElementAt(lastX, lastY);
        }
    
        /** Called on mouse drag to reposition the object */
        public void mouseDragged(MouseEvent e) {
            if (gobj != null) {
                gobj.move(e.getX() - lastX, e.getY() - lastY);
                lastX = e.getX();
                lastY = e.getY();
            }
        }
    
        /** Called on mouse click to move this object to the front */
        public void mouseClicked(MouseEvent e) {
            if (gobj != null) gobj.sendToFront();
        }
    
        /* Instance variables */
        private GObject gobj; /* The object being dragged */
        private double lastX; /* The last mouse X position */
        private double lastY; /* The last mouse Y position */
    }

  15. #15
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    There are two methods in the API docs - addMouseListeners() and addMouseListeners(EventListener listener)

    the first one adds the current class as the listener - however i assume that the class would have to implement an EventListener:

    Java Code:
    public class DragObjects extends GraphicsProgram implements EventListener {
    I've tried looking through the API for GraphicsProgram and it doesnt look like it implements EventListener itself.

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    You weren't wrong; the signature of that method is (according to the API docs):

    Java Code:
    public void addMouseListeners(EventListener listener)
    That's just one signature. It also has an overload that takes no parameters:
    addMouseListeners

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

    Default

    It may (or may not) have a bearing on this, but there was a question very recently that turned on an acm setFont() method not accepting an overloaded form that was clearly documented. I wonder if there are different versions of this library floating about. Or, less likely, something weird in the setup used by a particular class that leads to the shadowing that Fubarable wondered about before.

  18. #18
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    That seems most likely. Poor documentation is one of the worse infections in programming it seems. @OP, what version of the library are you using? Do you know?

Similar Threads

  1. AddMouseListeners();
    By xelo in forum New To Java
    Replies: 7
    Last Post: 04-10-2009, 12:49 AM
  2. Replies: 2
    Last Post: 01-27-2009, 02:10 PM
  3. Eclipse Bug - Can't Read From A File Using Eclipse?
    By carlodelmundo in forum New To Java
    Replies: 6
    Last Post: 01-26-2009, 04:25 PM
  4. Eclipse help
    By johnkennykumar in forum Eclipse
    Replies: 2
    Last Post: 12-31-2008, 03:21 AM
  5. J3D + eclipse
    By BeholdMyGlory in forum Eclipse
    Replies: 0
    Last Post: 12-25-2008, 12:09 AM

Posting Permissions

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