From 88e47b1b4bd6dba8ecd7304943ee3d48214e98fa Mon Sep 17 00:00:00 2001 From: Jonathan-Kalmbach Date: Wed, 10 Jul 2024 05:54:05 +0000 Subject: [PATCH] Dateien nach "testApplication" hochladen --- testApplication/TestAlgorithm.java | 61 ++++++++++++ testApplication/TestApplication.java | 137 +++++++++++++++++++++++++++ testApplication/TestDrawArea.java | 69 ++++++++++++++ 3 files changed, 267 insertions(+) create mode 100644 testApplication/TestAlgorithm.java create mode 100644 testApplication/TestApplication.java create mode 100644 testApplication/TestDrawArea.java diff --git a/testApplication/TestAlgorithm.java b/testApplication/TestAlgorithm.java new file mode 100644 index 0000000..df8d1bd --- /dev/null +++ b/testApplication/TestAlgorithm.java @@ -0,0 +1,61 @@ +package testApplication; + +import graph.DirectedGraph; +import graph.EdgeMarking; +import graph.MarkedVertex; +import graph.VertexMarking; +import logging.*; + +import javax.swing.*; +import java.util.Random; + +/** + * This class provides an example for using logging.Algorithm. + * It sums up integer number starting with 0 up to the maximum value specified in the parameter area. + * @see logging.Algorithm + * @author MSch�fer + * DHBW Stuttgart/Campus Horb AI2008
+ *
+ */ +public class TestAlgorithm extends Algorithm{ + + private graph.DirectedGraph graph; + + public TestAlgorithm() { + super(); + } + /** + * Creates a sum up algorithm. + * @param parameterArea the sum up parameter area the algorithm gets its parameters from + */ + public TestAlgorithm(TestParameterArea parameterArea){ + super(parameterArea,"Visualized algorithm"); + } + + /** + * Overwritten from super class. + * Runs the algorithm.. + * Returns the LogElementList produced by the algorithm processing. + * Adds integer number starting with 0 up to the maximum value. + * @return a LogElementList containing the algorithm processing single steps + */ + public LogElementList run(){ + Random random = new Random(); + MarkedVertex start = this.graph.getAllVertexes().get(0); + + String input = JOptionPane.showInputDialog("Enter 'BFS' to run BreadthFirstSearch algorithm or 'TopSort' for the TopSort algorithm:"); + + if ("BFS".equalsIgnoreCase(input)) { + this.graph.breadthFirstSearch(start); + } + if ("TopSort".equalsIgnoreCase(input)) { + this.graph.topSort(start); + } + + return this.graph.getNewLogList(); + } + + public void actualizeGraph(graph.DirectedGraph graph) { + this.graph = graph; + } +} diff --git a/testApplication/TestApplication.java b/testApplication/TestApplication.java new file mode 100644 index 0000000..556c43e --- /dev/null +++ b/testApplication/TestApplication.java @@ -0,0 +1,137 @@ +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 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 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 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 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 vertex1 = findVertexByName(appGraph, vertexName1); + MarkedVertex 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 sourceVertex = appGraph.getAllVertexes().get(sourceIndex); + MarkedVertex 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 findVertexByName(DirectedGraph graph, String name) { + for (MarkedVertex vertex : graph.getAllVertexes()) { + if (vertex.getName().equals(name)) { + return vertex; + } + } + return null; + } +} diff --git a/testApplication/TestDrawArea.java b/testApplication/TestDrawArea.java new file mode 100644 index 0000000..f50dcb7 --- /dev/null +++ b/testApplication/TestDrawArea.java @@ -0,0 +1,69 @@ +package testApplication; + +import java.awt.Color; +import java.awt.Graphics; +import java.util.Vector; + +import graph.*; +import logging.GraphLogElement; +import logging.NewLogElement; +import visualization.DrawArea; +import visualizationElements.BarChart; +import visualizationElements.BarChartElement; +import visualizationElements.ChessBoard; +import visualizationElements.DotChart; +import visualizationElements.Edge; +import visualizationElements.EdgeStyle; +import visualizationElements.Graph; +import visualizationElements.Hashtable; +import visualizationElements.List; +import visualizationElements.Maze; +import visualizationElements.Queue; +import visualizationElements.Stack; +import visualizationElements.Table; +import visualizationElements.Vertex; + +import logging.LogElementList; + +/** + * This class provides an example for using visualization.DrawArea. + * @see logging.Algorithm + @author MSch�fer + * DHBW Stuttgart/Campus Horb AI2008
+ *
+ */ +public class TestDrawArea extends DrawArea{ + + private static final long serialVersionUID = 1L; + private Maze maze; + private visualizationElements.Graph graph; + /** + * Standard constructor. + */ + public TestDrawArea() { + super(); + } + + /** + * Creates a test draw area and sets the specified log list. + * @param logList the draw area's log list test log elements. + * @param drawAreaName The display name over the draw area. + */ + public TestDrawArea(LogElementList logList, String drawAreaName){ + super(logList, drawAreaName); + } + + /** + * Draws a visualization element. + */ + public void draw(Graphics g){ + TestLogElement logElement = (TestLogElement) logList.get(); + logElement.getGraph().draw(g); + } + + public void actualizeGraph(visualizationElements.Graph graph){ + this.graph = graph; + } + + +} \ No newline at end of file