0

のすべての値に四角形が追加されたグラフィックを作成したいと考えていますArrayList。期待される最終結果は、 のすべての値に対して水平方向の一連の長方形ArrayListです。

次のコードを試してみましたがNullPointerException、コードを実行するとエラーが発生します。私が期待する出力を得る方法を誰か教えてもらえますか?

import processing.core.*;
public class Processing extends PApplet {
    ArrayList<Integer> test = new ArrayList<Integer>();
    private PApplet parent;

    int x = 100;
    int y = 100;
    public void setup() {
          size(640, 600);
          background(255);
          test.add(10);
            test.add(20);
            test.add(30);
          noLoop();
        }

        public void draw() {
          for(int testing: test){
              printTask(x,y);
          }
        }

        public void printTask(int x, int y){
            parent.rect(10,10,x,y);
        }
        static public void main(String args[]) {
            PApplet.main(new String[] { "--bgcolor=#ECE9D8", "Processing" });
        }
}

例外のスタック トレース:

Exception in thread "Animation Thread" java.lang.NullPointerException
  at Processing.printTask(Processing.java:30)
  at Processing.draw(Processing.java:25)
  at processing.core.PApplet.handleDraw(PApplet.java:1602)
  at processing.core.PApplet.run(PApplet.java:1503)
  at java.lang.Thread.run(Unknown Source)
4

3 に答える 3

0

これは、処理方法で記述された場合のコードの外観です;-) テストに保存された値を使用していなかったので、正方形のサイズに使用しました...また、メソッドにはわかりやすい方法で名前を付ける必要があると思います。 「drawRects()」のように

[編集] 私はあなたの目標の説明に近づくようにコードを編集したと思います... :)

    ArrayList<Integer> test = new ArrayList<Integer>();

void setup() {
  size(640, 600);
  background(255);
  for (int i = 1; i< 60;i++){
    test.add(i*10);
  }
  noFill();
  noLoop();
}

void draw() {
  for (int testing: test) {
    printTask(testing,testing);
  }
}

void printTask(int x, int y) {
  rect(x, 10, 10, y);
}
于 2013-07-05T15:55:21.880 に答える