Dateien nach "testApplication" hochladen
This commit is contained in:
parent
9aaa5b43cf
commit
88e47b1b4b
61
testApplication/TestAlgorithm.java
Normal file
61
testApplication/TestAlgorithm.java
Normal file
@ -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<EFBFBD>fer
|
||||||
|
* DHBW Stuttgart/Campus Horb AI2008<br>
|
||||||
|
* <br>
|
||||||
|
*/
|
||||||
|
public class TestAlgorithm extends Algorithm{
|
||||||
|
|
||||||
|
private graph.DirectedGraph<VertexMarking, EdgeMarking> 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<TestLogElement> run(){
|
||||||
|
Random random = new Random();
|
||||||
|
MarkedVertex<VertexMarking> 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<VertexMarking, EdgeMarking> graph) {
|
||||||
|
this.graph = graph;
|
||||||
|
}
|
||||||
|
}
|
137
testApplication/TestApplication.java
Normal file
137
testApplication/TestApplication.java
Normal file
@ -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<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;
|
||||||
|
}
|
||||||
|
}
|
69
testApplication/TestDrawArea.java
Normal file
69
testApplication/TestDrawArea.java
Normal file
@ -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<EFBFBD>fer
|
||||||
|
* DHBW Stuttgart/Campus Horb AI2008<br>
|
||||||
|
* <br>
|
||||||
|
*/
|
||||||
|
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<TestLogElement> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user