# Printing a sequence of number on the canvas using text()

**URL:** https://discourse.processing.org/t/printing-a-sequence-of-number-on-the-canvas-using-text/4936
**Category:** Coding Questions
**Tags:** homework
**Created:** [October 27, 2018, 10:13pm UTC](https://discourse.processing.org/t/printing-a-sequence-of-number-on-the-canvas-using-text/4936 "2018-10-27T22:13:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Dee-3\_123](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@Dee-3\_123](https://discourse.processing.org/u/Dee-3_123)
#### Post date: [October 27, 2018, 10:13pm UTC](https://discourse.processing.org/t/printing-a-sequence-of-number-on-the-canvas-using-text/4936/1 "2018-10-27T22:13:00Z")

</div>

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…

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [October 27, 2018, 10:29pm UTC](https://discourse.processing.org/t/printing-a-sequence-of-number-on-the-canvas-using-text/4936/2 "2018-10-27T22:29:36Z")

</div>

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. )

---

<div class="post-metadata">

### Author: ![Dee-3\_123](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@Dee-3\_123](https://discourse.processing.org/u/Dee-3_123)
#### Post date: [October 27, 2018, 10:49pm UTC](https://discourse.processing.org/t/printing-a-sequence-of-number-on-the-canvas-using-text/4936/3 "2018-10-27T22:49:17Z")

</div>

here is my code for that.

```auto
/*---------------------------------------*/

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);
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [October 28, 2018, 2:34am UTC](https://discourse.processing.org/t/printing-a-sequence-of-number-on-the-canvas-using-text/4936/4 "2018-10-28T02:34:03Z")

</div>

possibly first ignore “the JOB”  
and just play with the positioning of

> text(“what”,x,y);

```auto
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);
  }
}

```

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [October 28, 2018, 5:02am UTC](https://discourse.processing.org/t/printing-a-sequence-of-number-on-the-canvas-using-text/4936/5 "2018-10-28T05:02:17Z")

</div>

Or if you just want the values to be in a line :

```auto
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 :

```auto
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. 
}

```

---

<div class="post-metadata">

### Author: ![Dee-3\_123](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@Dee-3\_123](https://discourse.processing.org/u/Dee-3_123)
#### Post date: [October 30, 2018, 4:41pm UTC](https://discourse.processing.org/t/printing-a-sequence-of-number-on-the-canvas-using-text/4936/6 "2018-10-30T16:41:09Z")

</div>

Thank you for helping… it was so helpful.
