Adding the number of boxes

edited November 2016 in Questions about Code

Hi everyone, I have a problem with my project. I want to add more boxes into my project by mousePressed function.

Box[] boxes=new Box[5];
 void setup(){
   size(200,200);
   rectMode(CENTER);
   for(int i=0;i<boxes.length;i++){
     boxes[i]= new Box( random(100),random(100), random(5,15)
     );
   }
 }
  void draw(){
    background(192);
    for(int i=0;i<boxes.length;i++){
      boxes[i].draw();
    }
    for(int j=0;j<boxes.length;j++){
      for(int k=0; k< boxes.length; k++){
        if(j !=k){
          boxes[j].attract(boxes[k]);
        }
      }
    }
  }

  void mousePressed(){
    for(int i=0;i<boxes.length;i++){
      boxes[i].mousePressed();
    }
  }

Answers

  • code is unreadable

    edit post, highlight code, press ctrl-o

  • mousePressed needs to add a new Box, the same way it did with the first 5 in setup()

    but first you'll have to increase the size of the array.

    but tbh, you'd be better off with an ArrayList than with a Box[].

  • Answer ✓

    you are also missing the Box class.

  • this is my Box Classes:

    class Box{
      int frame = 0;
      float posX;
      float posY;
      float movX;
      float movY;
      float side;
      boolean spinning = true;
    
      Box(float x,float y,float s){
        posX=x;
        posY=y;
        side=s;
        movX=random(-1,1);
        movY=random(-1,1);
      }
       void draw(){
         posX+=movX;
         posY+=movY;
         pushMatrix();
         translate(posX,posY);
         rotate(0.1*frame);
         rect(0,0,side,side);
         if (spinning){
           frame++;
         }
         popMatrix();
       }
         void mousePressed(){
           if (dist(mouseX,mouseY,posX,posY)<side/2){
             spinning = !spinning;
           }
         }
    
    
         void attract(Box b){
           float d=dist(posX,posY,b.posX,b.posY);
           float diffX=b.posX-posX;
           float diffY=b.posY-posY;
           movX+=diffX/sq(d);
           movY+=diffY/sq(d);
       }
    
    
    }
    
  • edited November 2016

    But i can not increase the number of box. I tried to do :

    float num ;
    Box[] boxes=new Box[num];
    ..... 
    if( mousePressed = true) {
    num = num++;
    }
    

    but it did not work.... Can you help me?

  • line 2 initialises the size of the array to the current value of num, set in line 1. changing num later won't change the size of the array.

    there is a method to expand an array, it's in the reference. it is, however, terrible - arrays aren't meant to be extended. which is why i suggested using an ArrayList.

  • Can you explain for me? How to apply array list in this? I am quite confused now :(

  • Box[] boxes=new Box[5];
    

    becomes

    ArrayList<Box> boxes = new ArrayList<Box>();
    

    so that's a new ArrayList full of things of type Box. note that it doesn't have a limit to how big it can be, it can handle more than 5.

    instead of setting array entries to be new Box you add() them so

    boxes[i]= new Box( random(100),random(100), random(5,15));
    

    becomes

    boxes.add(new Box( random(100),random(100), random(5,15)));
    

    you can do this as many times as you like, ie within your mousePressed() method.

    and accessing them is not boxes[i] but boxes.get(i);

  • I tried , but when we change it , we will not use i, will we ?

  • for(int i=0;i<boxes.length;i++){
      boxes[i].draw();
    }
    

    ...

    for (int i = 0 ; i < boxes.size() ; i++){
      boxes.get(i).draw();
    }
    

    but if all you're using i for is to iterate over all the boxes then you don't need it and can use

    for (Box b : boxes) {
      b.draw();
    }
    
  • To be honest, Have you tested it? although I tried many times and following your induction, It does not work

  • Answer ✓

    Implementing @koogs suggestions:

    Kf

    //Box[] boxes=new Box[5];
    ArrayList<Box> boxes = new ArrayList<Box>();
    int initBoxNumber=5;
    
    void setup() {
      size(200, 200);
      rectMode(CENTER);
      for (int i=0; i<initBoxNumber; i++) {
        boxes.add(new Box( random(100),random(100), random(5,15)));
      }
    }
    void draw() {
      background(192);
      for (int i=0; i<boxes.size(); i++) {
        boxes.get(i).draw();
      }
      for (int j=0; j<boxes.size(); j++) {
        for (int k=0; k< boxes.size(); k++) {
          if (j !=k) {
            boxes.get(j).attract(boxes.get(k));
          }
        }
      }
    }
    
    void mousePressed() {
    
      //THESE next lines ... not sure what they are doing....
      //for (int i=0; i<boxes.length; i++) {
      //  boxes[i].mousePressed();
      //}
    
      //ADDING a new box... you just need to call
      boxes.add(new Box( random(100),random(100), random(5,15)));  //Same line as in setup
    }
    
  • thank you very very much kfrajer , it is fantastic :D

Sign In or Register to comment.