Results 1 to 18 of 18
- 10-20-2010, 05:28 PM #1
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
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:
This is error message: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 */ }
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)
- 10-20-2010, 05:32 PM #2
Your error is in DragObjects.java, and you have shown us DragFace.java.
- 10-20-2010, 05:36 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Have you not heard of indentation?
That's a wall of code...
But:
Why is the class you posted called DragFace?The method addMouseListeners() is undefined for the type DragObjects
ETA: That's what happens when I get distracted!
-
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.
- 10-20-2010, 05:47 PM #5
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
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 */ }
-
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.
- 10-20-2010, 06:03 PM #7
Actually, the method does exist. API Documentation - GraphicsProgram/addMouseListeners()
@ OP: Does this error occur in DragFace.java as well? Or just DragObjects.java?
-
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?
-
Do you have another class in the same package named "GraphicsProgram" that may be shadowing the original GraphicsProgram class?
- 10-20-2010, 07:26 PM #10
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
[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.
post class with identation?OK, now I'm stumped. OP, can you post your class with indentation?
- 10-20-2010, 07:37 PM #11
Senior Member
- Join Date
- May 2010
- Posts
- 112
- Rep Power
- 0
No, I do not think so mateDo you have another class in the same package named "GraphicsProgram" that may be shadowing the original GraphicsProgram class?Last edited by ccie007; 10-20-2010 at 07:37 PM. Reason: I didn't find any thing like that.
- 10-20-2010, 07:38 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 10-20-2010, 07:46 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 10-20-2010, 08:01 PM #14
[QUOTE=ccie007;150659]
as Jos said, indentation is adding whitespaces to code to make it much clearer to the reader.post class with identation?
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 */ }
- 10-20-2010, 08:21 PM #15
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:
I've tried looking through the API for GraphicsProgram and it doesnt look like it implements EventListener itself.Java Code:public class DragObjects extends GraphicsProgram implements EventListener {
-
- 10-20-2010, 08:30 PM #17
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.
- 10-21-2010, 06:14 AM #18
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
-
AddMouseListeners();
By xelo in forum New To JavaReplies: 7Last Post: 04-10-2009, 12:49 AM -
Is it possible to run eclipse plugins without using eclipse?
By ambar in forum EclipseReplies: 2Last Post: 01-27-2009, 02:10 PM -
Eclipse Bug - Can't Read From A File Using Eclipse?
By carlodelmundo in forum New To JavaReplies: 6Last Post: 01-26-2009, 04:25 PM -
Eclipse help
By johnkennykumar in forum EclipseReplies: 2Last Post: 12-31-2008, 03:21 AM -
J3D + eclipse
By BeholdMyGlory in forum EclipseReplies: 0Last Post: 12-25-2008, 12:09 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks