| Co-ordinate Systems | Graphics2D |
| Off By One Gotcha | Rulers |
| Scaling Gotcha | Learning More |
| Relative Origin | Links |
| Discovering The Screen Size |
| Co-ordinate Systems | ||||
|---|---|---|---|---|
| Java Drawing | Cartesian | Polar | PostScript | Gore |
![]() |
![]() |
![]() |
![]() |
![]() |
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.
// 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, 5x4 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.
// 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 );
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();
![]() |
and suggestions to improve this page to Roedy Green : | ||
| Canadian Mind Products | |||
| mindprod.com IP:[65.110.21.43] | |||
| Your face IP:[38.103.63.62] | ![]() | ||
| You are visitor number 15,838. | |||
| You can get a fresh 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 | ||