Lag with large collections of objects
in
Programming Questions
•
2 years ago
This runs smoothly on my computer except when large quanitites of raindrops are generated. Could anything be done to optimize performance or is this just the limitations of Java? mouseX controls wind, height - mouseY controls amount of rain.
- float wind,gravity;
- color sky;
- Raindrop drop;
- Ring ringlet;
- ArrayList rain;
- ArrayList rings;
- void setup(){
- size(800,600);
- sky = color(random(128) + 64);
- drop = new Raindrop();
- rain = new ArrayList();
- rings = new ArrayList();
- gravity = 10;
- ellipseMode(CENTER);
- smooth();
- }
- void draw(){
- background(sky);
- noFill();
- mouse();
- putRain();
- putRings();
- }
- void mouse(){
- wind = (mouseX / (width / 2.)) - 1.;
- for(int makeRain = 0; makeRain < ((height - mouseY) / 50) + 1; makeRain++){
- rain.add(new Raindrop());
- }
- }
- void putRain(){
- for(int rainPut = rain.size() - 1; rainPut > 0; rainPut--){
- drop = (Raindrop) rain.get(rainPut);
- drop.put();
- if(drop.y > height){
- rings.add(new Ring(drop.x,drop.s,drop.w,drop.c));
- rain.remove(rainPut);
- }
- }
- }
- void putRings(){
- if(rings.size() > 0){
- for(int ringPut = rings.size() - 1; ringPut > 0; ringPut--){
- ringlet = (Ring) rings.get(ringPut);
- ringlet.put();
- if(ringlet.a < 0){
- rings.remove(ringPut);
- }
- }
- }
- }
- class Ring {
- float x,o,s,w,a;
- color c;
- Ring(float putX, float putS, float putW, color putC){
- x = putX;
- s = putS;
- o = s;
- w = putW;
- c = putC;
- a = 255;
- }
- void put (){
- if(a >= 0){
- stroke(c);
- fill(c,a);
- strokeWeight(w);
- ellipse(x,height,s,o);
- s++;
- a-= ceil(a / 2) + 1;
- }
- }
- }
- class Raindrop {
- float x,y,s,w;
- color c;
- Raindrop(){
- x = random((width + (wind * 2))) - wind;
- y = 0;
- s = random(7) + 2;
- c = color(0,0,random(128) + 64);
- w = random(4) + 1;
- }
- void put(){
- strokeWeight(w);
- stroke(c);
- line(x,y,x + (wind * s),y + s);
- x += wind * s;
- y += gravity;
- }
- }
1