package OurApplication; import graph.*; import visualisation.ParameterArea; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Objects; /** * This class provides an example for using visualization.ParameterArea. * @see ParameterArea * @author MSch�fer * DHBW Stuttgart/Campus Horb AI2008
*
*/ public class OurParameterArea extends ParameterArea{ private static final long serialVersionUID = 1L; private DirectedGraph customGraph; private JRadioButton button1; private JRadioButton button2; private JRadioButton button3; private JRadioButton button4; private JRadioButton button5; private JButton buttonAddGraph; private JTextField textField1; private JTextField textField2; 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; // Standardmäßig Beispiel 1 this.customGraph = new DirectedGraph<>(); setBorder(BorderFactory.createTitledBorder("ParameterArea")); // Layout-Manager setzen, um die Buttons vertikal anzuordnen setLayout(new GridLayout(4, 1, 5, 5)); // 4 Zeilen, 1 Spalte, 5 Pixel Abstand // Buttons erstellen 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"); buttonAddGraph = new JButton("Graph einfügen"); // Eingabefelder textField1 = new JTextField("Knoten"); textField2 = new JTextField("Kanten"); // ButtonGroup erstellen und Buttons hinzufügen, um die gegenseitige Ausschließung zu gewährleisten ButtonGroup group = new ButtonGroup(); group.add(button1); group.add(button2); group.add(button3); group.add(button4); group.add(button5); // ActionListener hinzufügen 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); buttonAddGraph.addActionListener(e -> { String input1 = textField1.getText(); String input2 = textField2.getText(); this.customGraph = createGraph(input1, input2); }); // Buttons zum Panel hinzufügen add(button1); add(button2); add(button3); add(button4); add(button5); add(buttonAddGraph); add(textField1); add(textField2); } 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(); } } // Vertex Eingabe: A-100-100;B-200-200;C-100-200;D-200-100 // Edge Eingabe: A-B-15;C-D-10;D-A-3 public DirectedGraph createGraph(String vertices, String edges) { DirectedGraph newGraph = new DirectedGraph<>(); String[] allVertices = vertices.split(";"); String[] allEdges = edges.split(";"); 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)); } 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; } else if (Objects.equals(j.getName(), current[1])) { end = j; } } newGraph.addEdge(new MarkedEdge<>(i, start, end, new EdgeWeightMarking(Integer.parseInt(current[2])))); } return newGraph; } }