We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › How to switch from a loaded file to another one.
Page Index Toggle Pages: 1
How to switch from a loaded file to another one. (Read 1052 times)
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;
 }  
}








Re: How to switch from a loaded file to another one.
Reply #1 - Nov 2nd, 2009, 4:42am
 
if you have only 2 files you can do it like this :

Code:
String[] lines;
String[] lines2;
boolean file = true;

void setup() {
size(200, 200);
lines = loadStrings("list.txt");
}

void draw(){
for (int i=0; i < lines.length; i++) {
println(lines[i]);
}
}

void mousePressed() { // Click to add a line segment

if(file){
lines = loadStrings("list.txt");
}

if(!file){
lines = loadStrings("list2.txt");
}

file=!file;

}
Re: How to switch from a loaded file to another one.
Reply #2 - Nov 2nd, 2009, 5:00am
 
I see, the problem is that the "for" is situated in the "setup". So i guess that it loads once the TXT and stop, even if after i activate the switch it doesn't reload it. I tried to move the "for" in the "draw" but like that it crashes. I'm not that good  and i can't really figure out how to solve this issue.

Thanks

Lorenzo

Re: How to switch from a loaded file to another one.
Reply #3 - Nov 2nd, 2009, 5:15am
 
ah right, i didnt read your whole code, i the problem is when passing the values to your ball class. that happens in setup and it not redone when changing the file.

what for loop are you talking about ...

Code:
 for(int i=0; i < lines.length; i++){
String[] pieces = split(lines[i], ';');
if (pieces.length==2){
records[recordCount]= new Record(pieces);
recordCount++;

}


this one? what exactly happens there. What does your csv looks like?

Re: How to switch from a loaded file to another one.
Reply #4 - Nov 2nd, 2009, 6:27am
 
Yeah, that's the loop.
the CSV is something like this:

FERRERO P & C SPA ALBA CUNEO;15299
WIND TELECOMUNIC.SPA ROMA;14153
VOLKSWAGEN GROUP IT.SPA VERONA;10613


mmm... as you can probably tell i took the code somewhere else but i can't fit it for what i need.
Thanks for the help
Re: How to switch from a loaded file to another one.
Reply #5 - Nov 2nd, 2009, 7:01am
 
i am having a hard time understanding what you actually did in this part...

but anyway. try to copy it to void mousepressed after you changed the file... dont move it. so it reloads everytime you press the button. but you still need it in setup to run the first time.
Re: How to switch from a loaded file to another one.
Reply #6 - Nov 2nd, 2009, 12:50pm
 
I know that I'm annoying but i can't get it to work! What i originally wanted to do was to parse the CSV file and separate the names from the value as you can see in the above portion of the CSV. I took the code from the Ben Fry, Casey Reas Processing book. Plus i tried to add the switch but i can't get it to work. I copied the for loop in the void mousePressed but it gives me the error:

Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 200

If you know how to solve the error it would be great!
thanks again
Lorenzo
Re: How to switch from a loaded file to another one.
Reply #7 - Nov 2nd, 2009, 1:36pm
 
i managed!!

thanks again
Code:

void mousePressed(){
recordCount=0;
if(file){
lines = loadStrings("2008.csv");

}

if(!file){
lines = loadStrings("2009.csv");
//print(file);
}
file=!file;
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++;
}
}
}
Re: How to switch from a loaded file to another one.
Reply #8 - Nov 2nd, 2009, 1:42pm
 
great, thats what i ment Smiley
Page Index Toggle Pages: 1