package OurApplication; import graph.VertexMarking; import graph.EdgeMarking; import graph.DirectedGraph; import graph.ExampleGraphs; import graph.MarkedVertex; import graph.MarkedEdge; import graph.EdgeWeightMarking; import visualisation.ParameterArea; 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; 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 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"); 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 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 created from the input data. */ public DirectedGraph createGraph(String vertices, String edges) { DirectedGraph newGraph = new DirectedGraph<>(); try{ 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 if(allEdges.length != 1 || !Objects.equals(allEdges[0], "")){ MarkedVertex start = null; MarkedVertex end = null; for (String i: allEdges) { String[] current = i.split("-"); for (MarkedVertex 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])))); } } //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" + "Hinweis: ein Koten benötigt den Namen 'Start', ein anderer 'Ende'\n" + "(Festlegen der Start und Endknoten für die Algorithmen)", "Eingabefehler", JOptionPane.INFORMATION_MESSAGE, null ); } return newGraph; } }