Nodes & Edges
in
Programming Questions
•
2 years ago
Here is a very very simplified edition of an application I'm working on...it's supposed to display some circles connected by a line (edges). Circles and edges are different objects. My problem is that I want a circle to disappear if I click on it and the edge too should be deleted. Each edge link the root to each other circle. I got an error on line 64 that I don't know how to handle (IndexOutOfBounds...). I'm sure there's something really stupid I'm not seeing...I think I understand the error but I really don't know how to handle it...need your help guys...I'd appreciate any kind of help...Thank you =D
- ArrayList nodes;
- ArrayList edges;
- void setup(){
- size(500,500);
- nodes = new ArrayList();
- edges = new ArrayList();
- nodes.add( new Node(0));
- nodes.add( new Node(150, 275, 1));
- nodes.add( new Node(200, 275, 2));
- nodes.add( new Node(250, 275, 3));
- nodes.add( new Node(300, 275, 4));
- edges.add( new Edge((Node)nodes.get(0), (Node)nodes.get(1)));
- edges.add( new Edge((Node)nodes.get(0), (Node)nodes.get(2)));
- edges.add( new Edge((Node)nodes.get(0), (Node)nodes.get(3)));
- edges.add( new Edge((Node)nodes.get(0), (Node)nodes.get(4)));
-
- }
- void draw(){
- background(255,255,255);
- for(int i=0; i<nodes.size(); i++){
- Node n = (Node) nodes.get(i);
- n.display();
- }
- for(int j=0; j<edges.size(); j++){
- Edge e = (Edge) edges.get(j);
- e.display();
- }
- }
- class Node {
- int xpos, ypos;
- int id;
- Node(int i){
- id = i;
- xpos = width/2;
- ypos = height/5;
- }
-
- Node(int x, int y, int i){
- id = i;
- xpos = x;
- ypos = y;
-
- }
-
- void display(){
- smooth();
- ellipse(xpos, ypos, 20, 20);
- }
- }
- class Edge {
- int idOr, idDest;
-
- Edge(Node or, Node dest){
- idOr = or.id;
- idDest = dest.id;
- }
-
- void display(){
- smooth();
- Node orig = (Node) nodes.get(this.idOr);
- Node dest = (Node) nodes.get(this.idDest); // Processing underline this line when the error is generated
- strokeWeight(1);
- stroke(255,0,0,100);
- line(orig.xpos,
- orig.ypos,
- dest.xpos,
- dest.ypos);
- }
- }
- void mouseClicked() {
- for (int i = 1; i < nodes.size(); i++) {
- Node n = (Node) nodes.get(i);
- if(mouseButton==LEFT && dist(mouseX, mouseY, n.xpos, n.ypos) < 20){
-
- nodes.remove(i);
- nodes.trimToSize();
- // Here should be some code to handle edges when I delete,
- // clicking on it, a circle
- }
- }
- }
1