button3!
parent
91336d513d
commit
e80dfebed5
23
Edge.java
23
Edge.java
|
@ -4,23 +4,38 @@ public class Edge implements Comparable
|
||||||
private Node ende;
|
private Node ende;
|
||||||
private int wert;
|
private int wert;
|
||||||
private boolean gerichtet;
|
private boolean gerichtet;
|
||||||
|
|
||||||
public Edge(Node a, Node e, int w, boolean r){
|
public Edge(Node a, Node e, int w, boolean r){
|
||||||
this.anfang = a;
|
this.anfang = a;
|
||||||
this.ende = e;
|
this.ende = e;
|
||||||
this.wert = w ;
|
this.wert = w ;
|
||||||
this.gerichtet = r;
|
this.gerichtet = r;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node getAnfang(){return this.anfang;}
|
public Node getAnfang(){return this.anfang;}
|
||||||
|
|
||||||
public Node getEnde(){return this.ende;}
|
public Node getEnde(){return this.ende;}
|
||||||
|
|
||||||
public int getWert(){return this.wert;}
|
public int getWert(){return this.wert;}
|
||||||
|
|
||||||
public boolean istGerichtet(){return this.gerichtet;}
|
public boolean istGerichtet(){return this.gerichtet;}
|
||||||
|
|
||||||
public int compareTo(Object o) {
|
public int compareTo(Object o) {
|
||||||
Edge other = (Edge)o;
|
Edge other = (Edge)o;
|
||||||
if(this.wert < other.wert)return -1;
|
if(this.wert < other.wert)return -1;
|
||||||
if(this.wert > other.wert)return 1;
|
if(this.wert > other.wert)return 1;
|
||||||
return 0;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
58
Graph.java
58
Graph.java
|
@ -1,6 +1,8 @@
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
public class Graph
|
public class Graph
|
||||||
{
|
{
|
||||||
private ArrayList<Node> nodes;
|
private ArrayList<Node> nodes;
|
||||||
|
@ -36,6 +38,7 @@ public class Graph
|
||||||
public ArrayList<Edge> getEdges(){
|
public ArrayList<Edge> getEdges(){
|
||||||
return this.edges;
|
return this.edges;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addEdge(int a, int e, int w, boolean g){
|
public void addEdge(int a, int e, int w, boolean g){
|
||||||
Node an = null;
|
Node an = null;
|
||||||
Node en = null;
|
Node en = null;
|
||||||
|
@ -70,6 +73,7 @@ public class Graph
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Node> breitensuche(int start){
|
public ArrayList<Node> breitensuche(int start){
|
||||||
ArrayList<Node> ergebnis = new ArrayList<Node>();
|
ArrayList<Node> ergebnis = new ArrayList<Node>();
|
||||||
LinkedList<Node> q = new LinkedList<Node>();
|
LinkedList<Node> q = new LinkedList<Node>();
|
||||||
|
@ -90,7 +94,26 @@ public class Graph
|
||||||
|
|
||||||
return ergebnis;
|
return ergebnis;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void tiefensucheGraphisch(){
|
||||||
|
ArrayList<Node> ergebnis = new ArrayList<Node>();
|
||||||
|
LinkedList<Node> q = new LinkedList<Node>();
|
||||||
|
//q.add(this.getNode(start));
|
||||||
|
|
||||||
|
while(!q.isEmpty()){
|
||||||
|
Node aktuell = q.removeLast();
|
||||||
|
if (ergebnis.contains(aktuell)){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayList<Node> erreichbar = aktuell.getTargets();
|
||||||
|
Collections.sort(erreichbar, Collections.reverseOrder());
|
||||||
|
q.addAll(erreichbar);
|
||||||
|
|
||||||
|
ergebnis.add(aktuell);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public ArrayList<Node> tiefensuche(int start){
|
public ArrayList<Node> tiefensuche(int start){
|
||||||
ArrayList<Node> ergebnis = new ArrayList<Node>();
|
ArrayList<Node> ergebnis = new ArrayList<Node>();
|
||||||
LinkedList<Node> q = new LinkedList<Node>();
|
LinkedList<Node> q = new LinkedList<Node>();
|
||||||
|
@ -212,4 +235,39 @@ public class Graph
|
||||||
}
|
}
|
||||||
return sb;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
20
MyPanel.java
20
MyPanel.java
|
@ -13,6 +13,7 @@ public class MyPanel extends JPanel
|
||||||
private Node nmove;
|
private Node nmove;
|
||||||
private int x;
|
private int x;
|
||||||
private int y;
|
private int y;
|
||||||
|
|
||||||
public MyPanel(){
|
public MyPanel(){
|
||||||
g = new Graph();
|
g = new Graph();
|
||||||
nr = 1;
|
nr = 1;
|
||||||
|
@ -22,7 +23,7 @@ public class MyPanel extends JPanel
|
||||||
start = g.getNode(x, y);
|
start = g.getNode(x, y);
|
||||||
if ( start == null){
|
if ( start == null){
|
||||||
g.addNode(nr, x, y);
|
g.addNode(nr, x, y);
|
||||||
nr++;
|
nr++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,6 +41,7 @@ public class MyPanel extends JPanel
|
||||||
start = null;
|
start = null;
|
||||||
nmove=null;
|
nmove=null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void move (int x, int y){
|
public void move (int x, int y){
|
||||||
this. x = x;
|
this. x = x;
|
||||||
this. y = y;
|
this. y = y;
|
||||||
|
@ -52,6 +54,22 @@ public class MyPanel extends JPanel
|
||||||
public void right(int x, int y){
|
public void right(int x, int y){
|
||||||
this.nmove = g.getNode(x,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
|
@Override
|
||||||
protected void paintComponent(Graphics g){
|
protected void paintComponent(Graphics g){
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
|
|
|
@ -63,4 +63,8 @@ public class Node implements Comparable
|
||||||
if(this.number > other.number)return 1;
|
if(this.number > other.number)return 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String toString(){
|
||||||
|
return "N " + this.number + " " + this.x + " " + this.y;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
34
Window.java
34
Window.java
|
@ -8,6 +8,9 @@ public class Window implements ActionListener
|
||||||
private MyPanel panel;
|
private MyPanel panel;
|
||||||
private JButton button;
|
private JButton button;
|
||||||
private JButton button2;
|
private JButton button2;
|
||||||
|
private JButton button3;
|
||||||
|
private JButton button4;
|
||||||
|
|
||||||
public Window(){
|
public Window(){
|
||||||
fenster = new JFrame("Mein erstes Fenster");
|
fenster = new JFrame("Mein erstes Fenster");
|
||||||
fenster.setSize(400,400);
|
fenster.setSize(400,400);
|
||||||
|
@ -22,14 +25,23 @@ public class Window implements ActionListener
|
||||||
fenster.add(panel, BorderLayout.CENTER);
|
fenster.add(panel, BorderLayout.CENTER);
|
||||||
panel.setBackground(new Color(100,100,100));
|
panel.setBackground(new Color(100,100,100));
|
||||||
|
|
||||||
button = new JButton ("Linie");
|
button = new JButton ("Speichern");
|
||||||
top.add(button);
|
top.add(button);
|
||||||
button.addActionListener(this);
|
button.addActionListener(this);
|
||||||
|
|
||||||
button2 = new JButton("Kreis");
|
button2 = new JButton("Laden");
|
||||||
top.add(button2);
|
top.add(button2);
|
||||||
button2.addActionListener(this);
|
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);
|
fenster.setVisible(true);
|
||||||
panel.addMouseMotionListener(new MouseMotionListener(){
|
panel.addMouseMotionListener(new MouseMotionListener(){
|
||||||
public void mouseDragged(MouseEvent e){
|
public void mouseDragged(MouseEvent e){
|
||||||
|
@ -68,9 +80,19 @@ public class Window implements ActionListener
|
||||||
}
|
}
|
||||||
|
|
||||||
public void actionPerformed(ActionEvent e){
|
public void actionPerformed(ActionEvent e){
|
||||||
if (e.getSource() == this.button)
|
if (e.getSource() == this.button) {
|
||||||
if (e.getSource() == this.button2)
|
panel.save();
|
||||||
fenster.repaint();
|
}
|
||||||
|
if (e.getSource() == this.button2) {
|
||||||
|
panel.load();
|
||||||
|
}
|
||||||
|
if(e.getSource() == this.button3){
|
||||||
|
panel.clear();
|
||||||
|
}
|
||||||
|
if(e.getSource() == this.button4){
|
||||||
|
panel.tiefensuche();
|
||||||
|
}
|
||||||
|
fenster.repaint();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue