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 & HelpSyntax Questions › Processing + Java Question
Page Index Toggle Pages: 1
Processing + Java Question (Read 603 times)
Processing + Java Question
Feb 4th, 2010, 9:43pm
 
Hello, first post here.
I'm trying to port my processing project into Eclipse and combining it with java.awt. Since PApplet is extended from the Applet class and Applet + awt go well together, how is the draw() function in processing relates to the paint() function in awt?

For example I'd like to use the g.drawString() from awt within my draw() function. I know processing has it's own PFont, but I'd like to use the awt functions instead.

This is what I'm trying to add to draw():
Code:
public void paint(Graphics g) {
//font centering vars
g.setFont(font);
FontMetrics fm=g.getFontMetrics(font);
java.awt.geom.Rectangle2D rect[] = new java.awt.geom.Rectangle2D[line.length];

//draw the text
g.setColor(Color.black);
for (int i=0; i<line.length; i++) {
rect[i]=fm.getStringBounds(line[i],g);
int textWidth=(int)(rect[i].getWidth());
if (i==line.length-1) {
g.drawString(line[i], (width-textWidth)/2, padding+(fSize*(i+1)));
} else {
g.drawString(line[i], (width-textWidth)/2, padding+(fSize*i));
}
}
}

Any help would be appreciated thanks!
Re: Processing + Java Question
Reply #1 - Feb 5th, 2010, 2:06am
 
I did that in the past, to get glyph information from TTF fonts.
There might be problems (conflicts of threads...) but basically drawing on sketch's surface with AWT functions is possible.
The idea is to have a Graphics2D g2; variable, then initialize it with Processing's g variable: g2 = ((PGraphicsJava2D) g).g2;
We can then do stuff like:
Code:
AffineTransform transform = new AffineTransform();
transform.translate(40, 320);
transform.scale(SCALE, SCALE);
Shape vbs = glyphVector.getGlyphVisualBounds(i);
g2.draw(transform.createTransformedShape(vbs));

Rectangle r = glyphVector.getGlyphPixelBounds(i, null, 0.0f, 0.0f);
g2.draw(transform.createTransformedShape(r));

and so on.
Re: Processing + Java Question
Reply #2 - Feb 5th, 2010, 2:49pm
 
Awesome, this is exactly what I was looking for!
Thanks PhiLho.
Page Index Toggle Pages: 1