?
Solved

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

Posted on 2003-03-27
8
Medium Priority
?
232 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:kingkarn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 672 total points
ID: 8218502
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
 
LVL 35

Assisted Solution

by:girionis
girionis earned 664 total points
ID: 8218853
 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
 
LVL 35

Expert Comment

by:girionis
ID: 8218897
 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 3

Assisted Solution

by:wide_awake
wide_awake earned 664 total points
ID: 8219043
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
 
LVL 3

Expert Comment

by:wide_awake
ID: 8266951
Any luck with any of the suggestions provided?
0
 

Expert Comment

by:CleanupPing
ID: 9058454
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
 
LVL 15

Expert Comment

by:jimmack
ID: 9734849
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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question