Java Glossary : co-ordinates

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 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

You are here : home : Java Glossary : C words : co-ordinates.

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.

Java Drawing Cartesian Polar PostScript
drawing_coordinates cartesian_coordinates poral_coordinates cartesian_coordinates

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 8½x11 width x height.

drawRectangle and brothers deliberately draw rectangles one pixel taller and wider than requested. I am told if you understand the abstract drawing model the AWT uses, it turns out this extra pixel is deliberate and 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, at least one pixel thick.

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 appeart the size of postage stamps, or smaller.

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.

// 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 (  , y );

Here is how to find the size of the screen.

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

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.


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 1132.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/coordinates.html J:\mindprod\jgloss\coordinates.html