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 › Center Text on PShape
Page Index Toggle Pages: 1
Center Text on PShape (Read 745 times)
Center Text on PShape
Nov 26th, 2009, 8:11am
 
One of the examples I used to begin a project was the Presidential Map example.  A very easy to implement SVG map with coloring.  The problem I ran into was the ability to label the names of the states on the map.

I can't seem to find or think of a clever way to get the "center" of a drawn shape in order to place text correctly within the boundaries of each state as it is drawn.

Does anyone have a suggestion about how this might be done?  The name of each state is states[i] from the code snippet:

void statesColoring(String[] states, int c){
 for (int i = 0; i < states.length; ++i) {
   PShape state = usa.getChild(states[i]);
   // Disable the colors found in the SVG file
   state.disableStyle();
   // Set our own coloring
   fill(c);
   noStroke();
   // Draw a single state
   shape(state, width/2-usa.width/2, 170);
   // Label the state
   fill(color(0,0,0));
   textFont(stateFont,10);
   text(states[i], state.width/2, state.height/2);
 }

As you can imagine, state.width/2 and state.height/2 is not to correct solution, the right implementation would seem to be to either be able to ask a shape what its corner (or center) is or to ask the graphic context where the last pixel it drew was located.  Any advice/suggestions would be most welcome!  Thanks.
Re: Center Text on PShape
Reply #1 - Nov 26th, 2009, 9:34am
 
Good question, i am really not sure if you can get the position of a PShape Child. Probably not. Couldnt find any information about it.
So if not its probably easier to load the Map into illistrator. Create anothere SVG where you simply set the Text and then overlay it.
Sure doesnt work of you want to change the text but probably doesnt happen that often with states Smiley
Re: Center Text on PShape
Reply #2 - Nov 26th, 2009, 10:06am
 
It looks harder than you might imagine.  Here's an interesting article on the subject...

It looks to me like something you want to calculate only once when a program runs and then store as variables.
Re: Center Text on PShape
Reply #3 - Nov 26th, 2009, 10:12am
 
ohh good article. looks intersting. If you come up with a working code, post it here. could be useful for others.
Page Index Toggle Pages: 1