X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Fgui%2Fmap%2FMapCanvas.java;h=42215209ba3bfccf599fa2d9c000ebe1f89cf49f;hb=0a2480df5845e2d7190dfdec9b2653b1609e853d;hp=a62b10855ce14d25299f41e891f1b432999c4aff;hpb=326f489e36aa7f235bc19409a57bf4955cd50f24;p=GpsPrune.git diff --git a/tim/prune/gui/map/MapCanvas.java b/tim/prune/gui/map/MapCanvas.java index a62b108..4221520 100644 --- a/tim/prune/gui/map/MapCanvas.java +++ b/tim/prune/gui/map/MapCanvas.java @@ -1,5 +1,6 @@ package tim.prune.gui.map; +import java.awt.AlphaComposite; import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Color; @@ -10,6 +11,7 @@ import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; +import java.awt.RenderingHints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; @@ -58,6 +60,8 @@ import tim.prune.function.compress.MarkPointsInRectangleFunction; import tim.prune.function.edit.FieldEdit; import tim.prune.function.edit.FieldEditList; import tim.prune.gui.IconManager; +import tim.prune.gui.TripleStateCheckBox; +import tim.prune.gui.colour.PointColourer; import tim.prune.tips.TipManager; /** @@ -84,6 +88,8 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe private MapTileManager _tileManager = new MapTileManager(this); /** Image to display */ private BufferedImage _mapImage = null; + /** Second image for drawing track (only needed for alpha blending) */ + private BufferedImage _trackImage = null; /** Slider for transparency */ private JSlider _transparencySlider = null; /** Checkbox for scale bar */ @@ -93,7 +99,7 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe /** Checkbox for autopan */ private JCheckBox _autopanCheckBox = null; /** Checkbox for connecting track points */ - private JCheckBox _connectCheckBox = null; + private TripleStateCheckBox _connectCheckBox = null; /** Checkbox for enable edit mode */ private JCheckBox _editmodeCheckBox = null; /** Right-click popup menu */ @@ -237,8 +243,11 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe _autopanCheckBox.setFocusable(false); // stop button from stealing keyboard focus _topPanel.add(_autopanCheckBox); // Add checkbox button for connecting points or not - _connectCheckBox = new JCheckBox(IconManager.getImageIcon(IconManager.POINTS_DISCONNECTED_BUTTON), true); - _connectCheckBox.setSelectedIcon(IconManager.getImageIcon(IconManager.POINTS_CONNECTED_BUTTON)); + _connectCheckBox = new TripleStateCheckBox(); + _connectCheckBox.setIcon(0, IconManager.getImageIcon(IconManager.POINTS_CONNECTED_BUTTON)); + _connectCheckBox.setIcon(1, IconManager.getImageIcon(IconManager.POINTS_DISCONNECTED_BUTTON)); + _connectCheckBox.setIcon(2, IconManager.getImageIcon(IconManager.POINTS_HIDDEN_BUTTON)); + _connectCheckBox.setCurrentState(0); _connectCheckBox.setOpaque(false); _connectCheckBox.setToolTipText(I18nManager.getText("menu.map.connect")); _connectCheckBox.addItemListener(itemListener); @@ -484,7 +493,7 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe else if (px > (getWidth()-PAN_DISTANCE)) { panX = AUTOPAN_DISTANCE + px - getWidth(); } - if (py < PAN_DISTANCE) { + if (py < (2*PAN_DISTANCE)) { panY = py - AUTOPAN_DISTANCE; } if (py > (getHeight()-PAN_DISTANCE)) { @@ -505,8 +514,10 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe _mapImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); } - // Clear map Graphics g = _mapImage.getGraphics(); + // Set antialiasing according to config + ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, + Config.getConfigBoolean(Config.KEY_ANTIALIAS) ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF); // Clear to background g.setColor(Config.getColourScheme().getColour(ColourScheme.IDX_BACKGROUND)); g.fillRect(0, 0, getWidth(), getHeight()); @@ -580,24 +591,58 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe } } - // Paint the track points on top - int pointsPainted = 1; - try + // Work out track opacity according to slider + final float[] opacities = {1.0f, 0.75f, 0.5f, 0.3f, 0.15f, 0.0f}; + float trackOpacity = 1.0f; + if (_transparencySlider.getValue() < 0) { + trackOpacity = opacities[-1 - _transparencySlider.getValue()]; + } + + if (trackOpacity > 0.0f) { - pointsPainted = paintPoints(g); + // Paint the track points on top + int pointsPainted = 1; + try + { + if (trackOpacity > 0.9f) + { + // Track is fully opaque, just draw it directly + pointsPainted = paintPoints(g); + _trackImage = null; + } + else + { + // Track is partly transparent, so use a separate BufferedImage + if (_trackImage == null || _trackImage.getWidth() != getWidth() || _trackImage.getHeight() != getHeight()) + { + _trackImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); + } + // Clear to transparent + Graphics2D gTrack = _trackImage.createGraphics(); + gTrack.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f)); + gTrack.fillRect(0, 0, getWidth(), getHeight()); + gTrack.setPaintMode(); + // Draw the track onto this separate image + pointsPainted = paintPoints(gTrack); + ((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, trackOpacity)); + g.drawImage(_trackImage, 0, 0, null); + } + } + catch (NullPointerException npe) {} // ignore, probably due to data being changed during drawing + catch (ArrayIndexOutOfBoundsException obe) {} // also ignore + + // Zoom to fit if no points found + if (pointsPainted <= 0 && _checkBounds) + { + zoomToFit(); + _recalculate = true; + repaint(); + } } - catch (NullPointerException npe) {} // ignore, probably due to data being changed during drawing - catch (ArrayIndexOutOfBoundsException obe) {} // also ignore // free g g.dispose(); - // Zoom to fit if no points found - if (pointsPainted <= 0 && _checkBounds) { - zoomToFit(); - _recalculate = true; - repaint(); - } _checkBounds = false; // enable / disable transparency slider _transparencySlider.setEnabled(showMap); @@ -613,15 +658,12 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe { // Set up colours final ColourScheme cs = Config.getColourScheme(); - final int[] opacities = {255, 190, 130, 80, 40, 0}; - int opacity = 255; - if (_transparencySlider.getValue() < 0) - opacity = opacities[-1 - _transparencySlider.getValue()]; - final Color pointColour = makeTransparentColour(cs.getColour(ColourScheme.IDX_POINT), opacity); - final Color rangeColour = makeTransparentColour(cs.getColour(ColourScheme.IDX_SELECTION), opacity); - final Color currentColour = makeTransparentColour(cs.getColour(ColourScheme.IDX_PRIMARY), opacity); - final Color secondColour = makeTransparentColour(cs.getColour(ColourScheme.IDX_SECONDARY), opacity); - final Color textColour = makeTransparentColour(cs.getColour(ColourScheme.IDX_TEXT), opacity); + final Color pointColour = cs.getColour(ColourScheme.IDX_POINT); + final Color rangeColour = cs.getColour(ColourScheme.IDX_SELECTION); + final Color currentColour = cs.getColour(ColourScheme.IDX_PRIMARY); + final Color secondColour = cs.getColour(ColourScheme.IDX_SECONDARY); + final Color textColour = cs.getColour(ColourScheme.IDX_TEXT); + final PointColourer pointColourer = _app.getPointColourer(); final int winWidth = getWidth(); final int winHeight = getHeight(); @@ -643,7 +685,9 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe // draw track points inG.setColor(pointColour); int prevX = -1, prevY = -1; - boolean connectPoints = _connectCheckBox.isSelected(); + final int connectState = _connectCheckBox.getCurrentState(); + final boolean drawLines = (connectState % 2) == 0; // 0 or 2 + final boolean drawPoints = (connectState <= 1); // 0 or 1 boolean prevPointVisible = false, currPointVisible = false; boolean anyWaypoints = false; boolean isWaypoint = false; @@ -659,26 +703,34 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe currPointVisible = px >= 0 && px < winWidth && py >= 0 && py < winHeight; isWaypoint = _track.getPoint(i).isWaypoint(); anyWaypoints = anyWaypoints || isWaypoint; - if (currPointVisible) + if (!isWaypoint) { - if (!isWaypoint) + if ((currPointVisible || prevPointVisible) && drawPoints) { - // Draw rectangle for track point + // For track points, work out which colour to use if (_track.getPoint(i).getDeleteFlag()) { inG.setColor(currentColour); } - else { + else if (pointColourer != null) + { // use the point colourer if there is one + Color trackColour = pointColourer.getColour(i); + inG.setColor(trackColour); + } + else + { inG.setColor(pointColour); } - inG.drawRect(px-2, py-2, 3, 3); - pointsPainted++; + + // Draw rectangle for track point if it's visible + if (currPointVisible) + { + inG.drawRect(px-2, py-2, 3, 3); + pointsPainted++; + } } - } - if (!isWaypoint) - { + // Connect track points if either of them are visible - if (connectPoints && (currPointVisible || prevPointVisible) - && !(prevX == -1 && prevY == -1) + if (drawLines && !(prevX == -1 && prevY == -1) && !_track.getPoint(i).getSegmentStart()) { inG.drawLine(prevX, prevY, px, py); @@ -859,18 +911,6 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe } } - /** - * Make a semi-transparent colour for drawing with - * @param inColour base colour (fully opaque) - * @param inOpacity opacity where 0=invisible and 255=full - * @return new colour object - */ - private static Color makeTransparentColour(Color inColour, int inOpacity) - { - if (inOpacity > 240) return inColour; - return new Color(inColour.getRed(), inColour.getGreen(), inColour.getBlue(), inOpacity); - } - /** * Inform that tiles have been updated and the map can be repainted * @param inIsOk true if data loaded ok, false for error @@ -1050,8 +1090,7 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe else if (_drawMode == MODE_DRAW_POINTS_CONT) { DataPoint point = createPointFromClick(inE.getX(), inE.getY()); - _app.createPoint(point); - point.setSegmentStart(false); + _app.createPoint(point, false); // not a new segment } } else if (inE.getClickCount() == 2)