Hi, I've gotten numerous helps from this forum and I'm bring another question again.
I've made a sketch about falling circles and lines, with arrayList().
When I click the mouse, circles and connecting lines appear -> drop -> pile up.
With many people's advice(especially benja's one), my sketch have been developed a lot.
Let me show you my code first;
- ArrayList<Circle> circles;
- int numCircles = 13;
- int maxDistance; //max distance between circles when draw lines
- int m;
- float bgX;
- float bgY;
- float randomVal;
- float circleX;
- float circleY;
- PImage bg;
- PImage bgRed;
- PImage bgBlue;
- color[] colors;
- int indexColors;
- float[] opa;
- void setup()
- {
- size(1024, 768); //size of window
- smooth(); //draw circles with anti-aliased edge
- m = millis();
- //frameRate(15);
- //bg = loadImage("bg_trans.png");
- //bgRed = loadImage("bg_red.png");
- // bgBlue = loadImage("bg_blue.jpg");
- imageMode(CENTER);
- circles = new ArrayList<Circle>();
- createCircles();
- }
- void draw()
- {
- // clear background
- background(0,0,25);
- /*
- tint(255, 255);
- image(bgBlue, width/2, height/2);
- tint(255, 50);
- image(bg, width/2, height/2);
- tint(255, 0);
- image(bgRed, width/2, height/2);
- */
- for (int i=0; i< circles.size(); i++)
- {
- circles.get(i).update();
- circles.get(i).display();
- }
- // define maximum distance
- maxDistance = 180;
- //maxDistance =mouseX; //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.get(i).x, circles.get(i).y, circles.get(j).x, circles.get(j).y) < maxDistance){
- line(circles.get(i).x, circles.get(i).y, circles.get(j).x, circles.get(j).y);
- }
- }
- }
- }
- void mousePressed()
- {
- createCircles();
- }
- void createCircles()
- {
- opa = new float[5];
- colors = new color[5];
- opa[0] = random(100, 255);
- opa[1] = random(100, 255);
- opa[2] = random(100, 255);
- opa[3] = random(100, 255);
- opa[4] = random(100, 255);
- colors[0] = color(255, 206, 0, opa[0]); //yellow
- colors[1] = color(255, opa[1]); // white
- colors[2] = color(0, 58, 142, opa[2]); //blue
- colors[3] = color(226, 0, 0, opa[3]); //red
- colors[4] = color(73,73,73, opa[4]); //black(gray)
- for (int i=0; i<numCircles; i++)
- {
- //create circles in big circular area
- 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);
- indexColors = int(random(colors.length));
- circles.add(new Circle(circleX, circleY, random(5, 15), colors[indexColors]));
- }
- }
- class Circle {
- float x, y, dia;
- color col;
- float speed=0;
- float gravity=0.15;
- float fallLimit = height/2 + 350; //limit y for falling stars
- Circle(float x, float y, float dia, color 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);
- }
- }
This is all I have been working for. However, it has two serious problems...
1. It should start with blank screen but at the start of application, a set of circles and lines appear.
I guess it is because of line 66-90, in void draw(). I tried to put them inside void createCircles() but failed. There's something wrong....
2. When I click the mouse, circles appear but lines do not. I'm only guessing why. What I guess is;
I used '(circles.get(i).x, circles.get(i).y, circles.get(j).x, circles.get(j).y);' when I draw lines.
BUT, when I draw circles, I used 'circleX, circleY'.
I don't know why, but I'm only guessing it is wrong.
As for 1st problem, I failed and for 2nd problem, I cannot even approach to it.
Would anybody teach me how to fix it, please?
1