2

I'm working on a 3D scene in Java using the Processing API. It's a force-directed graph layout algorithm (although that isn't too important). I've got all the graph drawing done -- the nodes, edges, layout, etc. are looking good. Each node on the graph has a label, though, and I'd like to be able to display said label as text next to the node. I've tried working with the text() function, but so far it seems like my code just doesn't work. I see no text anywhere in the scene.

My code looks like the following:

pushMatrix();
translate(width/2, height/2, 0); // put 0,0,0 at the center of the screen
text("foo!", 20, 20, 20);
popMatrix();

And I don't see anything. Just the graph. So what am I missing?

Peter
  • 1,349
  • 11
  • 21

2 Answers2

0

Everything is fine with the tiny bit of code you displayed. You can see a modified version running here:

void setup() {
    size(400,400,P3D);    
    textSize(20);
} 

void draw() {  
    background(0);
    translate(width * .5, height*.5,0);
    rotateY(map(mouseX,0,width,-PI,PI));
    rotateX(map(mouseY,0,height,-PI,PI));
    pushMatrix();
    text("foo!", -20, 0, 20);
    popMatrix();
}

There might be something else along the way. Care to share more information ?

George Profenza
  • 50,687
  • 19
  • 144
  • 218
  • It turns out I'd tried to create a font using the createFont() method that didn't exist -- it was failing silently and just not drawing any text, I think. Thanks! – Peter Apr 19 '12 at 13:52
0

The fill color controls the text color, and defaults to white. So if you are painting white text on a white background, it won't show up. Add fill(0); before you draw text.

Also remember that shapes drawn after you send your text to the screen may over-write your text. The last 'hidden' statement in draw is to paint the screen.

Here's an example of drawing a series of vertical lines (in 2D) with a row of labels at the top:

   int startX;

   void setup() {
      size(400,400);
      textSize(12);
      startX = width/10; 
    }

    void draw() {
      background(255);
      int curX = startX;
      fill(0);            // Set the text color
      while (curX < width)
      {
        line(curX, 30, curX, height-10);
        text(curX, curX-(curX/20), 20);
        curX += width/10;
      }
    } // end draw