0

Possible Duplicate:
How to resize text in java

Is it possible to stretch text in java horizontally. I know it should be there but I am unable to figure it out. Font Size affects both heigth and width of text. I tried with FontMetrics but it only gives me the width of the text. But i need to change only the text width so that it should look as if it is being stretched.

If anyone is aware of doing this, please let me know. Thanks in Advance.

Community
  • 1
  • 1
user001
  • 991
  • 6
  • 16
  • 34

1 Answers1

4

There are probably other ways to achieve this same result, like converting the text to a shape and using a AffineTransformation, but this is the one that came to hand...

enter image description here

public class StretchText {

    public static void main(String[] args) {
        new StretchText();
    }

    public StretchText() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                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 {

        private BufferedImage imgTet;

        public TestPane() {
            Font font = UIManager.getFont("Label.font");
            FontMetrics fm = getFontMetrics(font);
            String text = "This is a test";
            int width = fm.stringWidth(text);
            int height = fm.getHeight();
            imgTet = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = imgTet.createGraphics();
            g2d.setColor(Color.BLACK);
            g2d.drawString(text, 0, fm.getAscent());
            g2d.dispose();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.drawImage(imgTet, 0, 0, getWidth(), imgTet.getHeight(), this);
            g2d.dispose();
        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • But is it a good idea to convert text to image? I feel if i can give any width attribute to text or something similar then it would be great – user001 Feb 03 '13 at 08:59
  • There's no solution that is going to give you back text that is stretched, without it first been converted to either an image or a shape, that I can think of – MadProgrammer Feb 03 '13 at 09:03
  • @GuillaumePolet That was one of the ideas I had, but I don't have the time to explore it. If you have an example, let me know so I can up vote it – MadProgrammer Feb 03 '13 at 09:54
  • 1
    What, no `KEY_TEXT_ANTIALIASING`?!? Good god, what is the world coming to? Next children will be disrespecting their parents and society will collapse. See [this answer](http://stackoverflow.com/a/13440543/418556) for the restoration of all things 'good and nice'. +1 (What.. you didn't think I was serious in that rant, ..did you?) – Andrew Thompson Feb 03 '13 at 11:09
  • @AndrewThompson In my playing with the KEY_TEXT_ANTIALIASING, it's difficult to get the right value, but I could just be missing something ;) - ps that's a really good set of examples related to the same problem, cheers – MadProgrammer Feb 03 '13 at 19:23