From 37c78cf5a58d41d54b3b15d33140924b60403868 Mon Sep 17 00:00:00 2001 From: Oshgnacknak Date: Sun, 5 Sep 2021 21:49:23 +0200 Subject: [PATCH] Allow user to specify weight for new connections --- README.md | 6 +++++ .../de/oshgnacknak/gruphi/GruphiFrame.java | 22 ++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 34fbf38..a5d2291 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,12 @@ Startknoten (grün) auswählen löscht Pfäde. - Escape, `Q`: Beenden +- `U`, `I`: +Gewichtung für die nächste Kante verkleinern/vergrößern + +- `O`: +Gewichtung für die nächste Kante auf 1 zurücksetzten + ## Wenn Knoten ausgewählt (rot) - WASD: diff --git a/src/main/java/de/oshgnacknak/gruphi/GruphiFrame.java b/src/main/java/de/oshgnacknak/gruphi/GruphiFrame.java index 611a3d6..c3d57b7 100644 --- a/src/main/java/de/oshgnacknak/gruphi/GruphiFrame.java +++ b/src/main/java/de/oshgnacknak/gruphi/GruphiFrame.java @@ -44,6 +44,7 @@ class GruphiFrame extends JFrame { private Map> paths = null; private boolean running = true; private boolean nuggets = false; + private double weight = 1.0; GruphiFrame(Gruphi gruphi) { super("Gruphi - The Graph GUI - By Osh"); @@ -141,6 +142,18 @@ class GruphiFrame extends JFrame { generatePaths(); } break; + case KeyEvent.VK_I: { + weight = Math.min(weight*1.01, 10); + } break; + + case KeyEvent.VK_U: { + weight = Math.max(weight/1.01, 0.1); + } break; + + case KeyEvent.VK_O: { + weight = 1.0; + } break; + default: break; } } @@ -212,7 +225,7 @@ class GruphiFrame extends JFrame { if (graph.getChildrenForNode(selected).contains(n)) { graph.disconnectNodes(selected, n); } else { - graph.connectNodes(selected, 1.0, n); + graph.connectNodes(selected, weight, n); } } else { selected.pos = v; @@ -288,9 +301,12 @@ class GruphiFrame extends JFrame { public void draw(Drawable d) { d.fill(Color.BLACK); + d.rect(0, 0, getWidth(), getHeight()); panningAndZooming.draw(d, () -> drawNodes(d)); + + d.text(10, 10, String.format("Weight: %.2f", weight)); } private void drawNodes(Drawable d) { @@ -323,7 +339,6 @@ class GruphiFrame extends JFrame { var path = paths.get(selected); d.fill(Color.GREEN); - d.strokeWeight(2); Node prev = null; for (var node : path) { if (prev != null) { @@ -336,6 +351,7 @@ class GruphiFrame extends JFrame { private void drawConnections(Drawable d) { for (var node : graph.getAllNodes()) { for (var child : graph.getChildrenForNode(node)) { + d.strokeWeight(2 * graph.getArcWeightBetween(node, child)); d.line(node.pos.x, node.pos.y, child.pos.x, child.pos.y); drawConnectionArrowHead(d, node, child); } @@ -346,7 +362,7 @@ class GruphiFrame extends JFrame { var v = child.pos .copy() .sub(node.pos); - var r = 4; + var r = 4 * graph.getArcWeightBetween(node, child); var p = v.copy() .setMag(v.mag() - child.radius) .add(node.pos);