Supporting User Interaction
by, 03-06-2012 at 08:41 PM (376 Views)
User interaction is supported with the graphics you want to display. You need to be able to determine when the user clicks on one of them. To determine whether a mouse click occurred over a particular Shape object, The Graphics2D class provides a hit method. You can also get the location of the mouse click. Call the contains method on the Shape class to determine whether the click was within the bounds of the Shape.
You can perform simple hit testing, if you are using primitive text by getting the outline Shape. It corresponds to the text and then calling hit or contains with that Shape. Much more sophisticated hit testing is required to support text editing. You should use one of the Swing editable text components if you want to allow the user to edit text.
If you are working with primitive text and are using the TextLayout class to manage the shaping and positioning of the text, you can also use TextLayout to perform hit testing for text editing.