Search code examples
javaswingawtdrawstring

centering individual letters in a vertical string in java


Okay so I have a vertical string but when it contains I's or L's they are offset from the rest of the string because of how they are typographically written they are in a sense left justified in the box they are drawn in unlike the rest are drawn centered. I am wondering how to make those letters fall into line with the others. Also of importance is that these are individual drawstring calls. I tried using AffineTransform but it mashes all the letters together. this is the code i use to loop through the string and write each character.

for(int i =0; i<team.length();i++) 
{
    gg.drawString(Character.toString(team.charAt(i)), 100, ypos-fm.getDescent());
    ypos+=40;
}

The string im using is BOLIVAR if you'd like to test it. Thanks in advance!


Solution

  • You could try centering the text around the character width

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TestVerticalTexr {
    
        public static void main(String[] args) {
            new TestVerticalTexr();
        }
    
        public TestVerticalTexr() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                String team = "BOLIVAR";
                FontMetrics fm = g2d.getFontMetrics();
                int ypos = fm.getHeight();
                for (int i = 0; i < team.length(); i++) {
                    int x = 100 - (fm.charWidth(team.charAt(i)) / 2);
                    g2d.drawString(Character.toString(team.charAt(i)), x, ypos);
                    ypos += fm.getHeight();
                }
                g2d.dispose();
            }
        }
    }