Class 如何在处理中打印ArrayList以及如何更新背景 ArrayList单元; int j=50; 无效设置(){ 大小(500500); 单元格=新的ArrayList(); int q=int(随机(1,5));//红色填充值//填充*51 对于(inti=0;i
,您可能应该将您的问题分成几个单独的问题,或者前往处理论坛寻求更深入的帮助 从ArrayList打印 要从数组列表打印Class 如何在处理中打印ArrayList以及如何更新背景 ArrayList单元; int j=50; 无效设置(){ 大小(500500); 单元格=新的ArrayList(); int q=int(随机(1,5));//红色填充值//填充*51 对于(inti=0;i,class,background,syntax-error,processing,void,Class,Background,Syntax Error,Processing,Void,,您可能应该将您的问题分成几个单独的问题,或者前往处理论坛寻求更深入的帮助 从ArrayList打印 要从数组列表打印单元格,您需要迭代列表并访问要打印的单元格类部分。例如: 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 *
单元格
,您需要迭代列表并访问要打印的单元格类部分。例如:
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 "[shades_game$Cell@6ec30aea]"
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);
}
}
在底部停止矩形您的问题是
rectMode(CORNERS)
使矩形越来越长。删除该行并将if语句更改为if(y对于打印部分,您还可以重写类中的toString
方法
比如:
ArrayList单元;
int j=50;
无效设置(){
大小(500500);
单元格=新的ArrayList();
int q=int(随机(1,5));//红色填充值//填充*51
对于(int i=0;我的问题有点太宽泛了。我建议将你的两个问题分成几个单独的问题,并在每个问题上贴一个小问题。你说“它不起作用”是什么意思?
for (Cell c : cells) {
println("Position: " + c.x + "," + c.y);
}