Home > Articles > Programming > Graphic Programming

  • Print
  • + Share This
This chapter is from the book

Techniques for Graphical User Input

Most user interaction problems in 2D consist of determining whether the user clicked on a particular shape or text area and other forms of so-called hit testing. Two methods, Graphics2D.hit() and the Shape.contains(), are particularly useful for solving shape clicking problems. Shape.contains() can be placed in the mouseEvent methods such as mousePressed() and mouseClicked() to test if a particular shape has been selected after the desired mouse event. An example of using the contains() method is provided in Listing 3.3.

Graphics2D.hit() can be used in more or less the same fashion; however, it is necessary to pass the hit() method a rectangular area of device space in which to search for the selected object.

  • + Share This
  • 🔖 Save To Your Account