I should just have a static final array of Chars, and draw those.
Hm… well I draw (managed) Images…
I load it, place each char into a seperate Image (52 or so) and just draw the letters I need.
public void drawText(Graphics2D g, String text, Color col, int scale, int xOff, int yOff)
{
char[] t=text.toCharArray();
g.setColor(col);
g.fillRect(xOff,yOff,6*scale*t.length,5*scale);
int c=43;
for(int i=0;i<t.length;i++)
{
switch(t[i])
{
case 'a': c= 0;break;
case 'b': c= 1;break;
case 'c': c= 2;break;
case 'd': c= 3;break;
case 'e': c= 4;break;
case 'f': c= 5;break;
case 'g': c= 6;break;
[...etc...]
}
if(c!=-1)
g.drawImage(chars[c],xOff+i*6*scale,yOff,6*scale,5*scale,null);
}
You get the idea? Obviously it’s easier if the chars match their natural order (e.g. no switch at all and just g.drawImage(chars[c&0xff]…)).
I’m worried about the allocation of hundreds of strings per second
Well that wasn’t a problem for me or even noticable (on a 4+ year old PC). However it’s “relativly” slow if you use drawString… it also produces a fair amout of garbage. Switch to images and it’s about as fast as it can get… oh and it’s cuter
If you are still worried about Strings… well just write a simiar method wich takes an int as argument, tear it apart to get the digits and… well you see? It’s about as expensive as using Strings - after all an Array is also just an object
Well just profile your code and find the real bottlenecks and spend time on speeding them up.