Frage
Ich brauche einen Kreis zu zeichnen um eine Ecke in JUNG . Der Kreis wird durch den Scheitelpunkt als Mittelpunkt und einem gegebenen Radius r definiert ist.
Lösung
So etwas wie dies, denke ich. Dies gibt Ihnen Punkte für Kreis mit gegebenen radius
. Um Auflösung von Punkten ändern x+=0.01
zu einem größeren / kleineren Wert nach Bedarf anpassen. Zum Kreismitte zu einem beliebigen Punkt (p,q)
zu bewegen, nur um es zu (x,y)
, dass plot(x+p,y+q);
ist.
double radius = 3;
for (double x = -radius; x <= radius; x += 0.01) {
double y = Math.sqrt(radius * radius - x * x);
plot(x, y);//top half of the circle
plot(x, -y);//bottom half of the circle
}
Bearbeiten : Es scheint, dass JUNG nicht wirklich ein XY-Plot, sondern ein Netzwerk / Graph Framework. Also alles, was Sie brauchen ist Ihre Punkte in einem Kreis um das Layout einer der verfügbaren Layouts. CircleLayout
und KKLayout
scheinen den Trick zu tun, obwohl CircleLayout
seltsame Ergebnisse für gibt, wenn es viele Knoten. Hier vollständiger Beispielcode:
//Graph holder
Graph<Integer, String> graph = new SparseMultigraph<Integer, String>();
//Create graph with this many nodes and edges
int nodes = 30;
for (int i = 1; i <= nodes; i++) {
graph.addVertex(i);
//connect this vertext to vertex+1 to create an edge between them.
//Last vertex is connected to the first one, hence the i%nodes
graph.addEdge("Edge-" + i, i, (i % nodes) + 1);
}
//This will automatically layout nodes into a circle.
//You can also try CircleLayout class
Layout<Integer, String> layout = new KKLayout<Integer, String>(graph);
layout.setSize(new Dimension(300, 300));
//Thing that draws the graph onto JFrame
BasicVisualizationServer<Integer, String> vv = new BasicVisualizationServer<Integer, String>(layout);
vv.setPreferredSize(new Dimension(350, 350)); // Set graph dimensions
JFrame frame = new JFrame("Circle Graph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
frame.pack();
frame.setVisible(true);
Ich habe SparseMultiGraph
entschieden, weil das ist, was war in JUNG Tutorial . Es gibt auch andere Arten von Grafiken, aber ich bin nicht sicher, was der Unterschied ist.
Sie können auch eine StaticLayout
verwenden, die (x,y)
Ecken nehmen, dann meinen Original-Code verwenden, um die Punkte zu zeichnen, aber das wäre nicht so elegant für JUNG Rahmen. Hängt davon ab, was Ihre Anforderungen sind jedoch.