How to get a paint / paintComponent image?

I have a quick question. How to get image created by JComponent.paint or paintComponent?

I have a JComponent that I use as a "workspace" and where I rewrote the paintComponent method with my own. The thing is, there are also children in my JComponent workspace who have their own paintComponent methods.

So, when Swing displays my workspace component, it displays the graphics of the workspace, and then its children.

However, I want to get an image that generates my component of the workspace (which includes workspace graphics and graphics for children).

How to do it?

I tried calling the paintComponent / paint-method myself using my own graphics, but I just returned the black image. Here is what I tried:

public void paintComponent(Graphics g) {

    if (bufferedImage != null) {
        g.drawImage(bufferedImage, 0, 0, this);
    }
    else {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
    }
}

public BufferedImage getImage() {

    BufferedImage hello = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics g = hello.getGraphics();
    paintComponent( g );

    return hello;
}

Any thoughts or comments are welcome! :)

+5
source share
4 answers

If you call getImage too early, your component will not be displayed yet and will have a width and height of 0. Have you made sure that you call at a fairly late date? Try printing the size of the component in stdout and see what its dimensions are.

+3
source

The problem seems to be related to how the BufferedImage was created. Using:

BufferedImage hello = bufferedImage.getSubimage(0,0, getWidth(), getHeight());

Instead, it worked. I also had to change the transition from paintComponent to painting to display them.

, . :

 BufferedImage hello = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);

? Offtopic, :)

+1

paintComponent() paint() . () . , .

. ImagePanel , , , , .

public class SomeApp extends JFrame {

    private static class ImagePanel extends JPanel {
        private BufferedImage currentImage;
        public BufferedImage getCurrentImage() {
            return currentImage;
        }
        @Override
        public void paint(Graphics g) {
            Rectangle tempBounds = g.getClipBounds();
            currentImage = new BufferedImage(tempBounds.width, tempBounds.height, BufferedImage.TYPE_INT_ARGB);
            super.paint(g);
            super.paint(currentImage.getGraphics());
        }
    }

    public SomeApp() {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setSize(800,600);
        int matrixSize = 4;
        setLayout(new BorderLayout());
        add(new JLabel("Wonderful Application"), BorderLayout.NORTH);
        final ImagePanel imgPanel = new ImagePanel();
        imgPanel.setLayout(new GridLayout(matrixSize,matrixSize));
        for(int i=1; i<=matrixSize*matrixSize; i++) {
            imgPanel.add(new JButton("A Button" + i));
        }
        add(imgPanel, BorderLayout.CENTER);
        final JPanel buttonPanel = new JPanel();
        buttonPanel.add(new JButton(new AbstractAction("get image") {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(SomeApp.this, new ImageIcon(imgPanel.getCurrentImage()));
            }

        }));
        add(buttonPanel, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        System.setProperty("swing.defaultlaf", UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SomeApp().setVisible(true);
            }
        });
    }
}

, , , , /. ComponentListener , . "" getSubimage . .

0

Changing the new BufferedImage to TYPE_INT_RGB solves this for me. I can't give an explanation, though - it's just a Swing secret.

public BufferedImage getImage() {

    BufferedImage hello = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics g = hello.getGraphics();
    paintComponent( g );

    return hello;
}
0
source

All Articles