Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2003-03-27
8
Medium Priority
?
237 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
7 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

578 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