import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.io.*; import java.text.DecimalFormat; import java.util.Vector; import java.util.ArrayList; import java.util.Collections; import javax.swing.*; import java.util.HashMap; import java.awt.event.WindowEvent; public class Aufgabe12 extends JFrame implements ActionListener { // enum statt Konstanten, Auswahl der Heuristik private JMenuItem mnOpen = new JMenuItem("Öffnen"); private JMenuItem mnClose = new JMenuItem("Schließen"); JComboBox cbStart = new JComboBox(); JComboBox cbZiel = new JComboBox(); JButton bnStartAction = new JButton("Starten"); JButton bnStep = new JButton("Step"); JLabel labelSpeed; JLabel labelErg = new JLabel("Ergebnis:"); int fileTyp = 0; // 1 oder 2 Dateityp, hier aber immer 1 // globale Variable fuer den Algorithmus int count = 0; // zaehlt die Durchlaeufe Vector liste = new Vector(); Knoten nodeStart = null; Knoten nodeActual = null; Knoten nodeZiel = null; ArrayList openList = new ArrayList(); ArrayList closeList = new ArrayList(); JTextArea editor = new JTextArea(); public Aufgabe12() { this.setSize(900, 800); setLocation(100, 10); setTitle("12. Aufgabe: "); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setGUI(); setMenues(); } /** * Aufbau der GUI-Elemente. */ public void setGUI() { this.getContentPane().setLayout(new GridBagLayout()); JPanel panelWest = new JPanel(); this.getContentPane().add(panelWest, new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.NORTHWEST, // anchor GridBagConstraints.NONE, // HORIZONTAL, VERTICAL, BOTH new Insets(10, 20, 0, 0), 0, 0)); // Top, left, bottom, right panelWest.setLayout(new GridBagLayout()); JLabel label1 = new JLabel("Init-Tools"); label1.setForeground(Color.BLUE); panelWest.add(label1, new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.NORTHWEST, // anchor GridBagConstraints.NONE, // HORIZONTAL, VERTICAL, BOTH new Insets(10, 20, 0, 0), 0, 0)); // Top, left, bottom, right panelWest.add(new JLabel("Startknoten"), new GridBagConstraints( 0, 1, 1, 1, 0.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.NORTHWEST, // anchor GridBagConstraints.NONE, // HORIZONTAL, VERTICAL, BOTH new Insets(20, 20, 0, 0), 0, 0)); // Top, left, bottom, right panelWest.add(cbStart, new GridBagConstraints( 0, 2, 1, 1, 0.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.NORTHWEST, // anchor GridBagConstraints.NONE, // HORIZONTAL, VERTICAL, BOTH new Insets(10, 20, 0, 0), 0, 0)); // Top, left, bottom, right panelWest.add(new JLabel("Zielknoten"), new GridBagConstraints( 0, 3, 1, 1, 0.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.NORTHWEST, // anchor GridBagConstraints.NONE, // HORIZONTAL, VERTICAL, BOTH new Insets(10, 20, 0, 0), 0, 0)); // Top, left, bottom, right panelWest.add(cbZiel, new GridBagConstraints( 0, 4, 1, 1, 0.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.NORTHWEST, // anchor GridBagConstraints.NONE, // HORIZONTAL, VERTICAL, BOTH new Insets(10, 20, 0, 0), 0, 0)); // Top, left, bottom, right JLabel label2 = new JLabel("Aktionen"); label2.setForeground(Color.BLUE); panelWest.add(label2, new GridBagConstraints( 0, 5, 1, 1, 0.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.NORTHWEST, // anchor GridBagConstraints.NONE, // HORIZONTAL, VERTICAL, BOTH new Insets(40, 20, 0, 0), 0, 0)); // Top, left, bottom, right panelWest.add(bnStartAction, new GridBagConstraints( 0, 6, 1, 1, 0.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.NORTHWEST, // anchor GridBagConstraints.NONE, // HORIZONTAL, VERTICAL, BOTH new Insets(10, 20, 0, 0), 0, 0)); // Top, left, bottom, right panelWest.add(bnStep, new GridBagConstraints( 0, 8, 1, 1, 0.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.NORTHWEST, // anchor GridBagConstraints.NONE, // HORIZONTAL, VERTICAL, BOTH new Insets(10, 20, 0, 20), 0, 0)); // Top, left, bottom, right this.getContentPane().add(new JScrollPane(editor), new GridBagConstraints( 1, 1, 1, 1, 100.0, 100.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.NORTHWEST, // anchor GridBagConstraints.BOTH, // HORIZONTAL, VERTICAL, BOTH new Insets(20, 20, 0, 20), 0, 0)); // Top, left, bottom, right this.getContentPane().add(labelErg, new GridBagConstraints( 0, 3, 2, 1, 100.0, 0.0, // gridx, gridy, gridwidth, gridheight, weightx, weighty GridBagConstraints.NORTHWEST, // anchor GridBagConstraints.HORIZONTAL, // HORIZONTAL, VERTICAL, BOTH new Insets(20, 40, 20, 0), 0, 0)); // Top, left, bottom, right editor.setFont(new Font("Arial", Font.BOLD, 14)); setFonts(getContentPane(), 16); bnStartAction.addActionListener(this); bnStep.addActionListener(this); bnStartAction.setEnabled(false); bnStep.setEnabled(false); Dimension dim = bnStartAction.getPreferredSize(); bnStep.setPreferredSize(dim); } // setGUI private void setMenues() { JMenuBar menuBar1 = new JMenuBar(); JMenu mainFile = new JMenu("Datei"); mnOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK)); mainFile.add(mnOpen); mnOpen.addActionListener(this); mnClose.addActionListener(this); mainFile.addSeparator(); mainFile.add(mnClose); menuBar1.add(mainFile); //--------------- this.setJMenuBar(menuBar1); for (int i = 0; i < menuBar1.getComponentCount(); i++) { JMenu main = (JMenu) menuBar1.getComponent(i); main.setFont(new Font("Arial", Font.BOLD, 16)); for (int j = 0; j < main.getItemCount(); j++) { Component c = main.getItem(j); if (c instanceof JMenuItem) { JMenuItem mn = (JMenuItem) c; mn.setFont(new Font("Arial", Font.BOLD, 16)); } } } } // setMenus private void setFonts(Container cont, int size) { for (int i = 0; i < cont.getComponentCount(); i++) { Component c = cont.getComponent(i); if (c instanceof JPanel) { setFonts((JPanel) c, size); } else { c.setFont(new Font("Arial", Font.BOLD, size)); } } } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == bnStartAction) { setButton(); bnStartAction_click(); } if (e.getSource() == bnStep) { action(); } if (e.getSource() == mnOpen) { mnOpen_click(); } } // actionPerformed private void mnOpen_click() { JFileChooser jFileChooser1 = new JFileChooser(); String sFileName; jFileChooser1.setMultiSelectionEnabled(false); // jFileChooser1.setCurrentDirectory( new File("D:\\Daten\Aufgabe12") ); jFileChooser1.setCurrentDirectory(new File((String) System.getProperties().get("user.dir"))); if (JFileChooser.APPROVE_OPTION == jFileChooser1.showOpenDialog(this)) { // Rufen Sie openFile auf, um Text aus einer Datei in die TextArea zu laden load(jFileChooser1.getSelectedFile().getPath()); // } } double readDouble(LineNumberReader din) throws IOException { String line = din.readLine(); // x line.replace(',', '.'); return Double.parseDouble(line); } private void load(String filename) { FileInputStream fin; InputStreamReader iin; LineNumberReader din; String line; int anzNodes, anzNext; Knoten v; Kante kn; String bez; setTitle("11. Aufgabe: " + filename); double x = 0, y = 0; liste.clear(); try { fin = new FileInputStream(filename); iin = new InputStreamReader(fin); din = new LineNumberReader(iin); // oder BufferedReader br = new BufferedReader(ir); fileTyp = Integer.parseInt(din.readLine()); anzNodes = Integer.parseInt(din.readLine()); HashMap array = new HashMap(anzNodes, 0.70f); for (int i = 0; i < anzNodes; i++) { bez = din.readLine(); // bez x = readDouble(din); y = readDouble(din); v = new Knoten(bez, x, y); liste.add(v); array.put(v.bez, v); anzNext = Integer.parseInt(din.readLine()); for (int j = 0; j < anzNext; j++) { bez = din.readLine(); kn = new Kante(bez); v.next.add(kn); } } // for anzNodes // suche die Nachbarschaftknoten mittels nextBez, eintragen der Knoten Knoten vNext; for (Knoten v2 : liste) { for (Kante kn2 : v2.next) { vNext = (Knoten) array.get(kn2.bez); if (vNext == null) { System.out.println(" vNext null"); } kn2.next = vNext; } } cbStart.setModel(new DefaultComboBoxModel(liste)); cbStart.setSelectedIndex(0); cbZiel.setModel(new DefaultComboBoxModel(liste)); cbZiel.setSelectedIndex(liste.size() - 1); setButton(); } catch (FileNotFoundException e1) { System.err.println("Datei war nicht vorhanden!"); } catch (IOException ee) { System.err.println("IOException: " + ee); } } // load private void setButton() { bnStartAction.setEnabled(true); bnStep.setEnabled(true); } // setButton private void setSpielfeld() { bnStartAction.setEnabled(true); bnStep.setEnabled(true); } private void clear_init_Nodes() { for (Knoten v : liste) { v.init(); } } // hier fehlt Code // 1) Test beim Startknoten // 2) Test beim Zielknoten // 3) Startknoten != Zielknoten // 4) holen nodeStart und nodeZiel // 5) clear_init_Nodes, wenn man die Datei zweimal startet // 6) nodeStart und nodeZiel initialisieren // 7) listen loeschen // 8) debugfenster starten // 9) nodeStart: f,g,distanz setzen // 10) nodeActual setzen // 11) count // 12) Timer private void bnStartAction_click() { int indexStart = cbStart.getSelectedIndex(); int indexZiel = cbZiel.getSelectedIndex(); // suche den Startbutton if (indexStart < 0) { Basis.Error("Fehler", "Sie haben keinen Startpunkt ausgewählt"); return; } if (indexZiel < 0) { Basis.Error("Fehler", "Sie haben keinen Zielpunkt ausgewählt"); return; } if (indexStart == indexZiel) { Basis.Error("Fehler", "Der Startpunkt ist identisch mit dem Zielpunkt"); return; } nodeStart = liste.get(indexStart); nodeZiel = liste.get(indexZiel); clear_init_Nodes(); openList.clear(); closeList.clear(); editor.setText("Start: " + nodeStart); nodeStart.f = 0; nodeStart.g = 0; nodeStart.distance2Ziel = 0; nodeActual = nodeStart; editor.append("\n Startknoten: " + nodeActual+"\n"); count = 0; } // bnStartAction_click // hier fehlt Code // 1) Test der Variable count // 2) Nachfolger suchen // 3) closeList - // 4) Bestimmung von f,g, distanz // 5) check, Knoten schon berechnet? // 6) NodeActual in closeList // 7) minimum aus openList private void action() { DecimalFormat df = new DecimalFormat("#0.00"); editor.append("\n\n\n in action"); } // action // hier fehlt Code // 1) bestimmt die Route und gibt sie aus // 2) Reihenfolge beachten !!, muss korrekt sein private void drawRoute() { ArrayList ergListe = new ArrayList<>(); Knoten node = nodeZiel; // hier fehlt Code editor.append("\n Ergebnis:\n: " + ergListe); } // hier fehlt Code fuer mnEuklid private double calcDistanz(Knoten node, Knoten nodeZiel) { double a = node.x - nodeZiel.x; double b = node.y - nodeZiel.y; return Math.sqrt(a * a + b * b); } public static void main(String[] args) { Aufgabe12 frame = new Aufgabe12(); frame.setVisible(true); } }