coordinates : Java Glossary


Java’s basic AWT (Advanced Windowing Toolkit) Graphics coordinate system is not the usual x,y you learned in high school. The origin 0,0 is in the top left corner of the Canvas/Component/ Container, not the entire screen, not the bottom left where Descartes put it. x gets larger as you go right, as you would expect, but y gets larger as you go down, which is not such a surprise if you have used PostScript and think about the order you write text on a page, top to bottom. The units for both x and y are pixels.
Co-ordinate Systems Graphics2D
Off By One Gotcha Rulers
Scaling Gotcha Learning More
Relative Origin Links
Discovering The Screen Size

Co-ordinate Systems

Co-ordinate Systems
Java Drawing Cartesian Polar PostScript Gore
Java drawing coordinates cartesian coordinates polar coordinates PostScript coordinates Gore coordinates

Interconverting the various systems is easy for anyone who remembers their grade 11 trigonometry. The only one you might have difficulty with is polar to Cartesian and back. For that see the polar coordinates entry.

For Java, units are integer pixels. For PostScript units are floating point 1/72 of an inch, close to a printer’s point of 1/72.27 inch.

For mouse events, drawing geometric shapes and placing Components with absolute coordinates in the null layout, the origin 0,0 is the upper left corner.

When you place an object on the screen, you specify the location of its upper left corner. When drawing an ellipse, you would specify the position of the upper left corner of a bounding rectangle surrounding the ellipse, not the center of the ellipse. When placing text you specify the top left corner of where you want the text to appear.

The 0,0 upper left corner is hidden under the menu bar of a Frame or JFrame. You have to account for it and leave about 24 pixels of additional height in designing your layouts.

For GridBagLayouts the origin 0,0 is the upper left corner of the enclosing container, not the entire screen or the application JFrame. In GridBagLayouts the first coordinate GridBagConstraints.gridx measures columns — horizontal displacement to the right in cell widths and the second GridBagConstraints.gridy measures rows — vertical displacement downward in cells, GridBagLayout then adjusts the size of the various rows and columns and precisely positions components within the rough cells. The programmer usually does not work with precise placement. She uses layouts to specify relative position and allows the LayoutManager to compute the exact placement.

Routines often ask for the width and height of a bounding box, in that order. Ordinary English is confused about the usual order. We often say height and width, then describe the dimensions of a piece of paper as × 11 width × height.

Off By One Gotcha

drawRectangle and brothers deliberately draw rectangles one pixel taller and wider than requested. It turns out this extra pixel is deliberate and supposedly unavoidable. The rationale is that you specify the path of an idealised box drawn with infinitely thin lines and the pen hangs down and to the right.
// draw a pink rectangle 4x3

gr.setColor( Color.PINK );

// x, y, width, height
gr.fillRect( 0, 0, 4, 3 );
And here is what you get: The red region represents the theoretical rectangle 4 x 3, and the pink rectangle shows what you actually get, 5 × 4 because the pen hangs down and the right of the theoretical line. The model is more suited to a pen plotter than a pixel display.

coordinates gotcha

Scaling Gotcha

Because coordinates are measured in hardware-dependent pixels, images generated by Java programs will be smaller when they are displayed on high resolution displays. At this point, no one is worrying about scaling to account for this resolution quality evolution despite the fact PostScript has been tackling the problem for over a decade. On an ultra-high res screen, unless Java evolves a scaling mechanism, applications will appear the size of postage stamps, or smaller.

Relative Origin

When you try do a setLocation on JFrames, or JWindows, you are working in screen coordinates, even when the JWindow has a parent. When you place Components, you are working in the coordinate system of its Container. The methods javax.swing.SwingUtilities.convertPoint, convertPointToScreen and convertPointFromScreen may be helpful then you are trying to convert between the coordinate systems of different components or the screen as a whole.

// You don't need SwingUtilities to do simple
// relative frame placement.
// Place daughterFrame a little to the right of motherFrame
int x = motherFrame.getX() + motherFrame.getWidth() + 40;
int y = motherFrame.getY();
daughterFrame.setLocation ( x , y );

Discovering The Screen Size

Here is how to find the size of the screen in pixels.


With the advent of Graphics2D and AffineTranform, Java’s coordinate system becomes more flexible, something similar to what PostScript offers. It is a convenience layer built on top of the integer pixel scheme. You can transform the coordinates to go in any direction you want that aren’t even necessarily orthogonal. You can use a mixture of int, float and double coordinates. And you can map the coordinate system to be any thing you want. You can work in user coordinates, something meaningful to you and have AffineTransform convert it to pixels.

Learning More

Oracle’s Javadoc on SwingUtilities coordinate methods : available:

This page is posted
on the web at:

Optional Replicator mirror
on local hard disk J:

Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

Your face IP:[]
You are visitor number