java - Padding between characters in a string -
i have java application draw string.
now adjust white space before , after each character fit grid (each char in own square). how can accomplish that? thank answers.
public class fonttesting extends applet { private final int gridsize=20; public void paint(graphics g) { graphics2d g2d = (graphics2d)g; paintbackground(g2d); string text1 = "atzlipjnmr . . a|"; string text2 = "hhhhhhhhhh . . i|"; string text3 = "wwwwwwwwww , , 9|"; string text4 = "atatatatat |"; font font = new font("monospaced", font.plain, 20); g2d.setcolor(color.black); g2d.setfont(font); g2d.setrenderinghint(renderinghints.key_text_antialiasing, renderinghints.value_text_antialias_on); g2d.drawstring(text1, 20, 36); g2d.setrenderinghint(renderinghints.key_text_antialiasing, renderinghints.value_text_antialias_on); g2d.drawstring(text2.touppercase(), 20, 56); g2d.setrenderinghint(renderinghints.key_text_antialiasing, renderinghints.value_text_antialias_on); g2d.drawstring(text3.touppercase(), 20, 76); g2d.setrenderinghint(renderinghints.key_text_antialiasing, renderinghints.value_text_antialias_on); g2d.drawstring(text4.touppercase(), 20, 96); } public static void main(string[] args) { frame f = new frame("antialiased text sample"); f.addwindowlistener(new windowadapter(){ public void windowclosing(windowevent e) { system.exit(0); } }); f.add(new fonttesting()); f.setbackground(color.white); f.setsize(new dimension(300, 180)); f.setvisible(true); } private void paintbackground(graphics2d g2){ g2.setpaint(color.gray); (int = 0; < getsize().width; += gridsize) { shape line = new line2d.float(i, 0, i, getsize().height); g2.draw(line); } (int = 0; < getsize().height; += gridsize) { shape line = new line2d.float(0, i, getsize().width, i); g2.draw(line); } }
}
draw text strings character character grid. have @ method.
private void painttext(graphics2d g2, int row, string text){ g2.setrenderinghint(renderinghints.key_text_antialiasing, renderinghints.value_text_antialias_on); for(int = 0; < text.length(); i++){ g2.drawstring(character.tostring(text.charat(i)), * gridsize, row * gridsize); } }
you can use in our paint()
mathod this.
painttext(g2d, 1, text1); painttext(g2d, 2, text2); [...]
Comments
Post a Comment