4

I have a java application where I draw a String.

enter image description here

Now I would like to adjust the white space before and after each character that they would fit to the grid (each char in their own square). How can I accomplish that? Thank you for your 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);
    for (int i = 0; i < getSize().width; i += gridSize) {
      Shape line = new Line2D.Float(i, 0, i, getSize().height);
      g2.draw(line);
    }

    for (int i = 0; i < getSize().height; i += gridSize) {
      Shape line = new Line2D.Float(0, i, getSize().width, i);
      g2.draw(line);
    }
}

}

1
  • 4
    could you share us your code? Commented Nov 10, 2016 at 9:05

1 Answer 1

2

Draw your text Strings character by character into the grid. Have a look at this method.

private void paintText(Graphics2D g2, int row, String text){
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    for(int i = 0; i < text.length(); i++){
        g2.drawString(Character.toString(text.charAt(i)), i * gridSize, row * gridSize);   
    }  
}

You can use it in our paint() mathod like this.

paintText(g2d, 1, text1);
paintText(g2d, 2, text2);
[...]
Sign up to request clarification or add additional context in comments.

2 Comments

This worked perfectly thank you. But I encountered another problem. I will be drawing a String which I will get from a .txt file. The problem is how will I know when do I have to go to next row if txt file will have more than one row?
If you read all text from file into a String you could split it by newline character \n and write something like this: int i = 1; for(String line : text.split("\n")) paintText(g2d, i++, line);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.