private void openMenuItemActionPerformed( java.awt.event.ActionEvent evt) { // GEN-FIRST:event_openMenuItemActionPerformed this.jTextStatus.setText(""); if (this.jOpenFileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File file = this.jOpenFileChooser.getSelectedFile(); this.clearAll(); try { BufferedReader input = new BufferedReader(new FileReader(file)); try { String line = null; line = input.readLine(); while (line != null) { String splitLine[] = line.split(":"); if (splitLine[0].equals("Vertex")) { int d = Integer.parseInt(splitLine[1]); int x = Integer.parseInt(splitLine[2]); int y = Integer.parseInt(splitLine[3]); this.pnGraph.listVertexs.add(new Vertex(x, y, d, -1)); } else if (splitLine[0].equals("Edge")) { Vertex headVertex = this.pnGraph.listVertexs.get(Integer.parseInt(splitLine[1])); Vertex tailVertex = this.pnGraph.listVertexs.get(Integer.parseInt(splitLine[2])); int l = Integer.parseInt(splitLine[3]); Edge edge = new Edge(headVertex, tailVertex, l); edge.edgeType = this.pnGraph.checkEdge(headVertex, tailVertex); headVertex.outgoingEdges.add(edge); tailVertex.incomingEdges.add(edge); this.pnGraph.listEdges.add(edge); } line = input.readLine(); } this.jTextStatus.setText("Load graph successfully"); } finally { input.close(); } } catch (IOException e) { } } } // GEN-LAST:event_openMenuItemActionPerformed
private void pnGraphMouseReleased( java.awt.event.MouseEvent evt) { // GEN-FIRST:event_pnGraphMouseReleased int x = 0; int y = 0; Vertex vertex = null; if (this.drawType != DrawType.NODRAW || this.start || this.end) { x = evt.getX() - jGraphPanel.radius / 2; y = evt.getY() - jGraphPanel.radius / 2; if (x < 0 || y < 0) { this.jTextStatus.setText("Invalid position"); this.drawType = DrawType.NODRAW; return; } vertex = this.pnGraph.checkInVertex(x, y); if (vertex != null) if (vertex.state != State.LABELED) if (this.start) { vertex.state = State.LABELED; vertex.setKey(0); this.startVertex = vertex; this.jTextStatus.setText(""); this.jSolution.append( "Starting vertex\t: Vertex " + Integer.toString(vertex.getData()) + "\n"); this.start = false; } else if (this.end) { vertex.state = State.LABELED; this.endVertex = vertex; this.jTextStatus.setText(""); this.jSolution.append( "Ending vertex\t: Vertex " + Integer.toString(vertex.getData()) + "\n"); this.end = false; } } if (this.drawType == DrawType.VERTEXS) { if (vertex != null) { this.jTextStatus.setText("Concurred with another vertex"); } else { this.pnGraph.listVertexs.add(new Vertex(x, y, this.numVertexs, -1)); this.numVertexs++; } } else if (this.drawType == DrawType.EDGES) { if (vertex == null) this.jTextStatus.setText("You must click on exist vertex"); else if (vertex == this.head) this.jTextStatus.setText("You must choose another vertex"); else { if (draw) { this.pnGraph.mDrag = false; this.pnGraph.head = null; Edge edge = new Edge(this.head, vertex, 10); edge.edgeType = this.pnGraph.checkEdge(this.head, vertex); if (this.jRandomCheck.isSelected()) { Random random = new Random(); edge.setLength(random.nextInt(20)); } else edge.setLength( Integer.parseInt( JOptionPane.showInputDialog( null, "Length of edge", "Edge", JOptionPane.QUESTION_MESSAGE))); this.head.outgoingEdges.add(edge); vertex.incomingEdges.add(edge); this.pnGraph.listEdges.add(edge); this.head = null; this.jTextStatus.setText("Draw an edge"); this.draw = false; } else { this.head = vertex; this.pnGraph.head = vertex; this.pnGraph.xM = evt.getX(); this.pnGraph.yM = evt.getY(); this.pnGraph.mDrag = true; this.jTextStatus.setText("Choose tail vertex"); this.draw = true; } } } this.pnGraph.repaint(); } // GEN-LAST:event_pnGraphMouseReleased