import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MyPanel extends JPanel { public Graph g; private int nr; private Node start; private Node nmove; private int x; private int y; public MyPanel(){ g = new Graph(); nr = 1; } public void press(int x, int y){ start = g.getNode(x, y); if ( start == null){ g.addNode(nr, x, y); nr++; } } public void released (int x, int y){ Node ende = g.getNode(x, y); if (start != null){ if (ende == null){ g.addNode(nr, x, y); nr++; ende = g.getNode(x, y); } g.addEdge(start.getNumber(), ende.getNumber(), 1, false); } start = null; nmove=null; } public void move (int x, int y){ this. x = x; this. y = y; if(nmove!= null){ nmove.x = x; nmove.y =y; } } 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); for (Node n: this.g.getNodes()){ g.drawOval(n.x -10, n.y -10, 20, 20); g.drawString(""+n.getNumber(), n.x -4 , n.y +5); } for(Edge e: this.g.getEdges()){ Node a = e.getAnfang(); Node b = e.getEnde(); if(a.x > b.x){ a=e.getEnde(); b=e.getAnfang(); } double dx= b.x -a.x; double dy = b.y -a.y; double alpha = Math.atan(dy/dx); double ax = a.x + 10* Math.cos(alpha); double ay = a.y + 10* Math.sin(alpha); double bx = b.x -10 * Math.cos(alpha); double by = b.y - 10* Math.sin(alpha); g.drawLine((int)ax, (int)ay, (int)bx,(int) by); } if(start != null){ g.drawLine(start.x, start.y, this.x, this.y); } } }