为什么会出现此错误? - 处理

时间:2012-11-20 07:02:31

标签: java processing

有人可以与我分享为什么我收到此错误?基本上这是一个我想要模拟基本植物生长的程序。我想这样做,花瓣都存放在一个圆圈阵列中。

Stem myStem;
Circle circles;

float scaleFactor=0.5;

void setup() {
  size(floor(400*scaleFactor), floor(800*scaleFactor));
  myStem = new Stem(200,800);

}

void draw() {

  background(150);
  smooth();
  Circle circles[];
  circles = new Circle[5];
  circles[0]  = new Circle(0, -40, 50, 50);
  circles[1]  = new Circle(0, -40, 50, 50);
  circles[2]  = new Circle(0, -40, 50, 50);
  circles[3]  = new Circle(0, -40, 50, 50);
  circles[4]  = new Circle(0, -40, 50, 50);

  for (int i = 0; i < circles.length; i++) {
   circles = ellipse(circles[i].c1, circles[i].c2, circles[i].c3, circles[i].c4);
   rotate(radians(72));
   circles[i] = Circle;
  }

  myStem.drawStem();

}

class Stem { 
  int initalloX=200;
  int initalloY=800;

  Stem(int tempInitalloX, int tempInitalloY) {
    initalloX = tempInitalloX;
    initalloY = tempInitalloY;

  }

  void drawStem() {
    background(#0DBADB);
    scale(scaleFactor, scaleFactor);
    stroke (12, 149, 11);
    fill (12, 149, 11);
    strokeWeight(10);
    line(initalloX, initalloY, initalloX, ((frameCount>250)?initalloY-500:initalloY-(2*frameCount)));
    //stem1
    if (frameCount>101) {
      noStroke();
      translate(initalloX, initalloY-200);
      scale(min((float)(frameCount-100)/100, 1), min((float)(frameCount-100)/100, 1));
      beginShape();
      vertex(0, 0);
      bezierVertex(-40, -5, -30, -40, -80, -20);
      bezierVertex(-47, -16, -52, 8, 0, 0);
      endShape(CLOSE);
      scale(1/min((float)(frameCount-100)/100, 1), 1/min((float)(frameCount-100)/100, 1));
      translate(-initalloX, -(initalloY-200));
    }
    //stem2
    if (frameCount>151) {
      noStroke();
      translate(initalloX, initalloY-300);
      scale(-min((float)(frameCount-150)/150, 1), min((float)(frameCount-150)/150, 1));
      beginShape();
      vertex(0, 0);
      bezierVertex(-40, -5, -30, -40, -80, -20);
      bezierVertex(-47, -16, -52, 8, 0, 0);
      endShape(CLOSE);
      scale(-1/min((float)(frameCount-150)/150, 1), 1/min((float)(frameCount-150)/150, 1));
      translate(-initalloX, -(initalloY-300));
    }
  }
}

class Circle {

  int c1 = 0;
  int c2 = -40;
  int c3 = 50;
  int c4 = 50;

  Circle(int tc1, int tc2, int tc3, int tc4) {
    c1 = tc1;
    c2 = tc2;
    c3 = tc3;
    c4 = tc4;
  }
 }

提前致谢...非常感谢所有帮助。

4 个答案:

答案 0 :(得分:1)

我想要宣布一个数组,我想到了一些新东西。

至于出了什么问题,看起来你正在使用一个名为“circle”的Circle变量,并将它与一个Circles数组混淆,并将其称为圆圈,这可能导致各种各样的问题。这可能是你应该专注于修复的。

答案 1 :(得分:1)

...猜测

班级中有两种圆圈定义

        Circle circles

