diff --git a/Edge.java b/Edge.java index 1072964..24c08c5 100644 --- a/Edge.java +++ b/Edge.java @@ -4,23 +4,38 @@ public class Edge implements Comparable private Node ende; private int wert; private boolean gerichtet; - + public Edge(Node a, Node e, int w, boolean r){ this.anfang = a; this.ende = e; this.wert = w ; this.gerichtet = r; } - + public Node getAnfang(){return this.anfang;} + public Node getEnde(){return this.ende;} + public int getWert(){return this.wert;} + public boolean istGerichtet(){return this.gerichtet;} - + public int compareTo(Object o) { Edge other = (Edge)o; if(this.wert < other.wert)return -1; if(this.wert > other.wert)return 1; return 0; -} + } + + public String toString(){ + /*String ergebnis = "E "; + ergebnis += anfang.getNumber() + " "; + ergebnis += ende.getNumber() + " "; + if (this.gerichtet) ergebnis += "1 "; + else ergebnis += "0 "; + ergebnis += this.wert; + return ergebnis; */ + + return "E " + anfang.getNumber() + " " + ende.getNumber() + " " + wert + " " + (gerichtet ? 1 : 0); + } } \ No newline at end of file diff --git a/Graph.java b/Graph.java index c2af13d..2fe7587 100644 --- a/Graph.java +++ b/Graph.java @@ -1,6 +1,8 @@ import java.util.ArrayList; import java.util.LinkedList; import java.util.Collections; +import java.io.*; + public class Graph { private ArrayList nodes; @@ -36,6 +38,7 @@ public class Graph public ArrayList getEdges(){ return this.edges; } + public void addEdge(int a, int e, int w, boolean g){ Node an = null; Node en = null; @@ -70,6 +73,7 @@ public class Graph } return null; } + public ArrayList breitensuche(int start){ ArrayList ergebnis = new ArrayList(); LinkedList q = new LinkedList(); @@ -90,7 +94,26 @@ public class Graph return ergebnis; } + + public void tiefensucheGraphisch(){ + ArrayList ergebnis = new ArrayList(); + LinkedList q = new LinkedList(); + //q.add(this.getNode(start)); + while(!q.isEmpty()){ + Node aktuell = q.removeLast(); + if (ergebnis.contains(aktuell)){ + continue; + } + + ArrayList erreichbar = aktuell.getTargets(); + Collections.sort(erreichbar, Collections.reverseOrder()); + q.addAll(erreichbar); + + ergebnis.add(aktuell); + } + } + public ArrayList tiefensuche(int start){ ArrayList ergebnis = new ArrayList(); LinkedList q = new LinkedList(); @@ -212,4 +235,39 @@ public class Graph } return sb; } + + public void save (String filename){ + try{ + FileWriter f = new FileWriter(filename); + for(Node n : this.nodes){ + f.write(n.toString() + "\n"); + } + for(Edge e : this.edges){ + f.write(e.toString() + "\n"); + } + f.close(); + }catch(IOException e){} + + } + + public int load (String filename){ + int max = 0; + try{ + BufferedReader f = new BufferedReader(new FileReader(filename)); + String line = f.readLine(); + while(line != null){ + String[] data = line.split(" "); + if(data[0].equals("N")) { + this.addNode(Integer.parseInt(data[1]),Integer.parseInt(data[2]),Integer.parseInt(data[3])); + if (Integer.parseInt(data[1]) > max) max = Integer.parseInt(data[1]); + } + if(data[0].equals("E")) { + this.addEdge(Integer.parseInt(data[1]),Integer.parseInt(data[2]),Integer.parseInt(data[3]),data[4].equals("1")); + } + line = f.readLine(); + } + f.close(); + } catch(IOException e){} + return max; + } } diff --git a/MyPanel.java b/MyPanel.java index bdb40fd..8b08bde 100644 --- a/MyPanel.java +++ b/MyPanel.java @@ -13,6 +13,7 @@ public class MyPanel extends JPanel private Node nmove; private int x; private int y; + public MyPanel(){ g = new Graph(); nr = 1; @@ -22,7 +23,7 @@ public class MyPanel extends JPanel start = g.getNode(x, y); if ( start == null){ g.addNode(nr, x, y); - nr++; + nr++; } } @@ -40,6 +41,7 @@ public class MyPanel extends JPanel start = null; nmove=null; } + public void move (int x, int y){ this. x = x; this. y = y; @@ -52,6 +54,22 @@ public class MyPanel extends JPanel public void right(int x, int y){ this.nmove = g.getNode(x,y); } + + public void save(){ + this.g.save("test.txt"); + } + + public void load(){ + this. nr = this.g.load("test.txt") +1; + } + + public void clear(){ + g = new Graph(); + } + + public void tiefensuche(){ + g.tiefensuche(1); + } @Override protected void paintComponent(Graphics g){ super.paintComponent(g); diff --git a/Node.java b/Node.java index b84aee5..772d5d6 100644 --- a/Node.java +++ b/Node.java @@ -63,4 +63,8 @@ public class Node implements Comparable if(this.number > other.number)return 1; return 0; } + + public String toString(){ + return "N " + this.number + " " + this.x + " " + this.y; + } } diff --git a/Window.java b/Window.java index 76eeeab..f92bf1d 100644 --- a/Window.java +++ b/Window.java @@ -8,6 +8,9 @@ public class Window implements ActionListener private MyPanel panel; private JButton button; private JButton button2; + private JButton button3; + private JButton button4; + public Window(){ fenster = new JFrame("Mein erstes Fenster"); fenster.setSize(400,400); @@ -22,14 +25,23 @@ public class Window implements ActionListener fenster.add(panel, BorderLayout.CENTER); panel.setBackground(new Color(100,100,100)); - button = new JButton ("Linie"); + button = new JButton ("Speichern"); top.add(button); button.addActionListener(this); - button2 = new JButton("Kreis"); + button2 = new JButton("Laden"); top.add(button2); button2.addActionListener(this); - + + button3 = new JButton("Clear"); + top.add(button3); + button3.addActionListener(this); + + button4 = new JButton("Tiefensuche"); + top.add(button4); + button4.addActionListener(this); + + fenster.setVisible(true); panel.addMouseMotionListener(new MouseMotionListener(){ public void mouseDragged(MouseEvent e){ @@ -68,9 +80,19 @@ public class Window implements ActionListener } public void actionPerformed(ActionEvent e){ - if (e.getSource() == this.button) - if (e.getSource() == this.button2) - fenster.repaint(); + if (e.getSource() == this.button) { + panel.save(); + } + if (e.getSource() == this.button2) { + panel.load(); + } + if(e.getSource() == this.button3){ + panel.clear(); + } + if(e.getSource() == this.button4){ + panel.tiefensuche(); + } + fenster.repaint(); } } diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..3fdabc1 --- /dev/null +++ b/test.txt @@ -0,0 +1,8 @@ +N 1 74 87 +N 2 154 202 +N 3 284 90 +N 4 156 113 +N 5 284 155 +N 6 245 233 +E 2 4 1 0 +E 4 1 1 0