Draw arrows to indicate the direction of connections
This commit is contained in:
@ -42,6 +42,20 @@ public class Canvas extends JPanel {
|
|||||||
public void line(double x1, double y1, double x2, double y2) {
|
public void line(double x1, double y1, double x2, double y2) {
|
||||||
g.drawLine((int) x1, (int) y1, (int) x2, (int) y2);
|
g.drawLine((int) x1, (int) y1, (int) x2, (int) y2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void rotated(double theta, double x, double y, Runnable r) {
|
||||||
|
g.rotate(theta, x, y);
|
||||||
|
r.run();
|
||||||
|
g.rotate(-theta, x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void triangle(double x1, double y1, double x2, double y2, double x3, double y3) {
|
||||||
|
int[] xs = {(int) x1, (int) x2, (int) x3};
|
||||||
|
int[] ys = {(int) y1, (int) y2, (int) y3};
|
||||||
|
g.fillPolygon(xs, ys, 3);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,4 +13,8 @@ public interface Drawable {
|
|||||||
void ellipse(double x, double y, double w, double h);
|
void ellipse(double x, double y, double w, double h);
|
||||||
|
|
||||||
void line(double x1, double y1, double x2, double y2);
|
void line(double x1, double y1, double x2, double y2);
|
||||||
|
|
||||||
|
void rotated(double theta, double x, double y, Runnable r);
|
||||||
|
|
||||||
|
void triangle(double x1, double y1, double x2, double y2, double x3, double y3);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package de.oshgnacknak.gruphi;
|
package de.oshgnacknak.gruphi;
|
||||||
|
|
||||||
import h07.graph.DirectedGraph;
|
import h07.graph.DirectedGraph;
|
||||||
|
import h07.graph.EmptyGraphFactory;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
@ -8,7 +9,6 @@ import java.awt.event.KeyAdapter;
|
|||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseAdapter;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.awt.geom.Point2D;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
class Gruphi extends JFrame {
|
class Gruphi extends JFrame {
|
||||||
@ -20,17 +20,16 @@ class Gruphi extends JFrame {
|
|||||||
DirectedGraph<Node, Double> graph = newGraph();
|
DirectedGraph<Node, Double> graph = newGraph();
|
||||||
|
|
||||||
MazeGenerator<Node> mazeGenerator = new MazeGenerator<>(graph, (a, b) ->
|
MazeGenerator<Node> mazeGenerator = new MazeGenerator<>(graph, (a, b) ->
|
||||||
a.pos.distance(b.pos) <= NEIGHBOUR_DISTANCE);
|
a.pos.dist(b.pos) <= NEIGHBOUR_DISTANCE);
|
||||||
|
|
||||||
Node selected = null;
|
Node selected = null;
|
||||||
Point2D.Double vel = new Point2D.Double(0, 0);
|
Vector vel = new Vector(0, 0);
|
||||||
private boolean running = true;
|
private boolean running = true;
|
||||||
|
|
||||||
Gruphi() {
|
Gruphi() {
|
||||||
super("Gruphi - The Graph GUI - By Osh");
|
super("Gruphi - The Graph GUI - By Osh");
|
||||||
|
|
||||||
var canvas = new Canvas(this::draw);
|
add(new Canvas(this::draw));
|
||||||
add(canvas);
|
|
||||||
pack();
|
pack();
|
||||||
setLocationRelativeTo(null);
|
setLocationRelativeTo(null);
|
||||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||||
@ -58,23 +57,21 @@ class Gruphi extends JFrame {
|
|||||||
graph.addNode(new Node(e.getX(), e.getY()));
|
graph.addNode(new Node(e.getX(), e.getY()));
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case MouseEvent.BUTTON3: {
|
case MouseEvent.BUTTON3: {
|
||||||
if (selected != null) {
|
if (selected != null) {
|
||||||
selected.color = Node.COLOR;
|
selected.color = Node.COLOR;
|
||||||
selected.radius = Node.RADIUS;
|
selected.radius = Node.RADIUS;
|
||||||
selected = null;
|
selected = null;
|
||||||
} else {
|
}
|
||||||
findClickedNode(e)
|
findClickedNode(e)
|
||||||
.ifPresent(n -> {
|
.ifPresent(n -> {
|
||||||
selected = n;
|
selected = n;
|
||||||
selected.radius *= 2;
|
selected.radius *= 1.3;
|
||||||
selected.color = Color.WHITE;
|
|
||||||
});
|
|
||||||
if (selected != null) {
|
|
||||||
selected.color = Color.RED;
|
selected.color = Color.RED;
|
||||||
}
|
});
|
||||||
}
|
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -205,11 +202,25 @@ class Gruphi extends JFrame {
|
|||||||
d.fill(Color.BLACK);
|
d.fill(Color.BLACK);
|
||||||
d.rect(0, 0, getWidth(), getHeight());
|
d.rect(0, 0, getWidth(), getHeight());
|
||||||
|
|
||||||
d.strokeWeight(2);
|
d.strokeWeight(1);
|
||||||
d.fill(Color.WHITE);
|
d.fill(Color.WHITE);
|
||||||
for (var node : graph.getAllNodes()) {
|
for (var node : graph.getAllNodes()) {
|
||||||
for (var child : graph.getChildrenForNode(node)) {
|
for (var child : graph.getChildrenForNode(node)) {
|
||||||
d.line(node.pos.x, node.pos.y, child.pos.x, child.pos.y);
|
d.line(node.pos.x, node.pos.y, child.pos.x, child.pos.y);
|
||||||
|
|
||||||
|
var v = child.pos
|
||||||
|
.copy()
|
||||||
|
.sub(node.pos);
|
||||||
|
var r = 4;
|
||||||
|
var p = v.copy()
|
||||||
|
.setMag(v.mag() - child.radius - r)
|
||||||
|
.add(node.pos);
|
||||||
|
|
||||||
|
d.rotated(v.angle(), p.x, p.y, () ->
|
||||||
|
d.triangle(
|
||||||
|
p.x+r, p.y,
|
||||||
|
p.x-r, p.y-r,
|
||||||
|
p.x-r, p.y+r));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -238,13 +249,12 @@ class Gruphi extends JFrame {
|
|||||||
|
|
||||||
void update() {
|
void update() {
|
||||||
if (selected != null) {
|
if (selected != null) {
|
||||||
selected.pos.x += vel.x;
|
selected.pos.add(vel);
|
||||||
selected.pos.y += vel.y;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DirectedGraph<Node, Double> newGraph() {
|
DirectedGraph<Node, Double> newGraph() {
|
||||||
throw new UnsupportedOperationException("Return a h07.graph.DirectedGraphImpl here");
|
return new EmptyGraphFactory<Node, Double>().createDirectedGraph();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
Reference in New Issue
Block a user