Skip to content
Snippets Groups Projects
Commit 41a14b5d authored by Jona Dirks's avatar Jona Dirks
Browse files

"dynamic" text size and not drawing vertices out of view

parent c6c0a3f5
No related branches found
No related tags found
1 merge request!29Feature/more efficent and more readable visualizer
Pipeline #204704 failed
......@@ -89,7 +89,9 @@ public class DirectedGraphJPanel extends CustomGraphJPanel implements Runnable {
int y = ((int) (((rawVertex.getPosition()[1] - windowStartY) / (windowEndY - windowStartY))
* (this.getHeight() - rectSize.getHeight() - 2 * offset))) + offset;
vertexCoords.put(rawVertex.getId(), new Point(x, y));
this.add(new VertexLabel(rawVertex, rectSize, thickness, x, y));
if (!(x < 0 || y < 0 || x > this.getWidth() || y > this.getHeight())) {
this.add(new VertexLabel(rawVertex, rectSize, thickness, x, y));
}
}
}
......
......@@ -7,12 +7,30 @@ import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
/**
* This class allows it to paint vertices on a JPanel.
*/
public class VertexLabel extends JLabel {
private int getFontSize(int id) {
String nameId = Integer.toString(id);
switch (nameId.length()) {
case 1:
case 2:
return 12;
case 3:
return 10;
case 4:
return 8;
case 5:
return 7;
}
return 6;
}
/**
* Create a VertexLabel.
*
......@@ -26,6 +44,7 @@ public class VertexLabel extends JLabel {
final int x, final int y) {
this.setOpaque(true);
this.setForeground(Color.WHITE);
this.setFont(new Font("Serif", Font.PLAIN, getFontSize(vertex.getId())));
this.setText("<html>[" + vertex.getId() + "] <br/>" + vertex.getData().toString() + "</html>");
this.setBackground(vertex.getColor());
this.setBounds(x, y, (int) size.getWidth(), (int) size.getHeight());
......
......@@ -33,6 +33,10 @@ class GraphVisualizerTest {
2. diesen Graph an den Visualisierer übermitteln
*/
Graph<Vertex, UndirectedEdge> graph = importerExporter.loadAndCreate(pfad);
graph.addVertex(new Vertex(123));
graph.addVertex(new Vertex(1234));
graph.addVertex(new Vertex(12345));
graph.addVertex(new Vertex(123456));
graph.updateVertex(new VisualVertex(2, "Hey", Color.RED));
graph.updateEdge(new VisualUndirectedEdge(1, 3, "Ho", Color.BLUE));
new GraphVisualizer(graph);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment