lerpColor()/ fill()使用Processing抛出NullPointerException

时间:2012-10-22 18:15:23

标签: colors processing

这是一个类的代码:

class Circle extends PApplet {
  //var declarations

  Circle(int duration, int from, int to, PApplet parent, int x, int y, int length, int height){
    this.ani = new Tween(parent, 1.3f, Tween.SECONDS, Shaper.QUADRATIC);
    Class s = Shaper.QUADRATIC;
    this.from = from;
    this.to = to;
    this.len = length;
    this.height = height;
    this.x = x;
    this.y = y;
  }

  void update(){
    int c = lerpColor(this.from, this.to, this.ani.position(), RGB);

    fill(c);
    ellipse(this.x, this.y, this.len, this.height);
  }
}

当我在update()的正确播种版本上运行Circle时(参见下面的示例),我得到了这个堆栈跟踪:

Exception in thread "Animation Thread" java.lang.NullPointerException
at processing.core.PApplet.fill(PApplet.java:13540)
at ellipses.Circle.update(Ellipses.java:85)
at ellipses.Ellipses.draw(Ellipses.java:39)
at processing.core.PApplet.handleDraw(PApplet.java:2128)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:190)
at processing.core.PApplet.run(PApplet.java:2006)
at java.lang.Thread.run(Thread.java:662)

它告诉我在fill()里面,当它不应该是的时候,某些东西是空的。我首先假设传递给fill()的值有些不对。 fill()的值来自lerpColor(),因此我可能错误地使用了lerpColor()

我的Circle实例看起来像这样:

int c1 = color(45, 210, 240);
int c2 = color(135, 130, 195);

cir = new Circle(1, c1, c2, this, 100, 200, 140, 140);
cir.update();

那么我该如何正确使用fill() / lerpColor

(顺便说一句,我在eclipse中使用proclipsing进行处理。)

1 个答案:

答案 0 :(得分:2)

首先,我不完全确定为什么你需要从一个似乎不是你的窗口的类扩展PApplet,但我离题了。

如果我理解你要做什么,问题在于填充功能而不是lerpColor。如果你试图调用主PApplet的fill函数而这个Circle类不是它,那么你需要告诉它在哪个PApplet上调用它。即你已经寄出的父母。我会做这样的事情。

class Circle extends PApplet {
  //var declarations
  Tween ani;
  int from, to, x, y, len, heightz;

  PApplet parr; // prepare to accept the parent instance

  Circle(int duration, int from, int to, PApplet parent, int x, int y, int length, int height) {
    this.ani = new Tween(parent, 1.3f, Tween.SECONDS, Shaper.QUADRATIC);
    Class s = Shaper.QUADRATIC;
    this.from = from;
    this.to = to;
    this.len = length;
    this.heightz = height;
    this.x = x;
    this.y = y;
    parr = parent; // store the parent instance
  }
  void update() {
    color c = lerpColor(this.from, this.to, this.ani.position(), RGB);
    parr.fill(c); // call fill on the parent
    parr.ellipse(this.x, this.y, this.len, this.height); // this one obviously suffers from the same problem...
  }

我希望这有帮助! PK