java: Bump java library versions for next release
[fw/altos] / altosuilib / AltosUIMapLine.java
1 /*
2  * Copyright © 2014 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altosuilib_3;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import java.io.*;
24 import java.lang.Math;
25 import java.awt.geom.*;
26 import java.util.*;
27 import java.util.concurrent.*;
28 import org.altusmetrum.altoslib_5.*;
29
30 public class AltosUIMapLine {
31         AltosUILatLon   start, end;
32
33         private Font    font = null;
34
35         public void set_font(Font font) {
36                 this.font = font;
37         }
38
39         private AltosUILatLon lat_lon(MouseEvent e, AltosUIMapTransform t) {
40                 return t.screen_lat_lon(e.getPoint());
41         }
42
43         public void dragged(MouseEvent e, AltosUIMapTransform t) {
44                 end = lat_lon(e, t);
45         }
46
47         public void pressed(MouseEvent e, AltosUIMapTransform t) {
48                 start = lat_lon(e, t);
49                 end = null;
50         }
51
52         private String line_dist() {
53                 String  format;
54                 AltosGreatCircle        g = new AltosGreatCircle(start.lat, start.lon,
55                                                                  end.lat, end.lon);
56                 double  distance = g.distance;
57
58                 if (AltosConvert.imperial_units) {
59                         distance = AltosConvert.meters_to_feet(distance);
60                         if (distance < 10000) {
61                                 format = "%4.0fft";
62                         } else {
63                                 distance /= 5280;
64                                 if (distance < 10)
65                                         format = "%5.3fmi";
66                                 else if (distance < 100)
67                                         format = "%5.2fmi";
68                                 else if (distance < 1000)
69                                         format = "%5.1fmi";
70                                 else
71                                         format = "%5.0fmi";
72                         }
73                 } else {
74                         if (distance < 10000) {
75                                 format = "%4.0fm";
76                         } else {
77                                 distance /= 1000;
78                                 if (distance < 100)
79                                         format = "%5.2fkm";
80                                 else if (distance < 1000)
81                                         format = "%5.1fkm";
82                                 else
83                                         format = "%5.0fkm";
84                         }
85                 }
86                 return String.format(format, distance);
87         }
88
89         public void paint(Graphics2D g, AltosUIMapTransform t) {
90                 g.setColor(Color.BLUE);
91
92                 if (start == null || end == null)
93                         return;
94
95                 Line2D.Double line = new Line2D.Double(t.screen(start),
96                                                        t.screen(end));
97
98                 g.draw(line);
99
100                 String  message = line_dist();
101                 g.setFont(font);
102                 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
103                 Rectangle2D     bounds;
104                 bounds = font.getStringBounds(message, g.getFontRenderContext());
105
106                 float x = (float) line.x1;
107                 float y = (float) line.y1 + (float) bounds.getHeight() / 2.0f;
108
109                 if (line.x1 < line.x2) {
110                         x -= (float) bounds.getWidth() + 2.0f;
111                 } else {
112                         x += 2.0f;
113                 }
114                 g.drawString(message, x, y);
115         }
116 }