Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6374367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:28:45+00:00 2026-05-25T01:28:45+00:00

I have built a custom component that shows only a line. The line is

  • 0

I have built a custom component that shows only a line. The line is drawn from the top left corner to the bottom right corner as a Line2D at the paint method. The background is transparent. I extended JComponent. These line components are draggable and change their line color when the mouse pointer is located max. 15 pixels away from the drawn line.
But if I have multiple of these components added to another custom component that extends JPanel they sometimes overlap. I want to implement that if the mouse pointer is more than 15 pixels away from the line the mouse events should fall through the component. How to let it fall through is my problem.
Is that even possible?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:28:46+00:00Added an answer on May 25, 2026 at 1:28 am

    For my final year project at university I did a whiteboard program and had the same problem. For each shape the user drew on the board I created a JComponent, which was fine when they were drawing rectangles, but more difficult with the free form line tool.

    The way I fixed it in the end was to do away with JComponents altogether. I had a JPanel which held a Vector (I think) of custom Shape objects. Each object held its own coordinates and line thicknesses and such. When the user clicked on the board, the mouse listener on the JPanel fired and went through each Shape calling a contains(int x, int y) method on each one (x and y being the coordinates of the event). Because the Shapes were added to the Vector as they were drawn I knew that the last one to return true was the topmost Shape.

    This is what I used for a straight line contains method. The maths might be a bit iffy but it worked for me.

    public boolean contains(int x, int y) {
    
        // Check if line is a point
        if(posX == endX && posY == endY){
            if(Math.abs(posY - y) <= lineThickness / 2 && Math.abs(posX - x) <= lineThickness / 2)
                return true;
            else
                return false;
        }
    
        int x1, x2, y1, y2;
    
        if(posX < endX){
            x1 = posX;
            y1 = posY;
            x2 = endX;
            y2 = endY;
        }
        else{
            x1 = endX;
            y1 = endY;
            x2 = posX;
            y2 = posY;
        }
    
    
        /**** USING MATRIX TRANSFORMATIONS ****/
    
        double r_numerator = (x-x1)*(x2-x1) + (y-y1)*(y2-y1);
        double r_denomenator = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
        double r = r_numerator / r_denomenator;
    
        // s is the position of the perpendicular projection of the point along
        // the line: s < 0 = point is left of the line; s > 0 = point is right of
        // the line; s = 0 = the point is along the line
        double s =  ((y1-y)*(x2-x1)-(x1-x)*(y2-y1) ) / r_denomenator;
    
        double distance = Math.abs(s)*Math.sqrt(r_denomenator);
    
        // Point is along the length of the line
        if ( (r >= 0) && (r <= 1) )
        {
                if(Math.abs(distance) <= lineThickness / 2){
                    return true;
                }
                else
                    return false;
        }
        // else point is at one end of the line
        else{
            double dist1 = (x-x1)*(x-x1) + (y-y1)*(y-y1); // distance to start of line
            double dist2 = (x-x2)*(x-x2) + (y-y2)*(y-y2); // distance to end of line
            if (dist1 < dist2){
                distance = Math.sqrt(dist1);
            }
            else{
                distance = Math.sqrt(dist2);
            }
            if(distance <= lineThickness / 2){
                return true;
            }
            else
                return false;
        }
        /**** END USING MATRIX TRANSFORMATIONS****/
    
    }
    

    posX and posY make up the coordinates of the start of the line and endX and endY are, yep, the end of the line. This returned true if the click is within lineThickness/2 of the centre of the line, otherwise you have to click right along the very middle of the line.

    Drawing the Shapes was a case of passing in the JPanel’s Graphics object to each Shape and doing the drawing with that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mac with a custom PHP 5 install that built from about
I have built a custom view component that displays a timeline of events. The
I have a custom built sign up component that I would like to enhance.
I have just built a custom autocomplete component using jquery. My problem is that
I have built a custom component using some containers and a TileList. Now when
I have built a custom membership provider that I have used in a previous
We have a custom-built Flash-based video player that I maintain, and it needs to
I have a custom application that was built to send opt-in newsletters and marketing
I have a test that exercises a custom Swing component using java.awt.Robot. I'd like
it's my first question. I have built a custom component: a RelativeLayout with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.