Skip to main content

Hit test on Graphics2D object?



I have a few Graphics2D objects (Line2D, Rectangle2D, Ellipse2D, Rectangle2D) and a GeneralPath. I know that if I want to hit test on them I could use the .contains(Point) method, but I need to hit test on these objects when they are not filled in. So I just want to test if the user clicked on their border/line. I don't need to worry about the thickness of the border for now. Let's say that I just need to worry if the user clicked within 10 pixels from the border/line.





I do have a MouseListener where I can get the coordinates of the mouse click, and my Graphics2D objects are stored on a data structure that I iterate over. I just don't know how to hit test on the lines/borders.





Any suggestions will be appreciated!


Comments

  1. I've never had cause to use it, but it looks like your best bet is the Graphics2D.hit() method here.

    Just use a 1px*1px rectangle at the mouse position, and set the onStroke parameter to true, and make sure that the Clip, Transform, and Stroke properties are set up correctly (as mentioned in the javadoc) before you do the call.

    ReplyDelete
  2. Here's what I'd do:

    Ellipse2D case:
    Each time you want to see if your mouse has touched the edge of an Ellipse2D, create an Ellipse2D that is slightly bigger than the original, and an Ellipse2D that is slightly smaller. If your mouse click point is inside the larger Ellipse2D but outside the smaller one, then you've clicked "close" to the edge of the original shape.

    Rectangle2D case:
    Solve this the same way you do the Ellipse2D - make one bigger, one smaller, and determine if your mouse is inside the bigger one but outside the smaller one.

    Line2D case:
    Create a Rectangle2D that encloses your Line2D by some predetermined width. Then see if your mouse is inside that Rectangle2D.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.