mouseClicked(MouseEvent e)

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

mouseClicked(MouseEvent e)

 
0
  #1
Nov 2nd, 2008
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
  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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,658
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 online now Online
Posting Virtuoso

Re: mouseClicked(MouseEvent e)

 
0
  #2
Nov 2nd, 2008
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.
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: mouseClicked(MouseEvent e)

 
0
  #3
Nov 2nd, 2008
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,
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,658
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 online now Online
Posting Virtuoso

Re: mouseClicked(MouseEvent e)

 
0
  #4
Nov 2nd, 2008
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
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: 2092 | Replies: 3
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC