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


}

Recommended Answers

All 3 Replies

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.

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,

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.