Mouse listener

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 57
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is offline Offline
Junior Poster in Training

Re: Mouse listener

 
0
  #11
Nov 1st, 2008
Ezzarel, I did run the applet with the code you provided. I am confused if I have to use that canvas mentioned in your code.
Here is my updated code, and I am getting one compile error. Can you plese check if my logic is correct :-
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.net.*;
  5.  
  6. public class DragRectangle extends JApplet implements MouseListener
  7. {
  8. private int previousX, previousY;
  9. private boolean drag;
  10. public void init()
  11. {
  12. addMouseListener(this);
  13.  
  14. }
  15. public void paint(Graphics g)
  16. {
  17. super.paint(g);
  18. g.setColor(Color.YELLOW);
  19. g.drawRect(previousX, previousY, getWidth(), getHeight());
  20. }
  21. public void mouseDragged(MouseEvent event)
  22. {
  23.  
  24. if(drag==false)
  25. {
  26. return;
  27. }
  28.  
  29. int x = event.getX();
  30. int y = event.getY();
  31. previousX=x;
  32. previousY=y;
  33.  
  34. }
  35. public void mouseReleased(MouseEvent evt)
  36. {
  37.  
  38. repaint();
  39. }
  40.  
  41. }

compile error:-
C:\Documents and Settings\Owner\My Documents\java\DragRectangle.java:6: DragRectangle is not abstract and does not override abstract method mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener
public class DragRectangle extends JApplet implements MouseListener
^
1 error

Tool completed with exit code 1
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,654
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Mouse listener

 
0
  #12
Nov 1st, 2008
That error is because when you implement a class (which you did by saying DragRectangle implements MouseListener) you have to have all of the methods from the class.

Example: lets say these are the methods from MouseListener:

int mouseStuff();
Object moreMouseStuff();
Object otherMouseStuff();

That would mean that any time you write a class that 'implements MouseListener', your class would have to include the methods mouseStuff, moreMouseStuff, and otherMouseStuff. Specifically, the compiler is complaining because you need to have a method in your class that is declared as follows:

  1. public void mouseExited(MouseEvent e){
  2. }

(Side note: what is the point of the code in your mouseDragged method that says if (drag==false)? It seems to me that you never initialized the variable, dragged. Also, it seems like the variable is useless anyway, since if mouseDragged gets called, its guaranteed that the mouse was dragged.)


Note: to the kid who is having trouble, you can ignore everything I say after this line... pay attention to what I said above. Everything after this point is directed at Ezzaral & others.
Documentation for the class can be found here:
http://java.sun.com/j2se/1.4.2/docs/...eListener.html
If you read the documentation, it says that mouseDragged is NOT a method supported by the class, so I am confused why he implemented it. It says to use MouseMotionListener. Am I looking at the wrong documentation somehow?

edit: I looked at the documentation for MouseMotionListener & back at Ezzeral's code, and it turns out that the kid just implemented the wrong class. So nevermind my confusion.
Last edited by BestJewSinceJC; Nov 1st, 2008 at 4:33 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 57
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is offline Offline
Junior Poster in Training

Re: Mouse listener

 
0
  #13
Nov 1st, 2008
so, how can I fix it? just by removing that line?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,654
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Mouse listener

 
0
  #14
Nov 1st, 2008
No, that will not solve your problem. The problem is that there are methods that you need to implement which you didn't implement. Specifically, you need to implement the mouseExited method. To do that, all you need to do is put the mouseExited method in your code.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,654
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Mouse listener

 
0
  #15