        Circle[] circles

答案 2 :(得分:1)

我认为circles[i] = Circle;是错误。您不能将Type(Circle类)分配给变量(即Object或类的实例)

答案 3 :(得分:1)

除了已经指出的所有内容之外,请注意ellipse()是一个void方法,因此它不会返回任何内容。这样一条线就好 circle = ellipse(x,y,z,z) 没有任何意义。您可能想使用存储在ciclcle [i]中的值来绘制省略号,所以只需调用即可 ellipse(circles[i].c1, circles[i].c2, circles[i].c3, circles[i].c4); 不需要分配它。此外,我不明白为什么创建5个相等的圆圈。如果您的圈子对象只是存储数据,为什么要存储相同的数据五次?电话:

for (int i = 0; i < circles.length; i++) {
ellipse(0, -40, 50, 50);
rotate(radians(72));
}

会产生同样的效果。

除了在绘制结束时调用background()(通过myStem.drawStem())将隐藏之前绘制的所有内容。 然而,不需要重新创建数组并每秒重新分配值60次,您可以将其移动到设置。

我对您的代码进行了这些更改。它现在将编译。仍然“花瓣”在原点绘制,并且需要处理它们的填充/描边,但至少它正在运行:) 你可能想在你的圆圈类中制作一个显示方法...更像我指向你制作的另一篇文章。干杯!

Stem myStem;

//Circle circles; // double declaration
  Circle circles[]; // keeping the array one only

float scaleFactor=0.5;

void setup() {
  size(floor(400*scaleFactor), floor(800*scaleFactor));
  myStem = new Stem(200,800);

  //mpoved this to setup, no need to recreate each frame
  circles = new Circle[5];
  circles[0]  = new Circle(0, -40, 50, 50);
  circles[1]  = new Circle(0, -40, 50, 50);
  circles[2]  = new Circle(0, -40, 50, 50);
  circles[3]  = new Circle(0, -40, 50, 50);
  circles[4]  = new Circle(0, -40, 50, 50);
  // also smooth only needs to be called once
  // unless ther is a noSmooth() somewhere
  smooth();

}

void draw() {

  // moved this here
  background(#0DBADB);

  for (int i = 0; i < circles.length; i++) {
   ellipse(circles[i].c1, circles[i].c2, circles[i].c3, circles[i].c4);
   // note you may use this instead
   //ellipse(0, -40, 50, 50);
   rotate(radians(72));
  }

  myStem.drawStem();


}



class Stem { 
  int initalloX=200;
  int initalloY=800;

  Stem(int tempInitalloX, int tempInitalloY) {
    initalloX = tempInitalloX;
    initalloY = tempInitalloY;

  }

  void drawStem() {
    //background(#0DBADB); // this was hiding all other draws
    scale(scaleFactor, scaleFactor);
    stroke (12, 149, 11);
    fill (12, 149, 11);
    strokeWeight(10);
    line(initalloX, initalloY, initalloX, ((frameCount>250)?initalloY-500:initalloY-(2*frameCount)));
    //stem1
    if (frameCount>101) {
      noStroke();
      translate(initalloX, initalloY-200);
      scale(min((float)(frameCount-100)/100, 1), min((float)(frameCount-100)/100, 1));
      beginShape();
      vertex(0, 0);
      bezierVertex(-40, -5, -30, -40, -80, -20);
      bezierVertex(-47, -16, -52, 8, 0, 0);
      endShape(CLOSE);
      scale(1/min((float)(frameCount-100)/100, 1), 1/min((float)(frameCount-100)/100, 1));
      translate(-initalloX, -(initalloY-200));
    }
    //stem2
    if (frameCount>151) {
      noStroke();
      translate(initalloX, initalloY-300);
      scale(-min((float)(frameCount-150)/150, 1), min((float)(frameCount-150)/150, 1));
      beginShape();
      vertex(0, 0);
      bezierVertex(-40, -5, -30, -40, -80, -20);
      bezierVertex(-47, -16, -52, 8, 0, 0);
      endShape(CLOSE);
      scale(-1/min((float)(frameCount-150)/150, 1), 1/min((float)(frameCount-150)/150, 1));
      translate(-initalloX, -(initalloY-300));
    }
  }
}

class Circle {

  int c1 = 0;
  int c2 = -40;
  int c3 = 50;
  int c4 = 50;

  Circle(int tc1, int tc2, int tc3, int tc4) {
    c1 = tc1;
    c2 = tc2;
    c3 = tc3;
    c4 = tc4;
  }
 }