How do I add background image in JTextPane?

The example below demonstrate how to create a custom JTextPane component that have a background image.

package org.kodejava.swing;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Objects;

public class TextPaneWithBackgroundImage extends JFrame {
    public TextPaneWithBackgroundImage() {
        initUI();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(
                () -> new TextPaneWithBackgroundImage().setVisible(true));
    }

    private void initUI() {
        setSize(500, 500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.LEFT));

        String imageFile = "/logo.png";
        CustomTextPane textPane = new CustomTextPane(imageFile);

        JScrollPane scrollPane = new JScrollPane(textPane);
        scrollPane.setPreferredSize(new Dimension(getWidth(), getHeight()));
        getContentPane().add(scrollPane);
        pack();
    }
}

/**
 * A custom text pane that have a background image. To customize the
 * JTextPane we override the paintComponent(Graphics g) method. We have
 * to draw an image before we call the super class paintComponent method.
 */
class CustomTextPane extends JTextPane {
    private final String imageFile;

    /**
     * Constructor of custom text pane.
     *
     * @param imageFile image file name for the text pane background.
     */
    CustomTextPane(String imageFile) {
        super();
        // To be able to draw the background image the component must
        // not be opaque.
        setOpaque(false);
        setForeground(Color.BLUE);

        this.imageFile = imageFile;
    }

    @Override
    protected void paintComponent(Graphics g) {
        try {
            // Load an image for the background image of out JTextPane.
            BufferedImage image = ImageIO.read(Objects.requireNonNull(
                    getClass().getResourceAsStream(imageFile)));
            g.drawImage(image, 0, 0, (int) getSize().getWidth(),
                    (int) getSize().getHeight(), this);
        } catch (IOException e) {
            e.printStackTrace();
        }

        super.paintComponent(g);
    }
}

JTextPane background image

How do I create a screen capture using Robot class?

In this example you’ll see how to create a screen capture / screenshot and save it as an image file such a PNG image. Some classes are use in this program including the java.awt.Robot, java.awt.image.BufferedImage and javax.imageio.ImageIO.

package org.kodejava.awt;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.io.File;
import java.io.IOException;

public class ScreenCapture {
    public static void main(String[] args) {
        try {
            Robot robot = new Robot();

            // Capture screen from the top left in 200 by 200 pixel size.
            BufferedImage bufferedImage = robot.createScreenCapture(
                    new Rectangle(new Dimension(200, 200)));

            // The captured image will the written into a file called
            // screenshot.png
            File imageFile = new File("screenshot.png");
            ImageIO.write(bufferedImage, "png", imageFile);
        } catch (AWTException | IOException e) {
            e.printStackTrace();
        }
    }
}

How do I read image file?

Here you see a code sample to read an image file. This code will work either the image file is located in a file folder or inside a jar file. You can use javax.imageio.ImageIO class to read the image file.

package org.kodejava.awt;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

public class ReadingImage {
    public static void main(String[] args) {
        ReadingImage demo = new ReadingImage();
        demo.getImage();
    }

    public void getImage() {
        try {
            InputStream is = getClass().getResourceAsStream("/kodejava.png");
            BufferedImage image = ImageIO.read(is);

            // Do something with the image.
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}