loper
Ex Member
How to switch from a loaded file to another one.
Nov 2nd , 2009, 4:28am
Hello, I created a small program that visualize data loaded from a CSV file, I'm trying to create a switch that skips from one CSV file to another as the mouse clicks. How can I get it to work? int numBalls = 200; float spring = 0.008; float friction =0.1; boolean sw; String[]lines; Record[]records; int recordCount; PFont font; Ball[] balls = new Ball[numBalls]; Att[] att = new Att[numBalls]; void setup() { size(screen.width, screen.height); noStroke(); smooth(); sw=true; font = loadFont("Monospaced-12.vlw"); for (int i = 0; i < numBalls; i++) { balls[i] = new Ball(random(width), random(height), 0, balls); att[i]=new Att(); } for (int i = 0; i < numBalls; i++) { att[i].init(); } lines=loadStrings("2009.csv"); records=new Record[lines.length]; for(int i=0; i < lines.length; i++){ String[] pieces = split(lines[i], ';'); if (pieces.length==2){ records[recordCount]= new Record(pieces); recordCount++; } } } void draw() { background(242,255,229); mouseOver(); if(sw==true){ lines = loadStrings("2008.csv"); //println(sw); } else{ lines = loadStrings("2009.csv"); //println(sw); } for(int i=0; i < recordCount; i++){ balls[i].pos=i+1; balls[i].diameter=sqrt(records[i].val)*0.2; balls[i].name=records[i].name; //balls[i].dim=lines[i]; att[i].k=(records[i].val)*0.000006; } for (int i = 0; i < numBalls; i++) { balls[i].move(); balls[i].display(); // att[i].render(); att[i].update(); float dx=(balls[i].x-att[i].x); float dy=(balls[i].y-att[i].y); float d=dist(balls[i].x,balls[i].y,att[i].x,att[i].y); if(d<1){ balls[i].kx=0; balls[i].ky=0; balls[i].x=att[i].x; balls[i].y=att[i].y; } else{ balls[i].kx=(-dx/(d))*100; balls[i].ky=(-dy/(d))*100; } } } class Ball { float x, y; float diameter; float vx = 0; float vy = 0; int id; String name; int pos; Ball[] others; float alp=40; float kx,ky; boolean over=false; Ball(float xin, float yin, float din, Ball[] oin) { x = xin; y = yin; diameter = din; others = oin; } void move() { x += vx+kx/100; y += vy+ky/100; } void name(){ textFont (font); float tw= textWidth(name+pos); float th= textAscent(); noStroke(); fill(255); rect(x-10+diameter,y+diameter-th-5,tw+30,th+10); fill(0, 102, 153); text(pos+"°"+" "+name,x+diameter,y+diameter); } void display() { if(over==false){ noStroke(); fill(106,22,61,220); ellipse(x, y, diameter, diameter); over=true; } else{ noStroke(); fill(106,22,61,30); ellipse(x, y, diameter, diameter); noFill(); stroke(0); strokeWeight(0.4); ellipse(width/2,height/2,dist(x,y,width/2,height/2)*2,dist(x,y,width/2,height/2) *2); over=false; } } } class Att{ float x; float y; float a; float k; float molt =0.000001; void init(){ a=random(0,10); } void update(){ a+=(1/k)*molt; x=sin(a)*(1/k)+width/2; y=cos(a)*(1/k)+height/2; } void render(){ stroke(255); strokeWeight(1); point (x,y); } } class Record{ String name; float val; public Record(String[] pieces){ name = pieces[0]; val = float(pieces[1]); } } void mouseOver(){ for (int i = 0; i < numBalls; i++) { if(mouseX<=(balls[i].x+(balls[i].diameter)/2) && mouseX>=(balls[i].x-(balls[i].diameter)/2) && mouseY>=(balls[i].y-(balls[i].diameter)/2) && mouseY<=(balls[i].y+(balls[i].diameter)/2)){ balls[i].name(); balls[i].over=true; att[i].molt=0; } else{ balls[i].over=false; att[i].molt=0.000001; } } } void mousePressed(){ if (sw==true){ sw=false; } else{ sw=true; } }