From 451bf6394ddd911024e62dd223b7bf07a7d6c996 Mon Sep 17 00:00:00 2001 From: Oshgnacknak Date: Sun, 11 Jul 2021 16:36:15 +0200 Subject: [PATCH] Use ShortestPathAlgorithm from Gruphi to interact with path finding --- .../de/oshgnacknak/gruphi/DoubleAddition.java | 16 +++++ .../de/oshgnacknak/gruphi/GruphiFrame.java | 65 ++++++++++++++++++- .../de/oshgnacknak/gruphi/GruphiMain.java | 1 + 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/oshgnacknak/gruphi/DoubleAddition.java diff --git a/src/main/java/de/oshgnacknak/gruphi/DoubleAddition.java b/src/main/java/de/oshgnacknak/gruphi/DoubleAddition.java new file mode 100644 index 0000000..af9aaf2 --- /dev/null +++ b/src/main/java/de/oshgnacknak/gruphi/DoubleAddition.java @@ -0,0 +1,16 @@ +package de.oshgnacknak.gruphi; + +import h07.algebra.Monoid; + +public class DoubleAddition implements Monoid { + + @Override + public Double zero() { + return 0.0; + } + + @Override + public Double add(Double a, Double b) { + return a + b; + } +} diff --git a/src/main/java/de/oshgnacknak/gruphi/GruphiFrame.java b/src/main/java/de/oshgnacknak/gruphi/GruphiFrame.java index 3946597..6d4eb4f 100644 --- a/src/main/java/de/oshgnacknak/gruphi/GruphiFrame.java +++ b/src/main/java/de/oshgnacknak/gruphi/GruphiFrame.java @@ -1,6 +1,7 @@ package de.oshgnacknak.gruphi; import h07.graph.DirectedGraph; +import h07.graph.Path; import javax.imageio.ImageIO; import javax.swing.*; @@ -10,6 +11,8 @@ import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.io.IOException; +import java.util.Map; +import java.util.Objects; import java.util.Optional; class GruphiFrame extends JFrame { @@ -18,6 +21,7 @@ class GruphiFrame extends JFrame { private static Image loadNugget() { try { + //noinspection ConstantConditions return ImageIO.read(Gruphi.class.getResourceAsStream("/nugget.png")); } catch (IOException e) { e.printStackTrace(); @@ -36,6 +40,8 @@ class GruphiFrame extends JFrame { private final Vector cameraVel = new Vector(0, 0); private Node selected = null; + private Node startNode = null; + private Map> paths = null; private boolean running = true; private boolean nuggets = false; @@ -100,6 +106,9 @@ class GruphiFrame extends JFrame { case KeyEvent.VK_DELETE: case KeyEvent.VK_BACK_SPACE: { if (selected != null) { + if (selected == startNode) { + clearPaths(); + } graph.removeNode(selected); selected = null; } @@ -128,6 +137,10 @@ class GruphiFrame extends JFrame { nuggets = !nuggets; } break; + case KeyEvent.VK_P: { + generatePaths(); + } break; + default: break; } } @@ -173,6 +186,22 @@ class GruphiFrame extends JFrame { }); } + private void generatePaths() { + clearPaths(); + + if (selected != null) { + startNode = selected; + startNode.color = Color.GREEN; + selected = null; + + var algo = Objects.requireNonNull( + gruphi.getShortestPathsAlgorithm(), + "Did you supply a path finding implementation"); + + paths = algo.shortestPaths(graph, startNode, new DoubleAddition(), Comparable::compareTo); + } + } + public void onMousePressed(int button, Vector v) { switch (button) { case MouseEvent.BUTTON1: { @@ -204,6 +233,10 @@ class GruphiFrame extends JFrame { selected = n; selected.radius *= 1.3; selected.color = Color.RED; + + if (selected == startNode) { + clearPaths(); + } }); } break; @@ -232,10 +265,16 @@ class GruphiFrame extends JFrame { private void clearGraph() { selected = null; + clearPaths(); for (var node : graph.getAllNodes()) { graph.removeNode(node); } } + private void clearPaths() { + startNode = null; + paths = null; + } + private Optional findClickedNode(Vector v) { return graph.getAllNodes() @@ -254,8 +293,14 @@ class GruphiFrame extends JFrame { private void drawNodes(Drawable d) { d.strokeWeight(1); d.fill(Color.WHITE); - drawConnections(d); + drawConnections(d); + drawCells(d); + + drawPath(d); + } + + private void drawCells(Drawable d) { for (var node : graph.getAllNodes()) { d.fill(node.color); d.ellipse(node.pos.x, node.pos.y, node.radius * 2, node.radius * 2); @@ -267,6 +312,24 @@ class GruphiFrame extends JFrame { } } + private void drawPath(Drawable d) { + if (selected == null || paths == null || !paths.containsKey(selected)) { + return; + } + + var path = paths.get(selected); + + d.fill(Color.GREEN); + d.strokeWeight(2); + Node prev = null; + for (var node : path) { + if (prev != null) { + d.line(prev.pos.x, prev.pos.y, node.pos.x, node.pos.y); + } + prev = node; + } + } + private void drawConnections(Drawable d) { for (var node : graph.getAllNodes()) { for (var child : graph.getChildrenForNode(node)) { diff --git a/src/main/java/de/oshgnacknak/gruphi/GruphiMain.java b/src/main/java/de/oshgnacknak/gruphi/GruphiMain.java index 9e8d8e7..fb1f8d6 100644 --- a/src/main/java/de/oshgnacknak/gruphi/GruphiMain.java +++ b/src/main/java/de/oshgnacknak/gruphi/GruphiMain.java @@ -5,6 +5,7 @@ public class GruphiMain { public static void main(String[] args) { var gruphi = new GruphiBuilder() // .setDirectedGraphFactory(someFactory) + // .setShortestPathsAlgorithm(somePathFinder) .createGruphi(); var frame = new GruphiFrame(gruphi);