943,725 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5820
  • Java RSS
Nov 2nd, 2008
0

mouseClicked(MouseEvent e)

Expand Post »
can you guys help me how can I remove all those drawn rectangles if user click the mouse in the applet field?
I am supposed to write a program that displays a rectangle and color it with green when user drag the mouse. But if user click the mouse anywhere in the field then that rectangle should disappear.

I have got all working except that mouse clicked part.
Here is my code
Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class DrawRect extends JApplet
  6. {
  7. private int pointCounter =0;
  8. private Point coordinate1[] = new Point[20];
  9. private Point coordinate2[] = new Point[20];
  10. private Point beg = new Point();
  11. private Point last = new Point();
  12. Rectangle rectn = new Rectangle();
  13. private boolean flag;
  14.  
  15. public DrawRect()
  16. {
  17. addMouseMotionListener( new MouseMotionAdapter()
  18. {
  19. public void mouseDragged(MouseEvent e)
  20. {
  21. last = e.getPoint();
  22. rectn.setFrameFromDiagonal(beg, last);
  23. repaint();
  24. }
  25. });
  26. addMouseListener(new MouseAdapter()
  27. {
  28. public void mousePressed(MouseEvent e)
  29. {
  30. beg = e.getPoint();
  31. }
  32.  
  33. public void mouseReleased(MouseEvent e)
  34. {
  35. coordinate1[pointCounter] = beg;
  36. coordinate2[pointCounter] = e.getPoint();
  37. pointCounter++;
  38. rectn.setFrameFromDiagonal(beg, beg);
  39. repaint();
  40. }
  41.  
  42. public void mouseClicked(MouseEvent e)
  43. {
  44.  
  45. if(flag==false)
  46. {
  47. repaint();
  48. }
  49. }
  50.  
  51. });
  52. }
  53.  
  54. public void paint(Graphics g)
  55. {
  56.  
  57. super.paint(g);
  58. Graphics2D g2 = (Graphics2D)g;
  59. g2.draw(rectn);
  60. g2.setPaint(Color.yellow);
  61. Rectangle rectn2 = new Rectangle();
  62. for (int i =0; i < pointCounter; i++)
  63. {
  64. rectn2.setFrameFromDiagonal(coordinate1[i], coordinate2[i]);
  65. g2.fill(rectn2);
  66. }
  67. }
  68.  
  69.  
  70. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008
Nov 2nd, 2008
0

Re: mouseClicked(MouseEvent e)

http://www.daniweb.com/forums/thread154611-3.html

I spent 2-3 hours helping you, the least you can do is mark the thread as 'solved'. I also gave you a suggestion on how to do this in the other thread, although I don't think my suggestion was a good way to do it. But if you make the rectangle have no area, then when it is repainted, it will display as invisible.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 2nd, 2008
0

Re: mouseClicked(MouseEvent e)

ok, how can I make my rectangle with no area?
Can you actually show me the code?
then, I will mark this thread as solved.

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

Re: mouseClicked(MouseEvent e)

http://www.daniweb.com/forums/thread154611-3.html


^^ No, I was talking about marking that thread as solved, which is your previous thread. Its a matter of courtesy to mark a thread as solved, especially when someone spends a lot of their time helping you, as I have.


Anyway, the way that you make a rectangle with no area is easy. Area = length * width, so a rectangle with either length = 0 or width = 0 will have 0 area. So set one of the sides of the rectangle to 0. Again, I direct you to the Javadoc:

http://java.sun.com/j2se/1.4.2/docs/...Rectangle.html
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 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: Out of curiosity, BigDecimal
Next Thread in Java Forum Timeline: how to copy BigInteger?





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


Follow us on Twitter


© 2011 DaniWeb® LLC