Main Page

Previous Next

Coordinate Systems in Components

In Chapter 15, we saw how your computer screen has a coordinate system that is used to define the position and size of a window. We also saw how we can add components to a container with their position established by a layout manager. This coordinate system is analogous to the screen coordinate system. The origin is at the top-left corner of the container, with the positive x-axis running horizontally from left to right, and the positive y-axis running from top to bottom. The positions of buttons in a JWindow or a JFrame object are specified as a pair of (x, y) pixel coordinates, relative to the origin at the top-left corner of the container object on the screen. Below you can see the coordinate system for the Sketcher application window.

Click To expand

Of course, the layered pane for the window object will have its own coordinate system with the origin in the top-left corner of the pane, and this is used to position the menu and the content pane. The content pane will have its own coordinate system too that will be used to position the components it contains.

It's not just containers and windows that have their own coordinate system: each JButton object also has its own system, as do JToolBar objects. In fact, every component has its own coordinate system.

Click To expand

It's clear that a container needs a coordinate system for specifying the positions of the components it contains. You also need a coordinate system to draw on a component – to draw a line for instance you need to be able to specify where it begins and ends in relation to the component – and while the coordinate system here is similar to that used for positioning components in a container, it's not exactly the same. It's more complicated when you are drawing – but for very good reasons. Let's see how the coordinate system for drawing works.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor