| | |
mouseClicked(MouseEvent e)
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2008
Posts: 57
Reputation:
Solved Threads: 1
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
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)
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DrawRect extends JApplet { private int pointCounter =0; private Point coordinate1[] = new Point[20]; private Point coordinate2[] = new Point[20]; private Point beg = new Point(); private Point last = new Point(); Rectangle rectn = new Rectangle(); private boolean flag; public DrawRect() { addMouseMotionListener( new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { last = e.getPoint(); rectn.setFrameFromDiagonal(beg, last); repaint(); } }); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { beg = e.getPoint(); } public void mouseReleased(MouseEvent e) { coordinate1[pointCounter] = beg; coordinate2[pointCounter] = e.getPoint(); pointCounter++; rectn.setFrameFromDiagonal(beg, beg); repaint(); } public void mouseClicked(MouseEvent e) { if(flag==false) { repaint(); } } }); } public void paint(Graphics g) { super.paint(g); Graphics2D g2 = (Graphics2D)g; g2.draw(rectn); g2.setPaint(Color.yellow); Rectangle rectn2 = new Rectangle(); for (int i =0; i < pointCounter; i++) { rectn2.setFrameFromDiagonal(coordinate1[i], coordinate2[i]); g2.fill(rectn2); } } }
•
•
Join Date: Sep 2008
Posts: 1,625
Reputation:
Solved Threads: 205
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.
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.
•
•
Join Date: Sep 2008
Posts: 1,625
Reputation:
Solved Threads: 205
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
^^ 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
![]() |
Similar Threads
- Colour picker, MouseDown(Event, int x, int y) Depreciation Help! (Java)
- Calculator GUI (Java)
- JDialog Problem (Java)
- Applet Help (Java)
- GridBagLayout HELP (Java)
- I lack focus... (Java)
- Applet qns (Java)
- Interfaces (Java)
- how to do it?? (Java)
Other Threads in the Java Forum
- Previous Thread: Out of curiosity, BigDecimal
- Next Thread: how to copy BigInteger?
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arc arguments array arrays automation binary bluetooth c++ chat class classes client code codesnippet component csv database doctype draw ebook eclipse error event exception fractal freeze game givemetehcodez graphics gui html ide image input integer intellij iphone j2me java java.xls javaprojects jmf jni jpanel julia linux list loop loops mac map method methods mobile netbeans newbie number online oracle page parameter plazmic print problem program programming project recursion reporting rotatetext scanner screen sell server set size sms socket sort sourcelabs sql string superclass swing system template test testautomation threads time title tree tutorial-sample windows working






