ProjektGraph/OurApplication/OurParameterArea.java

166 lines
4.9 KiB
Java
Raw Normal View History

2024-07-02 19:13:30 +00:00
package OurApplication;
2024-07-07 21:39:17 +00:00
import graph.*;
2024-07-02 19:13:30 +00:00
import visualisation.ParameterArea;
import javax.swing.*;
import java.awt.*;
2024-07-08 20:30:55 +00:00
import java.util.Objects;
/**
2024-07-08 22:17:31 +00:00
* 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.
*
2024-07-02 19:13:30 +00:00
* @see ParameterArea
*/
2024-07-08 22:17:31 +00:00
public class OurParameterArea extends ParameterArea {
private static final long serialVersionUID = 1L;
2024-07-08 22:17:31 +00:00
// Custom graph instance
2024-07-07 21:39:17 +00:00
private DirectedGraph<VertexMarking, EdgeMarking> customGraph;
2024-07-08 22:17:31 +00:00
// Radio buttons for selecting examples or custom graph
private JRadioButton button1;
private JRadioButton button2;
private JRadioButton button3;
private JRadioButton button4;
2024-07-07 18:34:38 +00:00
private JRadioButton button5;
2024-07-08 22:17:31 +00:00
// Button to add a custom graph
2024-07-07 21:39:17 +00:00
private JButton buttonAddGraph;
2024-07-08 22:17:31 +00:00
// Text fields for inputting vertex and edge data
2024-07-07 21:39:17 +00:00
private JTextField textField1;
private JTextField textField2;
2024-07-08 22:17:31 +00:00
// Selected example identifier
private int selectedExample;
2024-07-08 22:17:31 +00:00
/** TextField containing maximum sum up value. */
protected JTextField maxValue;
/**
* Standard constructor.
* Creates SumUpParameterArea with an empty JTextField.
*/
2024-07-02 19:13:30 +00:00
public OurParameterArea() {
super();
2024-07-08 22:17:31 +00:00
selectedExample = 1; // Default to example 1
2024-07-07 21:39:17 +00:00
this.customGraph = new DirectedGraph<>();
setBorder(BorderFactory.createTitledBorder("ParameterArea"));
2024-07-08 22:17:31 +00:00
// Set layout manager to arrange buttons vertically
setLayout(new GridLayout(4, 1, 5, 5)); // 4 rows, 1 column, 5 pixel gap
2024-07-08 22:17:31 +00:00
// Create radio buttons
button1 = new JRadioButton("Beispiel 1", true);
button2 = new JRadioButton("Beispiel 2");
button3 = new JRadioButton("Beispiel 3");
button4 = new JRadioButton("Beispiel 4");
2024-07-07 18:34:38 +00:00
button5 = new JRadioButton("Eigener Graph");
2024-07-08 22:17:31 +00:00
// Button to add a graph
2024-07-07 21:39:17 +00:00
buttonAddGraph = new JButton("Graph einfügen");
2024-07-08 22:17:31 +00:00
// Input text fields
2024-07-08 21:17:38 +00:00
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()));
2024-07-07 21:39:17 +00:00
2024-07-08 22:17:31 +00:00
// 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);
2024-07-07 18:34:38 +00:00
group.add(button5);
2024-07-08 22:17:31 +00:00
// Add action listeners to radio buttons
2024-07-07 21:39:17 +00:00
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);
2024-07-08 22:17:31 +00:00
// Add action listener to add graph button
2024-07-07 21:39:17 +00:00
buttonAddGraph.addActionListener(e -> {
String input1 = textField1.getText();
String input2 = textField2.getText();
this.customGraph = createGraph(input1, input2);
2024-07-07 18:34:38 +00:00
});
2024-07-08 22:17:31 +00:00
// Add components to panel
add(button1);
add(button2);
add(button3);
add(button4);
add(button5);
add(buttonAddGraph);
add(textField1);
add(textField2);
}
2024-07-08 22:17:31 +00:00
/**
* 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();
2024-07-07 18:34:38 +00:00
case 5:
2024-07-07 21:39:17 +00:00
return this.customGraph;
case 1:
default:
return temp.example1();
}
2024-07-02 22:46:21 +00:00
}
2024-07-08 22:17:31 +00:00
/**
* 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<>();
2024-07-07 21:39:17 +00:00
2024-07-08 22:17:31 +00:00
String[] allVertices = vertices.split(";");
String[] allEdges = edges.split(";");
2024-07-07 21:39:17 +00:00
2024-07-08 22:17:31 +00:00
// 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));
}
2024-07-07 21:39:17 +00:00
2024-07-08 22:17:31 +00:00
// 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;
}
2024-07-08 21:17:38 +00:00
if (Objects.equals(j.getName(), current[1])) {
2024-07-08 22:17:31 +00:00
end = j;
}
}
newGraph.addEdge(new MarkedEdge<>(i, start, end, new EdgeWeightMarking(Integer.parseInt(current[2]))));
}
2024-07-07 21:39:17 +00:00
2024-07-08 22:17:31 +00:00
return newGraph;
}
}