Board index » jbuilder » Drawing centered text within a square

Drawing centered text within a square


2003-12-02 01:52:57 PM
jbuilder4
Hi all,
I'm a Java newbie. I'm trying to draw a single character, centered
within a square. My code is based on the Java book I'm using (Core
Java by Horstmann and Cornell) and is functional, but seems wordy for
something this simple. Is there a simpler way? Here's the code:
(sqX and sqY are the x and y coordinates of the upper left corner of
my square. aFont is my font object, created in the class
constructor.)
private void drawCenteredChar( Graphics2D g2, int sqSize,
int sqX, int sqY, char c ) {
char[] ch = new char[1];
ch[0] = c;
String str = new String( ch );
g2.setFont(aFont);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = aFont.getStringBounds( str, context );
double fontX = sqX + (SqSize - bounds.getWidth()) / 2;
double fontY = sqY + (SqSize - bounds.getHeight()) / 2;
double baseY = fontY - bounds.getY();
g2.drawString( str, (int)fontX, (int)baseY );
}
In particular, is there a better way than creating a character array
to get a String containing just my one character?
 
 

Re:Drawing centered text within a square

Hal Brooks wrote:
Quote
[...]
String str = new String( ch );
[...]
In particular, is there a better way than creating a
character array to get a String containing just my one
character?
String str = String.valueOf(c);
Is that any better? It just hides code
similar to that used by the book's authors.
See String's valueOf(char c) method:
public static String valueOf(char c) {
char data[] = {c};
return new String(0, 1, data);
}
The authors' code might be slightly faster.
--
Paul Furbacher (TeamB)
Save time, search the archives:
www.borland.com/newsgroups/ngsearch.html
Is it in Joi Ellis's Faq-O-Matic?
www.visi.com/~gyles19/fom-serve/cache/1.html
Finally, please send responses to the newsgroup only.
That means, do not send email directly to me.
Thank you.
 

Re:Drawing centered text within a square

On Tue, 02 Dec 2003 08:10:18 -0500, Paul Furbacher
< XXXX@XXXXX.COM >wrote:
Quote
>In particular, is there a better way than creating a
>character array to get a String containing just my one
>character?

String str = String.valueOf(c);

Is that any better? It just hides code
similar to that used by the book's authors.
See String's valueOf(char c) method:

public static String valueOf(char c) {
char data[] = {c};
return new String(0, 1, data);
}

The authors' code might be slightly faster.
Thank you Paul!
 

{smallsort}

Re:Drawing centered text within a square

"Paul Furbacher" < XXXX@XXXXX.COM >wrote in message
Quote
The authors' code might be slightly faster.
I suspect it won't be. The primary bottleneck will probably be object
creation with method calls as a possible secondary contributor. Let's
look at the two options.
1. Original code
char[] ch = new char[1];
ch[0] = c;
String str = new String( ch );
This involves creation of 3 objects and 2 method calls. Objects: one
for the character array in line 1, one for the String in line 3, and
another for the copy of the array made by the String constructor.
Methods: one for the String constructor and another for the call to
System.arraycopy made by the String constructor.
2. Paul's code
String str = String.valueOf(c);
This involves the creation of only 2 objects and still only 2 method
calls. Objects: one for the character array in line 1 of the valueOf
method and another for the String in line 2 of the valueOf method.
valueOf uses a private String constructor that does not copy the array,
so the third object has been eliminated. Methods: one for the valueOf
method and another for the call to the String constructor made by
valueOf. The private String constructor does not copy the array, so the
call to System.arraycopy has been eliminated.
In short, by using String.valueOf you've made the code's intent clearer
*and* probably improved its speed. Well done! :-)
--
Gillmer J. Derge (TeamB)
 

Re:Drawing centered text within a square

On Wed, 3 Dec 2003 11:12:06 -0500, "Gillmer J. Derge \(TeamB\)"
< XXXX@XXXXX.COM >wrote:
Quote
>The authors' code might be slightly faster.

I suspect it won't be. The primary bottleneck will probably be object
creation with method calls as a possible secondary contributor.
Thank you Gillmer!
For the record, I didn't find a specific example in "Core Java" of
creating a String from a single character, so I cobbled together
pieces from different sections of the book. Any inefficiencies
therein are therefore my fault and not the authors'.