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.
Page Index Toggle Pages: 1
text & textfile (Read 639 times)
text & textfile
Jun 7th, 2007, 4:50pm
 
Hi,

I've started a new sketch that should open a .txt file (containing all the alphabet letters) and then should display each letters on a single line with the text() function. The background should be divised in two area one fixed defined by the background function and the other a gradient fading in and out.

Right now, i'm starting this post cause my code is pretty messy (i'm sure there's simpler and clever solutions) and i can't figure out how to make the text appear above the gradient side.


Quote:


int fontSize = 48; //size of the displaying font
int bckColor = 0;  //variable for the red gradient
int i=0;
int j=0;
boolean ASC = true; //used to make the gradient fade in and out
PFont font;

void setup(){
 size(200,1024);
 noStroke();  
 smooth();
 font = loadFont("Verdana-48.vlw");
 background(bckColor);
}

void draw(){
 String lines[] = loadStrings("alphabet.txt");
 fill(bckColor,0,0);
 rect(width/2, 0, width/2, height);
 //use that block in order to make the gradient fade in & out
 //The boolean sets the action to perform
 if (ASC == true) {
   bckColor+=10;
 }
 else if (ASC == false) {
   bckColor-=10;
 }
 //Assign the boolean state according to the limits
 if (bckColor == 250)
   {ASC = false;
 }
 else if (bckColor == 0){
   ASC = true;
 }
 
 //font settings
 textFont(font, fontSize);
 textAlign(CENTER);
 fill(250);
 
 //display each letter on a new line
 //used a while in order to display it
 //only once
 while (j<lines.length){
 text(lines[j]+"\n",width/2,(j*35));
 j++;
 }
}




Any help appreciated.
Cheers.
Re: text & textfile
Reply #1 - Jun 7th, 2007, 9:00pm
 
the while loop in draw wasn't resetting the counter j, so the text was only getting drawn once; i replaced it with a for loop for convenience. i also shuffled around and streamlined some stuff.
have a look:

Code:

int fontSize = 48, bckColor;
PFont font;
String lines[];
int inverter = -1, fader = -2;

void setup(){
size(200, 1024);
background(0);
noStroke();
smooth();
font = loadFont("Verdana-48.vlw");
textFont(font, fontSize);
textAlign(CENTER);
lines = loadStrings("alphabet.txt");
}

void draw(){
fill(bckColor, 0, 0);
rect(width/2, 0, width/2, height);

if (bckColor>=250 || bckColor<=0){
fader *= inverter;
}
bckColor += fader;

//display each letter on a new line
fill(250);
for (int i=0; i<lines.length; i++){
text(lines[i]+"\n", width/2, (i*35));
}
}
Page Index Toggle Pages: 1