X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=altosuilib%2FAltosSiteMapTile.java;h=f8b924a82cd7444ed74abf08a380271eb62a0a5b;hp=1046d6bd0191af519a608f2a62a5e752a676b0c7;hb=bf684a4c290573a3aa627fd8ddf6f6ebbe5fa057;hpb=0a6c76fc0525d6588a1d88127f0085f13a02f1af diff --git a/altosuilib/AltosSiteMapTile.java b/altosuilib/AltosSiteMapTile.java index 1046d6bd..f8b924a8 100644 --- a/altosuilib/AltosSiteMapTile.java +++ b/altosuilib/AltosSiteMapTile.java @@ -21,10 +21,10 @@ import java.awt.*; import java.awt.image.*; import javax.swing.*; import javax.imageio.*; -import java.awt.geom.Point2D; -import java.awt.geom.Line2D; +import java.awt.geom.*; import java.io.*; import java.util.*; +import java.awt.RenderingHints.*; import org.altusmetrum.altoslib_4.*; class AltosPoint { @@ -40,15 +40,46 @@ class AltosPoint { public class AltosSiteMapTile extends JComponent { int px_size; File file; + int status; Point2D.Double boost; Point2D.Double landed; + Line2D.Double line; + double line_course; + double line_dist; LinkedList points; - public void loadMap(File pngFile) { + public synchronized void queue_repaint() { + if (SwingUtilities.isEventDispatchThread()) + repaint(); + else { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + repaint(); + } + }); + } + } + + public void load_map(File pngFile) { file = pngFile; - repaint(); + queue_repaint(); + } + + private Font font = null; + + public void set_font(Font font) { + this.font = font; + this.status = AltosSiteMapCache.success; + } + + public void set_status(int status) { + if (status != this.status || file != null) { + file = null; + this.status = status; + queue_repaint(); + } } public void clearMap() { @@ -56,6 +87,8 @@ public class AltosSiteMapTile extends JComponent { landed = null; points = null; file = null; + status = AltosSiteMapCache.success; + line = null; } static Color stateColors[] = { @@ -78,22 +111,95 @@ public class AltosSiteMapTile extends JComponent { public void set_boost(Point2D.Double boost) { this.boost = boost; - repaint(); + queue_repaint(); } - public void paint(Graphics g) { - Graphics2D g2d = (Graphics2D) g; - AltosPoint prev = null; - Image img = null; + public void set_line(Line2D.Double line, double distance) { + this.line = line; + line_dist = distance; + queue_repaint(); + } - if (file != null) - img = AltosSiteMapCache.get_image(this, file, px_size, px_size); + private String line_dist() { + String format; + double distance = line_dist; - if (img != null) { - g2d.drawImage(img, 0, 0, null); + if (AltosConvert.imperial_units) { + distance = AltosConvert.meters_to_feet(distance); + if (distance < 10000) { + format = "%4.0fft"; + } else { + distance /= 5280; + if (distance < 10) + format = "%5.3fmi"; + else if (distance < 100) + format = "%5.2fmi"; + else if (distance < 1000) + format = "%5.1fmi"; + else + format = "%5.0fmi"; + } } else { + if (distance < 10000) { + format = "%4.0fm"; + } else { + distance /= 1000; + if (distance < 100) + format = "%5.2fkm"; + else if (distance < 1000) + format = "%5.1fkm"; + else + format = "%5.0fkm"; + } + } + return String.format(format, distance); + } + + int painting_serial; + int painted_serial; + + public void paint_graphics(Graphics2D g2d, Image image, int serial) { + + if (serial < painted_serial) + return; + + painted_serial = serial; + + if (image != null) { + AltosSiteMap.debug_component(this, "paint_graphics"); + g2d.drawImage(image, 0, 0, null); + } else { + AltosSiteMap.debug_component(this, "erase_graphics"); g2d.setColor(Color.GRAY); g2d.fillRect(0, 0, getWidth(), getHeight()); + String message = null; + switch (status) { + case AltosSiteMapCache.loading: + message = "Loading..."; + break; + case AltosSiteMapCache.bad_request: + message = "Internal error"; + break; + case AltosSiteMapCache.failed: + message = "Network error, check connection"; + break; + case AltosSiteMapCache.forbidden: + message = "Too many requests, try later"; + break; + } + if (message != null && font != null) { + g2d.setFont(font); + g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + Rectangle2D bounds; + bounds = font.getStringBounds(message, g2d.getFontRenderContext()); + + float x = getWidth() / 2.0f; + float y = getHeight() / 2.0f; + x = x - (float) bounds.getWidth() / 2.0f; + y = y + (float) bounds.getHeight() / 2.0f; + g2d.setColor(Color.BLACK); + g2d.drawString(message, x, y); + } } g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, @@ -101,6 +207,7 @@ public class AltosSiteMapTile extends JComponent { g2d.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); if (points != null) { + AltosPoint prev = null; for (AltosPoint point : points) { if (prev != null) { if (0 <= point.state && point.state < stateColors.length) @@ -118,21 +225,65 @@ public class AltosSiteMapTile extends JComponent { g2d.setColor(Color.BLACK); draw_circle(g2d, landed); } + + if (line != null) { + g2d.setColor(Color.BLUE); + g2d.draw(line); + + String message = line_dist(); + g2d.setFont(font); + g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + Rectangle2D bounds; + bounds = font.getStringBounds(message, g2d.getFontRenderContext()); + + float x = (float) line.x1; + float y = (float) line.y1 + (float) bounds.getHeight() / 2.0f; + + if (line.x1 < line.x2) { + x -= (float) bounds.getWidth() + 2.0f; + } else { + x += 2.0f; + } + g2d.drawString(message, x, y); + } + } + + public void paint(Graphics g) { + Graphics2D g2d = (Graphics2D) g; + Image image = null; + boolean queued = false; + + AltosSiteMap.debug_component(this, "paint"); + + ++painting_serial; + + if (file != null) { + AltosSiteMapImage aimage; + + aimage = AltosSiteMapCache.get_image(this, file, px_size, px_size); + if (aimage != null) { + if (aimage.validate(painting_serial)) + image = aimage.image; + else + queued = true; + } + } + if (!queued) + paint_graphics(g2d, image, painting_serial); } - public synchronized void show(AltosState state, AltosListenerState listener_state, - Point2D.Double last_pt, Point2D.Double pt) + public void show(int state, Point2D.Double last_pt, Point2D.Double pt) { if (points == null) points = new LinkedList(); - points.add(new AltosPoint(pt, state.state)); + points.add(new AltosPoint(pt, state)); - if (state.state == AltosLib.ao_flight_boost && boost == null) + if (state == AltosLib.ao_flight_boost && boost == null) boost = pt; - if (state.state == AltosLib.ao_flight_landed && landed == null) + if (state == AltosLib.ao_flight_landed && landed == null) landed = pt; - repaint(); + queue_repaint(); } public AltosSiteMapTile(int in_px_size) {