Null Point Exception with color[] array..
in
Programming Questions
•
1 year ago
I'm getting NPE problem but cannot figure out why.
Here's my code... I tweaked the code by benja.(Thanks!)
- Circle[] circles;
- int numCircles = 13;
- int maxDistance; //max distance between circles when draw lines
- int minDistance;
- int m;
- float bgX;
- float bgY;
- float randomVal;
- float circleX;
- float circleY;
- PImage bg;
- PImage bgRed;
- color[] colors;
- int indexColors;
- float opa;
- void setup() {
- size(displayWidth, displayHeight); //size of window
- smooth(); //draw circles with anti-aliased edge
- m = millis();
- bgX = (displayWidth-800)/2;
- bgY = (displayHeight-800)/2;
- //frameRate(15);
- bg = loadImage("bg_trans.png");
- bgRed = loadImage("bg_red.png");
- float opa = random(100, 255);
- colors[0] = color(255, 206, 0, opa); //yellow
- colors[1] = color(255, 0, opa); // white
- colors[2] = color(0, 58, 142, opa); //blue
- colors[3] = color(226, 0, 0, opa); //red
- colors[4] = color(73, 73, 73, opa); //black(gray)
- indexColors = int(random(colors.length));
- // create an array and fill it with circles
- circles = new Circle[numCircles]; //create an array of circles
- for (int i=0; i< numCircles; i++) {
- float randomAngle = random(0, TWO_PI);
- float randomRadius = random(20, 350); // Not too close of the center, adjust at will
- float circleX = width/2 + cos(randomAngle) * randomRadius;
- float circleY = height/2 + sin(randomAngle) * randomRadius;
- println(circleX + ", " + circleY);
- //random position in the window, random size between 5-15
- circles[i] = new Circle(circleX, circleY, random(2, 10), colors[indexColors]);
- }
- }
- void draw() {
- // clear background
- background(0, 0, 25);
- tint(255, 50);
- image(bg, bgX, bgY);
- // tint(255, 50);
- // image(bgRed, bgX, bgY);
- // update and display the circles
- for (int i=0; i< numCircles; i++) {
- circles[i].update();
- circles[i].display();
- }
- // define maximum distance
- maxDistance = 180;
- //minDistance =50;
- //maxDistance =8mouseX; //manuplate lines by mouse cursor
- // look of the lines
- stroke(255, 130);
- strokeWeight(0.5);
- for (int i=0; i< numCircles; i++) {
- // compare circle to other circles
- for (int j=i+1; j< numCircles; j++) {
- // draw line if distance is below 'maxDistance'
- if (dist(circles[i].x, circles[i].y, circles[j].x, circles[j].y) < maxDistance){
- line(circles[i].x, circles[i].y, circles[j].x, circles[j].y);
- }
- }
- }
- }
- void mousePressed() {
- setup();
- }
Here's class Circle()
- class Circle {
- float x, y, dia, col;
- float speed=0;
- float gravity=0.2;
- float fallLimit = bgY + 780; //limit y for falling stars
- Circle(float x, float y, float dia, float col) {
- this.x =x;
- this.y =y;
- this.dia = dia;
- this.col = col;
- }
- //circles move slightly in their position
- void update() {
- // code for movement here
- // this is just some random displacement
- x = x + random(-0.5, 0.5);
- y = y + random(-0.5, 0.5);
- int m = millis();
- speed = speed + gravity;
- if(y < fallLimit){
- y = y + speed;
- }
- if (y > fallLimit) {
- y = fallLimit;
- }
- /*
- if( get(x,y) == color(255,0,0) ){
- y = y + speed;
- }
- if( get(x,y) == color(0,0,255) ){
- y=y;
- }
- */
- }
- void display() {
- // code for drawing the circles
- noStroke();
- fill(col);
- ellipse(x, y, dia, dia);
- }
- }
Is there anybody can fix the problem? I'm going to crazy because of this......T_T
1