Component Location, Bounds, and Coordinates

A component's location is relative to the container in which it resides, whereas its bounds represent the actual pixel width and height of the component. If you'd like to find out the actual screen coordinates of a component, use the new method introduced in the 1.1 version of the AWT:

Point java.awt.Component.getLocationOnScreen()

Coordinates used in component methods are relative to the upper left-hand corner of the component in question. If you wish to draw a rectangle in a canvas, for example, and you specify the location of the rectangle as (10,10), the upper left-hand corner of the rectangle will be located 10 pixels below and to the right of the upper left-hand corner of the canvas.

Get Graphic Java™ 1.2, Volume I: AWT, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.