co-ordinates : Java Glossary

home page C words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all) ©1996-2010 Roedy Green, Canadian Mind Products
co-ordinates
Java’s basic AWT Graphics co-ordinate 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 co-ordinates cartesian co-ordinates polar co-ordinates PostScript co-ordinates Gore co-ordinates

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 co-ordinates 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 co-ordinates 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 co-ordinate 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
// 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.

co-ordinates gotcha

Scaling Gotcha

Because co-ordinates 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 co-ordinates, even when the JWindow has a parent. When you place Components, you are working in the co-ordinate system of its Container. The methods javax.swing.SwingUtilities.convertPoint, convertPointToScreen, and convertPointFromScreen may be helpful then you are trying to convert between the co-ordinate 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.
import java.awt.Dimension;
import java.awt.Toolkit;

...

// Get screen size in pixels, width by height.
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

// Get screen resolution in dots per inch.
// The Americans who develop Java have not noticed the world has gone metric.
int dpi = Toolkit.getDefaultToolkit().getScreenResolution();

Graphics2D

With the advent of Graphics2D and AffineTranform, Java’s co-ordinate 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 co-ordinates to go in any direction you want that aren’t even necessarily orthogonal. You can use a mixture of int, float and double co-ordinates. And you can map the co-ordinate system to be any thing you want. You can work in user co-ordinates, something meaningful to you, and have AffineTransform convert it to pixels.

Learning More

Sun’s Javadoc on SwingUtilies co-ordinate methods : available:
Sun’s JDK Technote Guide on 2D : available:

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/coordinates.html J:\mindprod\jgloss\coordinates.html
CMP logofeedback Please email your feedback for publication, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.191.110]
You are visitor number 21,025.