How to check whether point that mouse clicked located on the border of rectangle

Suppose I have one rectangle that add to Jpanel. I want to detect when mouse clicked.
That point is located on rectanble border or not. I found only method to check point
containing in rectangle area.
kingkarnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

imladrisCommented:
There is no built in method for that. You will have to calculate, in your code, where the boundaries of the rectangle are, and compare them to the coordinates of the mouse activity.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
girionisCommented:
 The following code should do (main code adopted from Sun's web site):

MouseEventDemo.java:

/*
 * Swing version
 */
import javax.swing.*;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class MouseEventDemo extends JApplet
                            implements MouseListener {
    BlankArea blankArea;
     int rectX = 30, rectY = 40, rectWidth = 100, rectHeight=130;
    final static String newline = "\n";
    public void init() {
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        JPanel contentPane = new JPanel();
        contentPane.setLayout(gridbag);
        c.fill = GridBagConstraints.BOTH;
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.insets = new Insets(1, 1, 1, 1);
        blankArea = new BlankArea(new Color(0.98f, 0.97f, 0.85f));
        gridbag.setConstraints(blankArea, c);
        contentPane.add(blankArea);
        c.insets = new Insets(0, 0, 0, 0);
         
        //Register for mouse events on blankArea and applet.
        blankArea.addMouseListener(this);
        addMouseListener(this);
        setContentPane(contentPane);
    }
   
    public void paint(Graphics g)
    {
         g.drawRect(rectX, rectY, rectWidth, rectHeight);
    }
   
    public void mousePressed(MouseEvent e) {
    }
    public void mouseReleased(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
    public void mouseClicked(MouseEvent e) {
         System.out.println("mouse clicked at x: " + e.getX() + " and y: " + e.getY());
         if ( (e.getX() % rectX == 0) || (e.getY() % rectY == 0) ||
              (e.getY() % (rectY + rectHeight) == 0) || (e.getX() % (rectX + rectWidth) == 0) )
              System.out.println("Clicked on border.");
    }
}

BlankArea.java:

/*
 * Swing version
 */
import javax.swing.*;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Graphics;
public class BlankArea extends JLabel {
    Dimension minSize = new Dimension(100, 100);
    public BlankArea(Color color) {
        setBackground(color);
        setOpaque(true);
        setBorder(BorderFactory.createLineBorder(Color.black));
    }
    public Dimension getMinimumSize() {
        return minSize;
    }
    public Dimension getPreferredSize() {
        return minSize;
    }
}

  Hope it helps.
0
girionisCommented:
 Basicalyl a good solution is to have it in a method, lets say, borderClicked that returns true or false and according to output you can do things:

public void mouseClicked(MouseEvent e) {
    if (borderClicked(e))
        // do something
    else
        // do something else.
}

public boolean borderClicked(MouseEvent e)
{
    if ( (e.getX() % rectX == 0) || (e.getY() % rectY == 0) ||
             (e.getY() % (rectY + rectHeight) == 0) || (e.getX() % (rectX + rectWidth) == 0) )
        return true

    return false;
}
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

wide_awakeCommented:
if you can check whether a click is in a rectangle or not, just test whether the click is inside the rectangle and NOT inside a slightly smaller rectangle.

Where the "slightly" is the border width of your rectangle.

0
wide_awakeCommented:
Any luck with any of the suggestions provided?
0
CleanupPingCommented:
kingkarn:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
jimmackCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split between imladris, girionis and wide_awake.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.