Hi! there,
I am trying to print a sequence of number on canvas using text() and for loop but those numbers are overwriting on one another. actually I want to print Fibonacci sequence on the canvas and the number of steps entered by the users. please anyone can help me with that…
It Would be helpful if we had some of your Code, But it is relatively easy. If you want the numbers to be displayed one under the other, then just put at the yPosition inside your text() i * textHeight. (Or whatever your Loop Variable is called. )
here is my code for that.
/*---------------------------------------*/
import javax.swing.JOptionPane;
size(500, 500);
int steps = int(JOptionPane.showInputDialog(
" how many steps do yo want ? "));
int pr=0;//first number
int sr=1;//second number
for (int i=0; i<steps; i++) {
int next;
next=pr+sr;//sum of first and second number
pr=sr;
sr=next;
println(pr+" ");
for (int textX=100; textX<500; textX+=100) {
for (int textY=100; textY<500; textY+=100) {
if (steps<5) {
if (steps<2) {
textSize(20);
fill(255);
text(pr+" ", 100, 100);
} else if (steps<3) {
textSize(20);
fill(255);
text(pr +" ", 200, 100);
}
}
}
}
}
1 Like
possibly first ignore “the JOB”
and just play with the positioning of
text(“what”,x,y);
int my_windX=500, my_windY=500;
int posX, posY, iperline=6, imax =50;
int x0=10, y0=15, dx=80, dy=30;
void settings() {
size(my_windX, my_windY);
}
void setup() {
background(0, 200, 200);
fill(200, 0, 0);
textSize(14);
noLoop();
//here could recalculate how many number you can print in one line
// x0 + iperline*dx is the space needed, my_windX is the space you have
iperline=floor((my_windX-x0)/dx);
println("iperline: "+iperline);
}
void draw() {
for (int i=0; i<imax; i++) {
posX=x0 + dx*(i%iperline);
posY=y0 + dy*(floor(i/iperline));
text("i= "+nf(i, 3), posX, posY);
}
}
1 Like
Or if you just want the values to be in a line :
for (int i = 0; i < steps; i++) {
yourNumber = //the number you want displayed. Might be an array
String spacing = ", ";
String number = str(yourNumber) + spacing;
text(number, i * textWidth(number), y);
// shows the numbers one after the other with Something inbetween. Example : spacing = ", " then the numbers will Show like 1, 2, 3, and so on.
}
Or one under the other :
for (int i = 0; i < steps; i++) {
yourNumber = //the number you want displayed. Might be an array
int offset = 5; // distance of Pixels to the underlying number
text(number, x, i * textHeight(str(number)) + offset);
// shows the numbers one under the other with some space inbetween. Can‘t give you an example here, since the Text space here is fixed.
}
2 Likes
Thank you for helping… it was so helpful.