2015-06-08 2 views
-2
ArrayList<Cell> cells; 
int j=50; 

void setup() { 
    size(500, 500); 
    cells=new ArrayList<Cell>(); 
    int q=int(random(1, 5)); //red fill value //fill *51 
    for (int i=0; i<1; i++) { 
    cells.add(new Cell(0, 0, 15, color(q*51, 0, 0), 100, 50)); //adds a new cell for each iteration 
    j+=50; 
    println(q+" "+i); //prints value of the red and then iteration of the for loop 

    } 
} 

    void draw() { 
    printArray(cells); //prints my array list but it is not working prints "[[email protected]]" 

    for (int i=0; i<cells.size(); i++) { 
     background(255); //supposed to remove all of the other square trail 
     cells.get(i).display(); //displays each rectangle 
    } 
    } 

    class Cell { 
    private int x; 
    private int y; 
    private int speed; 
    private color c; 
    private int wwidth; 
    private int hheight; 
    public Cell(int xpos, int ypos, int speedy, color col, int widthh, int heightt) { // then set all of the private vars to these public ones 
     x=xpos; 
     y=ypos; 
     speed=speedy; 
     c=col; 
     wwidth=widthh; 
     hheight=heightt; 
    } 
    void display() { 
     fill(c); 
     rectMode(CORNERS); 
     rect(x, y, wwidth, hheight); 
     if (y<=height+hheight) { //if y location of the rect is less than the window then increase y pos 
     y+=speed; //increase y by speed 
     } 
     println(y+" "+speed); 
    } 
    } 

Я хочу, чтобы прямоугольник не следовал за прямоугольником. Мне также нужно распечатать ячейки ArrayList, но String s: cells println (s) не работает ... Может кто-нибудь мне помочь? Я хочу, чтобы прямоугольник приземлялся внизу и сидел там, как он, но без длинной строки прямоугольников, следующих за ним. Любая помощь будет принята с благодарностью.Как распечатать ArrayList в процессе обработки и как обновить фон

Edit: проще работать

+0

Этот вопрос является слишком широким. Я предлагаю разделить два вопроса на отдельные вопросы и разместить меньший [MCVE] (http://stackoverflow.com/help/mcve) вместе с каждым. Что именно вы имеете в виду, когда говорите «это не работает»? –

ответ

0

Вы должны, вероятно, разделить свой вопрос на отдельные из них, или над головой обработки форума для получения дополнительной помощи в глубину.

Печать из ArrayList
Для печати cell из списка массива, вы итерация списка и получить доступ к части класса Cell вы хотите напечатать. Например:

for (Cell c : cells) { 
    println("Position: " + c.x + "," + c.y); 
} 

Стоп прямоугольник в нижних
Вашей проблемой является rectMode(CORNERS), которая делает прямоугольник длиннее и длиннее. Удалите эту строку и измените оператор if на if (y <= height-hheight), и он работает как ожидалось.

0

Для части печати вы также можете переопределить метод toString в классе.

нравится:

ArrayList<Cell> cells; 
int j=50; 

void setup() { 
    size(500, 500); 
    cells=new ArrayList<Cell>(); 
    int q=int(random(1, 5)); //red fill value //fill *51 
    for (int i=0; i<1; i++) { 
    cells.add(new Cell(0, 0, 15, color(q*51, 0, 0), 100, 50)); //adds a new cell for each iteration 
    j+=50; 
    println(q+" "+i); //prints value of the red and then iteration of the for loop 
    } 
} 

void draw() { 
    printArray(cells); //prints my array list but it is not working prints "[[email protected]]" 

    for (int i=0; i<cells.size(); i++) { 
    background(255); //supposed to remove all of the other square trail 
    cells.get(i).display(); //displays each rectangle 
    } 
} 

class Cell { 
    private int x; 
    private int y; 
    private int speed; 
    private color c; 
    private int wwidth; 
    private int hheight; 
    public Cell(int xpos, int ypos, int speedy, color col, int widthh, int heightt) { // then set all of the private vars to these public ones 
    x=xpos; 
    y=ypos; 
    speed=speedy; 
    c=col; 
    wwidth=widthh; 
    hheight=heightt; 
    } 
    void display() { 
    fill(c); 
    rect(x, y, wwidth, hheight); 
    if (y<=height+hheight) { //if y location of the rect is less than the window then increase y pos 
     y+=speed; //increase y by speed 
    } 
    } 

    @Override 
    String toString() { 
    String s = "\nbuild your string...\n"; 
    s+= "like:\nx = " + x + "\n"; 
    s+= "speed = " + speed + "\n"; 
    s+= "and so on...\n"; 
    return s; 
    } 
} 
Смежные вопросы