943,722 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4371
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 1st, 2008
0

Re: Mouse listener

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 :-
Java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008
Nov 1st, 2008
0

Re: Mouse listener

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:

Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 1st, 2008
0

Re: Mouse listener

so, how can I fix it? just by removing that line?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008
Nov 1st, 2008
0

Re: Mouse listener

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 1st, 2008
0

Re: Mouse listener

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 1st, 2008
0

Re: Mouse listener

what code goes inside the two methods, mouseExited and mouseMoved?
This problme turns out to be harder than I thought!

Thanks,
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008
Nov 1st, 2008
0

Re: Mouse listener

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 1st, 2008
0

Re: Mouse listener

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
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008
Nov 1st, 2008
0

Re: Mouse listener

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 1st, 2008
0

Re: Mouse listener

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:-
Java Syntax (Toggle Plain Text)
  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,
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: can anyone find my mistake in TrainingZone.java
Next Thread in Java Forum Timeline: Need help from java expert





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC