0

I have a string which is displayed in my program, but I'm wondering how I would convert the output of the string into an image identical to the original string. No idea if this can be done.

I would like the output to be exactly what is is the JTextArea. Is this possible and if so what should I look into?

Anderson Green
  • 30,230
  • 67
  • 195
  • 328
Tim
  • 1,056
  • 4
  • 17
  • 34
  • *"I would like the output to be exactly what is is the JTextArea."* Using what PLAF? At what width/height? Including the text selection and cursor? Most importantly.. why? – Andrew Thompson Aug 10 '12 at 02:10

2 Answers2

5

assylias beat me to it, but seen as I was so close I thought I'd post it anyway

public class TestFrame extends JFrame {

    private JTextArea text;

    public TestFrame() {

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setTitle("Text");

        setLayout(new BorderLayout());
        text = new JTextArea();
        add(new JScrollPane(text));

        JButton btnPrint = new JButton("Print");
        add(btnPrint, BorderLayout.SOUTH);

        btnPrint.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                BufferedImage img = new BufferedImage(text.getWidth(), text.getHeight(), BufferedImage.TYPE_INT_RGB);
                Graphics2D g2d = img.createGraphics();
                text.printAll(g2d);
                g2d.dispose();

                try {
                    ImageIO.write(img, "png", new File("StringToGraphics.png"));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            }
        });

        text.setWrapStyleWord(true);
        text.setColumns(15);
        text.setLineWrap(true);
        text.setText("I have a string which is displayed in my program, but I'm wondering how I would convert the output of the string into an image identical to the original string. No idea if this can be done.\n\nI would like the output to be exactly what is is the JTextArea. Is this possible and if so what should I look into?");

        setSize(200, 300);

        setLocationRelativeTo(null);
        setVisible(true);

    }

}

From this

The Frmae

to this

The result

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • +1 See also [ComponentImageCapture](http://stackoverflow.com/questions/5853879/java-swing-obtain-image-of-jframe/5853992#5853992). – Andrew Thompson Aug 10 '12 at 02:11
  • Any way to make the background transparent? – Cardinal System Feb 17 '23 at 18:19
  • @CardinalSystem That would mostly likely require you to make the text area transparent (ie setOpaque), but i wouldn’t do that on a live component which is displayed in the screen, which makes more complicated – MadProgrammer Feb 17 '23 at 22:46
3

You can use ScreenImage to create an image from a component using the createImage method. In summary, here is what it does under the hood (using a JLabel):

public static void main(String args[]) throws AWTException, IOException {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new JFrame("test");
            JLabel text = new JLabel("text");
            frame.add(text);
            frame.setPreferredSize(new Dimension(100, 100));
            frame.pack();
            frame.setVisible(true);

            BufferedImage img = getImage(text);
            try {
                ImageIO.write(img, "png", new File("C:/temp/img.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    });
}

private static BufferedImage getImage(JComponent c) {
    Rectangle region = c.getBounds();
    BufferedImage image = new BufferedImage(region.width, region.height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = image.createGraphics();
    g2d.translate(-region.x, -region.y);
    g2d.setColor(c.getBackground() );
    g2d.fillRect(region.x, region.y, region.width, region.height);
    c.paint(g2d);
    g2d.dispose();
    return image;
}
assylias
  • 321,522
  • 82
  • 660
  • 783