Global Positioning System Reference
In-Depth Information
intuitive next step is to calculate and draw the grid on the screen. At this
point, the developer has to be aware that a Swing component can be con-
structed at any time|yet, it is impossible to request its actual size before
it is added to a top-level container. Only after all components have been
supplied, Swing can start laying out the components with frame.pack() .
Without the grid defined in the constructor, there is no way to draw ge-
ometric primitives. Therefore the programmer has to listen to the commu-
nication inside the framework. The setBounds(x,y,w,h) method is invoked
on the JComponent by the Swing Framework whenever the assigned space
has changed. By calling the super method, the framework can proceed
with its normal procedure, and the specialization can be added afterwards.
/* trigger the (re)calculation of mapscales.*/
public void setBounds(int x, int y, int width, int height)
{
super.setBounds(x, y, width, height);
// add customized code here
calculateScales();
:
Now, the calculateScales() method has access to the component's pixel
size and the decimal grid, which is the prerequisite to draw with decimal
values. The method will not be described in detail. Basically, the hori-
zontal and vertical scales are being managed by additional visual Swing
components using predefined Swing constants:
class MapScalePanel extends JPanel implements SwingConstants
The main purpose of the two map scales is to provide a conversion of
x- and y-values from pixel to decimal and vice versa. The two methods
return the (x;y) pixels for spatial (lat,lon) coordinates:
int horPixel = horScale.dec2pix( pos.getLongitude());
int verPixel = verScale.dec2pix( pos.getLatitude() );
Vice versa, any position on the screen can be calculated to real-world co-
ordinates:
public void mouseClicked(MouseEvent e) {
double lon = horScale.pix2dec(e.getX());
double lat = verScale.pix2dec(e.getY());
:
The methods take into account that Swing counts vertical pixels from top to
bottom (reading direction), while the (longitude) values grow from bottom
to top.
Note that the map scales are only visible from inside the package, since
they have to and should only be used in conjunction with the map panel.
 
Search WWH ::




Custom Search