ProjektGraph/OurApplication/OurParameterArea.java

166 lines
4.9 KiB
Java

package OurApplication;
import graph.*;
import visualisation.ParameterArea;
import javax.swing.*;
import java.awt.*;
import java.util.Objects;
/**
* This class extends visualisation.ParameterArea and provides an example for using it.
* It includes functionality for managing a graph selection and creation based on user input.
*
* @see ParameterArea
*/
public class OurParameterArea extends ParameterArea {
private static final long serialVersionUID = 1L;
// Custom graph instance
private DirectedGraph<VertexMarking, EdgeMarking> customGraph;
// Radio buttons for selecting examples or custom graph
private JRadioButton button1;
private JRadioButton button2;
private JRadioButton button3;
private JRadioButton button4;
private JRadioButton button5;
// Button to add a custom graph
private JButton buttonAddGraph;
// Text fields for inputting vertex and edge data
private JTextField textField1;
private JTextField textField2;
// Selected example identifier
private int selectedExample;
/** TextField containing maximum sum up value. */
protected JTextField maxValue;
/**
* Standard constructor.
* Creates SumUpParameterArea with an empty JTextField.
*/
public OurParameterArea() {
super();
selectedExample = 1; // Default to example 1
this.customGraph = new DirectedGraph<>();
setBorder(BorderFactory.createTitledBorder("ParameterArea"));
// Set layout manager to arrange buttons vertically
setLayout(new GridLayout(4, 1, 5, 5)); // 4 rows, 1 column, 5 pixel gap
// Create radio buttons
button1 = new JRadioButton("Beispiel 1", true);
button2 = new JRadioButton("Beispiel 2");
button3 = new JRadioButton("Beispiel 3");
button4 = new JRadioButton("Beispiel 4");
button5 = new JRadioButton("Eigener Graph");
// Button to add a graph
buttonAddGraph = new JButton("Graph einfügen");
// Input text fields
textField1 = new JTextField("A-100-100;Ende-200-200;Start-100-200;D-200-100");
textField2 = new JTextField("A-Ende-15;Start-D-10;D-A-3;A-Ende-15;Start-D-10;D-A-3");
textField1.setPreferredSize(new Dimension(button5.getWidth(), button5.getHeight()));
textField2.setPreferredSize(new Dimension(button5.getWidth(), button5.getHeight()));
// Button group to ensure mutual exclusion of radio buttons
ButtonGroup group = new ButtonGroup();
group.add(button1);
group.add(button2);
group.add(button3);
group.add(button4);
group.add(button5);
// Add action listeners to radio buttons
button1.addActionListener(e -> selectedExample = 1);
button2.addActionListener(e -> selectedExample = 2);
button3.addActionListener(e -> selectedExample = 3);
button4.addActionListener(e -> selectedExample = 4);
button5.addActionListener(e -> selectedExample = 5);
// Add action listener to add graph button
buttonAddGraph.addActionListener(e -> {
String input1 = textField1.getText();
String input2 = textField2.getText();
this.customGraph = createGraph(input1, input2);
});
// Add components to panel
add(button1);
add(button2);
add(button3);
add(button4);
add(button5);
add(buttonAddGraph);
add(textField1);
add(textField2);
}
/**
* Retrieves the selected graph based on the user's choice.
*
* @return The selected graph as a DirectedGraph instance.
*/
public DirectedGraph<VertexMarking, EdgeMarking> getSelectedGraph() {
ExampleGraphs temp = new ExampleGraphs();
switch (selectedExample) {
case 2:
return temp.example2();
case 3:
return temp.example3();
case 4:
return temp.example4();
case 5:
return this.customGraph;
case 1:
default:
return temp.example1();
}
}
/**
* Creates a directed graph based on input vertex and edge data.
*
* @param vertices String representation of vertices in the format "Name-X-Y;..."
* @param edges String representation of edges in the format "Source-Target-Weight;..."
* @return DirectedGraph<VertexMarking, EdgeMarking> created from the input data.
*/
public DirectedGraph<VertexMarking, EdgeMarking> createGraph(String vertices, String edges) {
DirectedGraph<VertexMarking, EdgeMarking> newGraph = new DirectedGraph<>();
String[] allVertices = vertices.split(";");
String[] allEdges = edges.split(";");
// Create vertices
for (String i: allVertices) {
String[] current = i.split("-");
newGraph.addVertex(new MarkedVertex<>(Integer.parseInt(current[1]), Integer.parseInt(current[2]), current[0], null, Color.BLACK));
}
// Create edges
MarkedVertex<VertexMarking> start = null;
MarkedVertex<VertexMarking> end = null;
for (String i: allEdges) {
String[] current = i.split("-");
for (MarkedVertex<VertexMarking> j: newGraph.getAllVertexes()) {
if (Objects.equals(j.getName(), current[0])) {
start = j;
}
if (Objects.equals(j.getName(), current[1])) {
end = j;
}
}
newGraph.addEdge(new MarkedEdge<>(i, start, end, new EdgeWeightMarking(Integer.parseInt(current[2]))));
}
return newGraph;
}
}