Programmieren_Projekt/testApplication/TestApplication.java

138 lines
5.5 KiB
Java

package testApplication;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import graph.*;
import logging.LogElementList;
import visualization.HybridWindow;
import java.awt.*;
import java.util.Random;
import static java.awt.Color.black;
public class TestApplication {
public static void main(String[] args) {
LogElementList<TestLogElement> logList = new LogElementList<>();
TestParameterArea parameterArea = new TestParameterArea();
TestDrawArea drawArea = new TestDrawArea(logList, "visualization");
TestTextArea textArea = new TestTextArea(logList);
TestAlgorithm algorithm = new TestAlgorithm(parameterArea);
TestLegendArea legendArea = new TestLegendArea();
HybridWindow<TestDrawArea, TestTextArea, TestParameterArea, TestAlgorithm, TestLogElement, TestLegendArea> applet = new HybridWindow<>(drawArea, textArea, parameterArea, algorithm, logList, legendArea);
JFrame frame = new JFrame("Visualise");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.pack();
applet.init();
applet.start();
frame.setSize(800, 600);
frame.setVisible(true);
String input = JOptionPane.showInputDialog("Enter 'manual' to manually or 'auto' or 'demo' for graph creation:");
DirectedGraph<VertexMarking, EdgeMarking> appGraph = new DirectedGraph<>();
if ("manual".equalsIgnoreCase(input)) {
// Anzahl der zu erstellenden Vertexe
int vertexCount = Integer.parseInt(JOptionPane.showInputDialog("How many vertices would you like to add?"));
for (int i = 0; i < vertexCount; i++) {
String vertexInput = JOptionPane.showInputDialog(null,
"Enter vertex name, x position and y position(comma separated):",
"Vertex Input", JOptionPane.QUESTION_MESSAGE);
if (vertexInput != null && !vertexInput.isEmpty()) {
String[] parts = vertexInput.split(",");
if (parts.length >= 3) { // Es müssen 3 durch Komma getrennte Elemente angegeben werden
String name = parts[0].trim();
int x = Integer.parseInt(parts[1].trim());
int y = Integer.parseInt(parts[2].trim());
Color color = Color.BLACK;
MarkedVertex<VertexMarking> newVertex = new MarkedVertex<>(name, null, x, y, color);
appGraph.addVertex(newVertex);
} else {
JOptionPane.showMessageDialog(null, "Invalid input. Please enter all required fields.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
// Nachdem alle Vertexe hinzugefügt wurden, füge Edges hinzu
int edgeCount = Integer.parseInt(JOptionPane.showInputDialog("How many edges would you like to add?"));
for (int i = 0; i < edgeCount; i++) {
String edgeInput = JOptionPane.showInputDialog(null,
"Enter the names of the two vertices you want to connect (comma separated):",
"Edge Input", JOptionPane.QUESTION_MESSAGE);
if (edgeInput != null && !edgeInput.isEmpty()) {
String[] vertexNames = edgeInput.split(",");
if (vertexNames.length == 2) { // Es müssen jeweils 2 durch Komma getrennte Knoten angegeben werden
String vertexName1 = vertexNames[0].trim();
String vertexName2 = vertexNames[1].trim();
MarkedVertex<VertexMarking> vertex1 = findVertexByName(appGraph, vertexName1);
MarkedVertex<VertexMarking> vertex2 = findVertexByName(appGraph, vertexName2);
if (vertex1 != null && vertex2 != null) {
appGraph.addEdge(new MarkedEdge<>("edge" + (i + 1), vertex1, vertex2, null));
} else {
JOptionPane.showMessageDialog(null, "One or both vertices not found.", "Error", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(null, "Invalid input. Please enter two vertex names.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
System.out.println("Graph construction complete with " + appGraph.getAllVertexes().size() + " vertices and " + appGraph.getAllEdges().size() + " edges.");
}
else {
// Automatische Erstellung des Graphen
Random random = new Random();
String numberOfVertexes = JOptionPane.showInputDialog("How many vertexes do you want? :");
int number = Integer.valueOf(numberOfVertexes);
for (int i = 0; i < number; i++) {
char letter = (char) ('A' + i);
String vertexName = "vertex" + letter;
appGraph.addVertex(new MarkedVertex<>(vertexName, null, random.nextInt(20, 300), random.nextInt(20, 300), Color.BLACK));
}
for (int i = 0; i < number; i++) {
int sourceIndex = random.nextInt(appGraph.getAllVertexes().size());
int destIndex = random.nextInt(appGraph.getAllVertexes().size());
MarkedVertex<VertexMarking> sourceVertex = appGraph.getAllVertexes().get(sourceIndex);
MarkedVertex<VertexMarking> destVertex = appGraph.getAllVertexes().get(destIndex);
appGraph.addEdge(new MarkedEdge<>("edge" + i, sourceVertex, destVertex, null));
}
}
drawArea.actualizeGraph(appGraph.getVisualizedGraph());
algorithm.actualizeGraph(appGraph);
}
private static Color parseColor(String colorStr) {
try {
return (Color) Color.class.getField(colorStr.toUpperCase()).get(null);
} catch (Exception e) {
return Color.BLACK; // Standardfarbe, wenn nichts zugeordnet werden konnte
}
}
private static MarkedVertex<VertexMarking> findVertexByName(DirectedGraph<VertexMarking, EdgeMarking> graph, String name) {
for (MarkedVertex<VertexMarking> vertex : graph.getAllVertexes()) {
if (vertex.getName().equals(name)) {
return vertex;
}
}
return null;
}
}