Connect squares with mouse click
in
Programming Questions
•
7 months ago
Hi all,
I just started learning processing (again), and now i have some questions. I have one class called Square and it draws squares when clicked and it stores them in an ArrayList. The ArrayList adds it's elements dynamically. Now i wanted to add PVector lines from one point to another. When i create two squares on different coordinates i want them to connect with a line. When i create three squares i want them all to be connected. So by my logic i stored the coordinates in point1 and point2. But i don't know how to do this, so help :). Here is the code:
- float size;
- float speedX;
- float speedY;
- Square(float tempX, float tempY, float tempSize){
- posX = tempX;
- posY = tempY;
- size = tempSize;
- }
- void display(){
- rectMode(CENTER);
- fill(0);
- noStroke();
- rect(posX, posY, size, size);
- }
- }
- ArrayList square;
- PVector[] point1;
- PVector[] point2;
- float randomX = random(0, 600);
- void setup(){
- size(600, 600);
- background(255);
- smooth();
- square = new ArrayList();
- point1 = new PVector[100];
- point2 = new PVector[100];
- }
- void draw(){
- background(255);
- for(int i=0; i < square.size(); i++){
- Square s = (Square) square.get(i);
- s.display();
- }
- }
- void mouseReleased(){
- if(mouseButton == LEFT){
- square.add(new Square(mouseX, mouseY, random(10, 25)));
- for(int i=0; i < square.size(); i++){
- point1[i] = new PVector(mouseX, mouseY);
- println("First" + point1[i].get());
- }
- for(int y=0; y < square.size(); y++){
- point2[y] = new PVector(mouseX, mouseY);
- println("Second" + point2[y].get());
- }
- }
- }
1