Nov 1st, 2008
Actually, you DO need to erase the part where it says "MouseListener", but not for the reason that you think. You were supposed to implement MouseMotionListener. So where it says MouseListener, change it MouseMotionListener. Then, go to the Java API for that class. (Link: http://java.sun.com/j2se/1.4.2/docs/...nListener.html). Go to the method summary and make sure that you have each of the methods in your code. Currently, you only have the mouseDragged method in your code, so you will have to add the mouseMoved method to your code.

Also, since you don't yet know how implementing classes works, GO READ THE JAVA TUTORIALS!

Interfaces:
http://java.sun.com/docs/books/tutor...interface.html
Last edited by BestJewSinceJC; Nov 1st, 2008 at 4:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 57
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is offline Offline
Junior Poster in Training

Re: Mouse listener

 
0
  #16
Nov 1st, 2008
what code goes inside the two methods, mouseExited and mouseMoved?
This problme turns out to be harder than I thought!

Thanks,
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,654
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Mouse listener

 
0
  #17
Nov 1st, 2008
Read the link I gave you about MouseMotionListener. The method detail for mouseDragged states that as the user drags the mouse, MouseEvents continue to occur until the user stops by unclicking. Therefore, if you want the Rectangle to move wherever the user drags it, then you would have to call the repaint() method inside your mouseDragged code. The code that you posted earlier, in mouseDragged in post #11, will be useful for knowing where the mouse currently is. If you read the method details for mouseDragged, you will also notice that it will probably not help you with the part about "I want the rectangle to be green when the user stops dragging it." To make the Rectangle green, you will need to look into other methods. Hint: look into the MouseListener interface. You can do this by googling for Java MouseListener.
Last edited by BestJewSinceJC; Nov 1st, 2008 at 6:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 57
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is offline Offline
Junior Poster in Training

Re: Mouse listener

 
0
  #18
Nov 1st, 2008
I dont want my rectangle to be filled with green while dragging, rather I want my recatngle to be filled when I release my mouse(done with dragging). I dont think I need repaint method in mouseDragged method, do I?
Also, in mouse moved method, do I need to keep track of the coordinates?
This thing is so so so confusing! first time I am doin something related to mouseListener class
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,654
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Mouse listener

 
0
  #19
Nov 1st, 2008
What I was saying in my last post is that you want the Rectangle to be moved to wherever you drag your mouse on the screen. This can be done by putting the correct code in mouseDragged. You also want the Rectangle to be colored green when the user unclicks. This can be done by implementing MouseListener, then putting the correct code in the correct method (read the description of the methods in the MouseListener javadoc!). So, in summary, you have to implement both MouseMotionListener and MouseListener.

When repaint() is called, I'm pretty sure it calls paintComponent. So you would have to write the code that does the actual redrawing of the Rectangle based on its position in paintComponent. But when you want to change the Rectangle's color, you can just put the code to change the color in the method that detects when the user unclicks. Then, right after that, you'd say repaint() so that the color change takes effect.

If you need clarification of anything I just said, I will clarify. Beyond that, I won't help anymore unless you post code and ask specific questions about it.
Last edited by BestJewSinceJC; Nov 1st, 2008 at 7:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 57
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is offline Offline
Junior Poster in Training

Re: Mouse listener

 
0
  #20
Nov 1st, 2008
how can I make the rectangle disappear after I click the mouse anywhere in applet?
what line of codes do I need to add?

Thanks,
here is my working version of code:-
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class DragRectangle extends JApplet
  6. {
  7. private Point coordinate1[]=new Point[20];
  8. private Point coordinate2[]=new Point[20];
  9. private Point beg=new Point();
  10. private Point last=new Point();
  11. private int pointCounter=0;
  12. Rectangle rect = new Rectangle();
  13.  
  14. public void DrawRectangle()
  15. {
  16. addMouseMotionListener(new MouseMotionAdapter()
  17. {
  18. public void mouseDragged(MouseEvent ev)
  19. {
  20. last = ev.getPoint();
  21. rect.setFrameFromDiagonal(beg,last);
  22. repaint();
  23. }
  24. });
  25. addMouseListener(new MouseAdapter()
  26. {
  27. public void mousePressed(MouseEvent e)
  28. {
  29. beg = e.getPoint();
  30. }
  31.  
  32. public void MouseReleased(MouseEvent ev)
  33. {
  34. coordinate1[pointCounter]=beg;
  35. coordinate2[pointCounter]= ev.getPoint();
  36. pointCounter ++;
  37. rect.setFrameFromDiagonal(beg,beg);
  38. repaint();
  39. }
  40. });
  41. }
  42. public void paint(Graphics g)
  43. {
  44. super.paint(g);
  45. Graphics2D g2 = (Graphics2D)g;
  46. g2.setPaint(Color.black);
  47. g2.draw(rect);
  48. g2.setPaint(Color.yellow);
  49. Rectangle rect2 = new Rectangle();
  50. for (int i =0; i < pointCounter; i++)
  51. {
  52. rect2.setFrameFromDiagonal(coordinate1[i], coordinate2[i]);
  53. g2.fill(rect2);
  54. }
  55. }
  56. }

Thanks,
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2712 | Replies: 22
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC