We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,617 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

mouseClicked(MouseEvent e)

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

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);
        }
    }


}
2
Contributors
3
Replies
5 Hours
Discussion Span
4 Years Ago
Last Updated
4
Views
complexcodes
Junior Poster in Training
57 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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.

BestJewSinceJC
Posting Maven
2,774 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
Skill Endorsements: 13

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,

complexcodes
Junior Poster in Training
57 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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/api/java/awt/Rectangle.html

BestJewSinceJC
Posting Maven
2,774 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
Skill Endorsements: 13

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0887 seconds using 2.95MB