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 & HelpPrograms › PGraphics text problem
Page Index Toggle Pages: 1
PGraphics text problem (Read 384 times)
PGraphics text problem
Dec 27th, 2008, 5:42pm
 
Hey,

Been trying to squash the bug that's bothering me for a while now and can't figure out the problem so I have to turn to you..
I have two classes (A, B), completely unrelated. They both draw text, with different fonts and sizes.
I use a PGraphics object to export to tiff, just redraw onto the object what is normally drawen. The problem is that when I have an instance of class B, A doesnt save correctly. The textWidth and textAscent properties change for the class A. It's strange because I don't even have to draw it on the PGraphics object, just having an instance of it, and having it draw some text on the canvas alters the 2 properties of A, that I need to underline the text.
When drawing to the PGraphics object I use the normal procedure, begindraw etc.. and I always initialize the fontsize, font etc to the appropriate one before any drawing to the PGraphics object for class A.

Anyone know why this happens?
Re: PGraphics text problem
Reply #1 - Dec 27th, 2008, 8:16pm
 
Excuse me, but I have hard time understanding the problem.
Perhaps you can make a simplified version of your code, showing the issue with minimal code, so we can see the issue and experiment with solutions.
Re: PGraphics text problem
Reply #2 - Dec 27th, 2008, 9:42pm
 
This is what I refered to as class B above. Draws a rectangle if it has no text, otherwise it displays text. In the end, the problem only happens when it does have some text as that's the only time it calls text functions..

class TextBox extends Window {
 String infoText;

 void display() {
   if (infoText.equals("")) {
     // the box is empty - draw a border to give a clue to its position
     noFill();
     stroke(100);
     w = h;
     rect(x, y, w, h);
   }
   else {
     textFont(infoFont);
     textSize(fontSize);
     fill(160);
     text(infoText, x, y, w, h);
     textFont(font);
   }
 }
}

Here I save as an image:

IMG2Save = createGraphics(ww, wh, JAVA2D);
IMG2Save.beginDraw();
IMG2Save.colorMode(HSB, 360, 100, 100);
IMG2Save.background(100, 0, 100);
IMG2Save.strokeWeight(1);
IMG2Save.smooth();
IMG2Save.stroke(strokeColor);
IMG2Save.textAlign(CENTER, CENTER);
IMG2Save.rectMode(CENTER);
IMG2Save.imageMode(CENTER);        
       
// export connections
for(int i = 0; i < numCons; i++)
 connections[i].exportIMG();
// export nodes
IMG2Save.textFont(font);                
for(int i = 0; i < num; i++)
 nodes[i].exportIMG();
IMG2Save.endDraw();        
IMG2Save.save("filename");

Each node in the exportIMG()  function amongst other things does
 
IMG2Save.textSize(currentFontSize);
IMG2Save.fill(0);    
IMG2Save.text(string.getString(), x, y);
if (underlined)
 IMG2Save.line(x - textWidth(text)/2, y + textAscent()/1.3, x + textWidth(text)/2, y + textAscent()/1.3);

So the class A is the node..
So the problem is when I try to export when there is a TextBox instance with text being displayed. I don't see how it can influence IMG2Save..
For example, for textWidth(text) and textAscent() of the node, without and with a TextBox object on export I get
56 11.2
80 16
The fontsize of the text in TextBox is 10 and 14 for the node..
Sorry for not being clearer last time, hope it's better this time.. A lot longer read thought :)
Re: PGraphics text problem
Reply #3 - Dec 27th, 2008, 10:38pm
 
I see. I think the line() call should be:
IMG2Save.line(x - IMG2Save.textWidth(text)/2, y + IMG2Save.textAscent()/1.3, x + IMG2Save.textWidth(text)/2, y + IMG2Save.textAscent()/1.3);
because otherwise you get the display's font, set by TextBox class, indeed.
Re: PGraphics text problem
Reply #4 - Dec 27th, 2008, 11:54pm
 
Yep, that was the mistake. Thanks a lot for noticing! I doubt I would've seen it any time soon myself..
Merci beaucoup!
Page Index Toggle Pages: 1