ProjektGraph/OurApplication/OurParameterArea.java

201 lines
6.0 KiB
Java
Raw Permalink Normal View History

2024-07-02 19:13:30 +00:00
package OurApplication;
2024-07-09 13:51:40 +00:00
import graph.VertexMarking;
import graph.EdgeMarking;
import graph.DirectedGraph;
import graph.ExampleGraphs;
import graph.MarkedVertex;
import graph.MarkedEdge;
import graph.EdgeWeightMarking;
2024-07-02 19:13:30 +00:00
import visualisation.ParameterArea;
2024-07-09 13:51:40 +00:00
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.Color;
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");
2024-07-08 23:55:13 +00:00
textField2 = new JTextField("A-Ende-15;Start-D-10;D-A-3");
2024-07-08 21:17:38 +00:00
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 23:55:13 +00:00
try{
String[] allVertices = vertices.split(";");
String[] allEdges = edges.split(";");
2024-07-07 21:39:17 +00:00
2024-07-08 23:55:13 +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 23:55:13 +00:00
// Create edges
2024-07-09 10:39:36 +00:00
if(allEdges.length != 1 || !Objects.equals(allEdges[0], "")){
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;
}
2024-07-08 23:55:13 +00:00
}
2024-07-09 10:39:36 +00:00
newGraph.addEdge(new MarkedEdge<>(i, start, end, new EdgeWeightMarking(Integer.parseInt(current[2]))));
2024-07-08 22:17:31 +00:00
}
}
2024-07-08 23:55:13 +00:00
//Fehlerbehandlung
}catch(Throwable e){
JFrame info = new JFrame();
JPanel contentpane = new JPanel();
info.setContentPane(contentpane);
JOptionPane.showMessageDialog(
contentpane,
"Fehler bei der Eingabe des Graphen. Beispieleingabe für Knoten und Kanten:\n" +
"Knoten: Name;X-Coordinate;Y-Coordinate\nKanten: StartKnoten;Endknoten;Gewichtung\n" +
2024-07-09 10:39:36 +00:00
"Hinweis: ein Koten benötigt den Namen 'Start', ein anderer 'Ende'\n" +
2024-07-08 23:55:13 +00:00
"(Festlegen der Start und Endknoten für die Algorithmen)",
"Eingabefehler",
JOptionPane.INFORMATION_MESSAGE,
null
);
2024-07-08 22:17:31 +00:00
}
2024-07-07 21:39:17 +00:00
2024-07-08 23:55:13 +00:00
2024-07-08 22:17:31 +00:00
return newGraph;
}
}