diff --git a/Graph.java b/Graph.java index a297466..c2af13d 100644 --- a/Graph.java +++ b/Graph.java @@ -19,12 +19,23 @@ public class Graph this.nodes.add(new Node(nr)); } + public void addNode(int nr, int x, int y){ + this.nodes.add(new Node(nr, x, y)); + } + public void addEdge(Edge e){ this.edges.add(e); e.getAnfang().addEdge(e); e.getEnde().addEdge(e); } + public ArrayList getNodes(){ + return this.nodes; + } + + public ArrayList getEdges(){ + return this.edges; + } public void addEdge(int a, int e, int w, boolean g){ Node an = null; Node en = null; @@ -50,7 +61,15 @@ public class Graph } return null; } - + + public Node getNode(int x, int y){ + for(Node n: this.nodes){ + int dx = n.x - x; + int dy = n.y -y; + if (dx * dx + dy * dy <= 100) return n; + } + return null; + } public ArrayList breitensuche(int start){ ArrayList ergebnis = new ArrayList(); LinkedList q = new LinkedList(); diff --git a/MyPanel.java b/MyPanel.java index 9bd648e..bdb40fd 100644 --- a/MyPanel.java +++ b/MyPanel.java @@ -7,16 +7,79 @@ import java.awt.event.*; public class MyPanel extends JPanel { - public boolean linie = false; - public boolean kreis = false; - int x = 100; - int y = 100000000; + 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); + } @Override protected void paintComponent(Graphics g){ super.paintComponent(g); - - if (linie) g.drawLine(20,20,200,50); - if (kreis) g.drawOval(50, 50, 30, 30); - + 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); + } } } \ No newline at end of file diff --git a/Node.java b/Node.java index b665aa6..b84aee5 100644 --- a/Node.java +++ b/Node.java @@ -6,6 +6,8 @@ public class Node implements Comparable public int entfernung; public Node vorgaenger; public boolean bearbeitet; + public int x; + public int y; public Node(int n){ this.number = n; @@ -15,6 +17,15 @@ public class Node implements Comparable vorgaenger = null; } + public Node(int n, int x, int y){ + this.number = n; + this.x = x; + this.y = y; + this.edges = new ArrayList(); + + entfernung = -1; + vorgaenger = null; + } public int getNumber (){return this.number;} public void addEdge(Edge e){ diff --git a/Window.java b/Window.java index f018c19..76eeeab 100644 --- a/Window.java +++ b/Window.java @@ -11,44 +11,66 @@ public class Window implements ActionListener public Window(){ fenster = new JFrame("Mein erstes Fenster"); fenster.setSize(400,400); - + + fenster.setLayout( new BorderLayout()); + + JPanel top = new JPanel(); + top.setBackground(new Color(170,170,170)); + fenster.add(top, BorderLayout.PAGE_START); panel = new MyPanel(); - - fenster.add(panel); + + fenster.add(panel, BorderLayout.CENTER); panel.setBackground(new Color(100,100,100)); - + button = new JButton ("Linie"); - panel.add(button); + top.add(button); button.addActionListener(this); - + button2 = new JButton("Kreis"); - panel.add(button2); + top.add(button2); button2.addActionListener(this); - - + fenster.setVisible(true); - - fenster.addMouseListener(new MouseListener(){ - public void MouseEntered(MouseEvent e){}; - public void MousePressed(MouseEvent e){ - System.out.println("( " + e.getPoint().getX() + " | " + e.getPoint().getY() + " )"); - panel.x = (int)e.getPoint().getX(); - panel.y = (int)e.getPoint().getY(); - fenster.repaint(); - - }; - public void MouseReleased(MouseEvent e){}; - public void MouseCicked(MouseEvent e){}; - public void MouseExited(MouseEvent e){}; - - }); + panel.addMouseMotionListener(new MouseMotionListener(){ + public void mouseDragged(MouseEvent e){ + panel.move((int) e.getPoint().getX(), (int) e.getPoint().getY()); + fenster.repaint();} + + public void mouseMoved(MouseEvent e){ + panel.move((int) e.getPoint().getX(), (int) e.getPoint().getY()); + fenster.repaint(); + } + }); + panel.addMouseListener(new MouseListener(){ + public void mouseEntered(MouseEvent e){}; + + public void mousePressed(MouseEvent e){ + if (e.getButton()== MouseEvent.BUTTON1){ + panel.press((int) e.getPoint().getX(), (int) e.getPoint().getY()); + } + if (e.getButton()== MouseEvent.BUTTON3){ + panel.right((int) e.getPoint().getX(), (int) e.getPoint().getY()); + } + fenster.repaint(); + + }; + + public void mouseReleased(MouseEvent e){ + panel.released((int) e.getPoint().getX(), (int) e.getPoint().getY()); + fenster.repaint(); + }; + + public void mouseClicked(MouseEvent e){}; + + public void mouseExited(MouseEvent e){}; + + }); } - - + public void actionPerformed(ActionEvent e){ - if (e.getSource() == this.button) panel.linie = !panel.linie; - if (e.getSource() == this.button2) panel.kreis = !panel.kreis; - fenster.repaint(); + if (e.getSource() == this.button) + if (e.getSource() == this.button2) + fenster.repaint(); } }