diff --git a/src/com/tejpbit/graph/controller/CreateTool.java b/src/com/tejpbit/graph/controller/CreateTool.java index 71ab12e..c0270e6 100644 --- a/src/com/tejpbit/graph/controller/CreateTool.java +++ b/src/com/tejpbit/graph/controller/CreateTool.java @@ -14,12 +14,12 @@ /** * * @author André - * tool to create edges and verticies, also used for moving edges. + * tool to create Nodes and edges. */ public class CreateTool extends Tool implements IExtraPaintObject { private Node firstNode; - int x, y; + int x1, y1, x2, y2; /** * Gets the node at where the press occured.
@@ -27,58 +27,61 @@ public class CreateTool extends Tool implements IExtraPaintObject { */ @Override public void mousePressed(GraphView gView, int x, int y) { - System.out.println("Press"); firstNode = gView.getGraph().getNodeAt(x, y); + this.x1 = x; + this.y1 = y; } @Override public void mouseDragged(GraphView gView, int x, int y) { - this.x = x; - this.y = y; + this.x2 = x; + this.y2 = y; gView.addExtraPaintObject(this); gView.repaint(); } - @Override - public void paint(Graphics g) { - g.setColor(Color.BLUE); - g.drawLine(firstNode.centerX(), firstNode.centerY(), x, y); - } - /** * if there was two different nodes where the press and where the release occured. Draw an edge between them */ @Override public void mouseReleased(GraphView gView, int x, int y) { - System.out.println("Release"); Node n = gView.getGraph().getNodeAt(x, y); - if (n == null || firstNode == null) { - System.out.println("n: " + n); - System.out.println("firstNode: " + firstNode); - gView.repaint(); - return; + if (n != null && firstNode != null) { + gView.getGraph().addEdge(firstNode, n); + firstNode = null; + } else if (n == null && firstNode == null) { + } - gView.getGraph().addEdge(firstNode, n); - firstNode = null; gView.repaint(); } - /** * Method for mouse clicked. Adds a node if possible. */ @Override - public void mouseClicked(GraphView gView, int x, int y) { //TODO But how should one move nodes? + public void mouseClicked(GraphView gView, int x, int y) { System.out.println("Click"); Rect rect = new Rect(x, y , x, y); rect.inset( - NodeView.radius, - NodeView.radius); + // TODO you don't want this import JOptionPane if (! gView.getGraph().addNode(x, y)) { - JOptionPane.showMessageDialog(gView, "Can't place one node ontop of another.", "Bad position", JOptionPane.ERROR_MESSAGE); // TODO you don't want this import + JOptionPane.showMessageDialog(gView, "Can't place one node ontop of another.", "Bad position", JOptionPane.ERROR_MESSAGE); } gView.repaint(); } + + @Override + public void paint(Graphics g) { + g.setColor(Color.BLUE); + + if (firstNode != null) + g.drawLine(firstNode.centerX(), firstNode.centerY(), x2, y2); + else { + g.drawLine(x1, y1, x2, y2); + } + } } diff --git a/src/com/tejpbit/graph/controller/MainFrame.java b/src/com/tejpbit/graph/controller/MainFrame.java index ec70ed0..a069044 100644 --- a/src/com/tejpbit/graph/controller/MainFrame.java +++ b/src/com/tejpbit/graph/controller/MainFrame.java @@ -49,7 +49,7 @@ public class MainFrame extends JFrame { private CreateTool createTool; private DestroyTool destroyTool; - private InspectTool checkNodeTool; + private ManipulateTool checkNodeTool; public MainFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -88,7 +88,7 @@ public MainFrame() { graph.setViewCallBack(graphView); createTool = new CreateTool(); destroyTool = new DestroyTool(); - checkNodeTool = new InspectTool(nodeId, neighbours); + checkNodeTool = new ManipulateTool(nodeId, neighbours); initializeActionListeners(); diff --git a/src/com/tejpbit/graph/controller/InspectTool.java b/src/com/tejpbit/graph/controller/ManipulateTool.java similarity index 86% rename from src/com/tejpbit/graph/controller/InspectTool.java rename to src/com/tejpbit/graph/controller/ManipulateTool.java index 9385eee..55231a3 100644 --- a/src/com/tejpbit/graph/controller/InspectTool.java +++ b/src/com/tejpbit/graph/controller/ManipulateTool.java @@ -6,12 +6,12 @@ import com.tejpbit.graph.model.Node; import com.tejpbit.graph.view.GraphView; -public class InspectTool extends Tool{ +public class ManipulateTool extends Tool{ private final JTextField idField; private final JTextField neighbourField; - public InspectTool(JTextField idField, JTextField neighbourField) { + public ManipulateTool(JTextField idField, JTextField neighbourField) { this.idField = idField; this.neighbourField = neighbourField; } diff --git a/src/com/tejpbit/graph/view/GraphView.java b/src/com/tejpbit/graph/view/GraphView.java index bf439fb..5cf6640 100644 --- a/src/com/tejpbit/graph/view/GraphView.java +++ b/src/com/tejpbit/graph/view/GraphView.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.List; +import javax.swing.JOptionPane; import javax.swing.JPanel; import com.tejpbit.graph.controller.Tool; @@ -48,14 +49,12 @@ public void setActiveTool(Tool tool) { @Override public void paintComponent(Graphics g) { super.paintComponent(g); - System.out.println("repint!"); - System.out.println("number of nodes: " + nodes.size()); for (NodeView n : nodes) n.paint(g); - for (EdgeView e : edges) { + for (EdgeView e : edges) e.paint(g); - } + if (extraPaintObject != null) { extraPaintObject.paint(g); extraPaintObject = null;