From c7d4dc807711ef94dc8fb8a94b22f9aae30e9751 Mon Sep 17 00:00:00 2001 From: Holt59 Date: Tue, 13 Mar 2018 22:58:27 +0100 Subject: [PATCH] Merge master. --- src/main/org/insa/algo/AlgorithmFactory.java | 2 +- .../org/insa/graphics/AlgorithmPanel.java | 5 ++- .../org/insa/graphics/NodesInputPanel.java | 6 ++-- src/main/org/insa/graphics/PathsPanel.java | 8 +++-- .../org/insa/graphics/drawing/Drawing.java | 26 +++++++++++++-- .../drawing/components/BasicDrawing.java | 33 +++++++++++++------ .../org/insa/graphics/utils/ColorUtils.java | 21 ++++++++++++ 7 files changed, 79 insertions(+), 22 deletions(-) create mode 100644 src/main/org/insa/graphics/utils/ColorUtils.java diff --git a/src/main/org/insa/algo/AlgorithmFactory.java b/src/main/org/insa/algo/AlgorithmFactory.java index 00fe9a7..9ce8036 100644 --- a/src/main/org/insa/algo/AlgorithmFactory.java +++ b/src/main/org/insa/algo/AlgorithmFactory.java @@ -83,7 +83,7 @@ public class AlgorithmFactory { Class[] params = c.getParameterTypes(); if (params.length == 1 && params[0].isAssignableFrom(data.getClass())) { c.setAccessible(true); - constructed = (AbstractAlgorithm) c.newInstance(new Object[]{ data }); + constructed = (AbstractAlgorithm) c.newInstance(new Object[] { data }); break; } } diff --git a/src/main/org/insa/graphics/AlgorithmPanel.java b/src/main/org/insa/graphics/AlgorithmPanel.java index 04acac5..e9b4b8e 100644 --- a/src/main/org/insa/graphics/AlgorithmPanel.java +++ b/src/main/org/insa/graphics/AlgorithmPanel.java @@ -34,6 +34,7 @@ import org.insa.graph.Node; import org.insa.graphics.NodesInputPanel.InputChangedEvent; import org.insa.graphics.drawing.Drawing; import org.insa.graphics.drawing.components.MapViewDrawing; +import org.insa.graphics.utils.ColorUtils; public class AlgorithmPanel extends JPanel implements DrawingChangeListener { @@ -366,12 +367,10 @@ public class AlgorithmPanel extends JPanel implements DrawingChangeListener { * @return A new NodesInputPanel containing inputs for the given names. */ protected NodesInputPanel createNodesInputPanel(String[] nodeNames) { - final Color[] nodeColors = { new Color(57, 172, 115), new Color(255, 77, 77), - new Color(77, 77, 255), new Color(77, 255, 77) }; NodesInputPanel panel = new NodesInputPanel(); panel.setAlignmentX(Component.LEFT_ALIGNMENT); for (int i = 0; i < nodeNames.length; ++i) { - panel.addTextField(nodeNames[i] + ": ", nodeColors[i % nodeColors.length]); + panel.addTextField(nodeNames[i] + ": ", ColorUtils.getColor(i)); } panel.setEnabled(false); return panel; diff --git a/src/main/org/insa/graphics/NodesInputPanel.java b/src/main/org/insa/graphics/NodesInputPanel.java index e9d6e52..bd14815 100644 --- a/src/main/org/insa/graphics/NodesInputPanel.java +++ b/src/main/org/insa/graphics/NodesInputPanel.java @@ -25,6 +25,7 @@ import org.insa.graph.Graph; import org.insa.graph.Node; import org.insa.graph.Point; import org.insa.graphics.drawing.Drawing; +import org.insa.graphics.drawing.Drawing.AlphaMode; import org.insa.graphics.drawing.DrawingClickListener; import org.insa.graphics.drawing.overlays.MarkerOverlay; @@ -247,7 +248,8 @@ public class NodesInputPanel extends JPanel MarkerOverlay tracker = markerTrackers.getOrDefault(textField, null); if (curnode != null) { if (tracker == null) { - tracker = drawing.drawMarker(curnode.getPoint(), markerColor); + tracker = drawing.drawMarker(curnode.getPoint(), markerColor, markerColor, + AlphaMode.OPAQUE); markerTrackers.put(textField, tracker); } else { @@ -412,7 +414,7 @@ public class NodesInputPanel extends JPanel MarkerOverlay tracker = markerTrackers.getOrDefault(input, null); if (tracker != null) { MarkerOverlay newMarker = this.drawing.drawMarker(tracker.getPoint(), - tracker.getColor()); + tracker.getColor(), tracker.getColor(), AlphaMode.OPAQUE); markerTrackers.put(input, newMarker); newMarker.setVisible(tracker.isVisible()); tracker.delete(); diff --git a/src/main/org/insa/graphics/PathsPanel.java b/src/main/org/insa/graphics/PathsPanel.java index 317aaea..43c5018 100644 --- a/src/main/org/insa/graphics/PathsPanel.java +++ b/src/main/org/insa/graphics/PathsPanel.java @@ -38,6 +38,7 @@ import org.insa.graph.Path; import org.insa.graph.io.BinaryPathWriter; import org.insa.graphics.drawing.Drawing; import org.insa.graphics.drawing.overlays.PathOverlay; +import org.insa.graphics.utils.ColorUtils; import org.insa.graphics.utils.FileUtils; import org.insa.graphics.utils.FileUtils.FolderType; @@ -109,14 +110,14 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh * @throws IOException If a resource was not found. * */ - public PathPanel(Path path) throws IOException { + public PathPanel(Path path, Color color) throws IOException { super(); setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS)); setBorder(BorderFactory.createCompoundBorder( BorderFactory.createMatteBorder(0, 0, 1, 0, Color.LIGHT_GRAY), new EmptyBorder(5, 0, 5, 0))); this.path = path; - this.overlay = drawing.drawPath(this.path); + this.overlay = drawing.drawPath(this.path, color); JCheckBox checkbox = new JCheckBox(); checkbox.setSelected(true); @@ -286,6 +287,7 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh /* * (non-Javadoc) + * * @see java.lang.Object#toString() */ public String toString() { @@ -310,7 +312,7 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh public void addPath(Path path) { try { - this.add(new PathPanel(path)); + this.add(new PathPanel(path, ColorUtils.getColor(this.getComponentCount()))); this.setVisible(true); this.revalidate(); this.repaint(); diff --git a/src/main/org/insa/graphics/drawing/Drawing.java b/src/main/org/insa/graphics/drawing/Drawing.java index 5cf48c2..01cff5f 100644 --- a/src/main/org/insa/graphics/drawing/Drawing.java +++ b/src/main/org/insa/graphics/drawing/Drawing.java @@ -11,6 +11,23 @@ import org.insa.graphics.drawing.overlays.PointSetOverlay; public interface Drawing { + /** + * Available fill mode for the creation of markers, see the documentation of + * each value for more details. + */ + enum AlphaMode { + + /** + * Do not use the original transparency of the inner part to fill it. + */ + OPAQUE, + + /** + * Use the original transparency of the inner part to fill it. + */ + TRANSPARENT + } + /** * Add a listener to click to this drawing. * @@ -36,14 +53,17 @@ public interface Drawing { public void clearOverlays(); /** - * Draw a marker at the given position with the given color. + * Draw a marker at the given position using the given colors and according to + * the given mode. * * @param point Position of the marker to draw. - * @param color Color of the marker to draw. + * @param outer Color for the outer part of the marker to draw. + * @param inner Color for the inner part of the marker to draw. + * @param mode Mode for filling the inner par of the marker. * * @return A MarkerOverlay instance representing the newly drawn marker. */ - public MarkerOverlay drawMarker(Point point, Color color); + public MarkerOverlay drawMarker(Point point, Color outer, Color inner, AlphaMode mode); /** * Create a new PointSetOverlay that can be used to add overlay points to this diff --git a/src/main/org/insa/graphics/drawing/components/BasicDrawing.java b/src/main/org/insa/graphics/drawing/components/BasicDrawing.java index 43c885f..902f314 100644 --- a/src/main/org/insa/graphics/drawing/components/BasicDrawing.java +++ b/src/main/org/insa/graphics/drawing/components/BasicDrawing.java @@ -120,11 +120,16 @@ public class BasicDrawing extends JPanel implements Drawing { // Image to draw private Image image; - public BasicMarkerOverlay(Point point, Color color) { + // Inner color and fill mode. + private Color innerColor; + private final AlphaMode alphaMode; + + public BasicMarkerOverlay(Point point, Color color, Color inner, AlphaMode alphaMode) { super(color); this.point = point; - this.color = color; - this.image = MarkerUtils.getMarkerForColor(color); + this.image = MarkerUtils.getMarkerForColor(color, inner, alphaMode); + this.innerColor = inner; + this.alphaMode = alphaMode; } @Override @@ -134,8 +139,9 @@ public class BasicDrawing extends JPanel implements Drawing { @Override public void setColor(Color color) { + this.innerColor = this.innerColor.equals(this.color) ? color : innerColor; super.setColor(color); - this.image = MarkerUtils.getMarkerForColor(color); + this.image = MarkerUtils.getMarkerForColor(color, this.innerColor, alphaMode); } @Override @@ -347,6 +353,7 @@ public class BasicDrawing extends JPanel implements Drawing { } this.addMouseListener(new MouseAdapter() { + @Override public void mouseClicked(MouseEvent evt) { if (zoomControls.contains(evt.getPoint())) { @@ -363,6 +370,7 @@ public class BasicDrawing extends JPanel implements Drawing { listener.mouseClicked(lonlat); } } + }); } @@ -398,6 +406,7 @@ public class BasicDrawing extends JPanel implements Drawing { /* * (non-Javadoc) + * * @see org.insa.graphics.drawing.Drawing#clear() */ @Override @@ -413,6 +422,7 @@ public class BasicDrawing extends JPanel implements Drawing { /* * (non-Javadoc) + * * @see org.insa.graphics.drawing.Drawing#clearOverlays() */ @Override @@ -456,6 +466,7 @@ public class BasicDrawing extends JPanel implements Drawing { /* * (non-Javadoc) + * * @see * org.insa.graphics.drawing.Drawing#addDrawingClickListener(org.insa.graphics. * drawing.DrawingClickListener) @@ -467,6 +478,7 @@ public class BasicDrawing extends JPanel implements Drawing { /* * (non-Javadoc) + * * @see org.insa.graphics.drawing.Drawing#removeDrawingClickListener(org.insa. * graphics.drawing.DrawingClickListener) */ @@ -475,13 +487,13 @@ public class BasicDrawing extends JPanel implements Drawing { this.drawingClickListeners.remove(listener); } - public BasicMarkerOverlay createMarker(Point point, Color color) { - return new BasicMarkerOverlay(point, color); + public BasicMarkerOverlay createMarker(Point point, Color outer, Color inner, AlphaMode mode) { + return new BasicMarkerOverlay(point, outer, inner, mode); } @Override - public MarkerOverlay drawMarker(Point point, Color color) { - BasicMarkerOverlay marker = createMarker(point, color); + public MarkerOverlay drawMarker(Point point, Color outer, Color inner, AlphaMode mode) { + BasicMarkerOverlay marker = createMarker(point, outer, inner, mode); synchronized (overlays) { this.overlays.add(marker); } @@ -646,8 +658,9 @@ public class BasicDrawing extends JPanel implements Drawing { } BasicMarkerOverlay origin = null, destination = null; if (markers && !path.isEmpty()) { - origin = createMarker(path.getOrigin().getPoint(), color); - destination = createMarker(path.getDestination().getPoint(), color); + origin = createMarker(path.getOrigin().getPoint(), color, color, AlphaMode.TRANSPARENT); + destination = createMarker(path.getDestination().getPoint(), color, color, + AlphaMode.TRANSPARENT); } BasicPathOverlay overlay = new BasicPathOverlay(points, color, origin, destination); synchronized (overlays) { diff --git a/src/main/org/insa/graphics/utils/ColorUtils.java b/src/main/org/insa/graphics/utils/ColorUtils.java new file mode 100644 index 0000000..1f4e422 --- /dev/null +++ b/src/main/org/insa/graphics/utils/ColorUtils.java @@ -0,0 +1,21 @@ +package org.insa.graphics.utils; + +import java.awt.Color; + +public class ColorUtils { + + private static final Color[] COLORS = { // List of available colors + new Color(57, 172, 115), // Forest (Green) + new Color(246, 67, 63), // Red + new Color(110, 56, 172), // Purple + new Color(53, 191, 179), // Cyan + new Color(219, 136, 48), // Orange / Brown + new Color(110, 110, 110), // Gray + new Color(56, 104, 172) // Blue + }; + + public static Color getColor(int i) { + return COLORS[i % COLORS.length]; + } + +}