开发者

How do I get rid of the border made by capturing a jframe image to file?

开发者 https://www.devze.com 2023-03-12 16:17 出处:网络
So I made an application that creates a graphical timeline from a csv file. I have that part finished now I just need help getting the image \"pretty\". When capturing the image the border from the JF

So I made an application that creates a graphical timeline from a csv file. I have that part finished now I just need help getting the image "pretty". When capturing the image the border from the JFrame is captured too! How do I ma开发者_如何学JAVAke it such that the border is not captured? Or how do I get rid of it and keep the image size?

How do I get rid of the border made by capturing a jframe image to file?


Here is a simple example. Just to clarify your needs. Based on solution of How to remove the title bar from a JFrame Screenshot?.

The following program takes screenshot of its JFrame and writes it to the file.

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;

/* Writes self screenshot on Screenshot button click. */
public class ScreenshotFrame extends JFrame {

    public ScreenshotFrame () {
        initComponents();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ScreenshotFrame().setVisible(true);
            }
        });
    }

    private void initComponents() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        JButton screenshotButton = new JButton();

        screenshotButton.setText("Screenshot");
        screenshotButton.setToolTipText("Take my screenshot.");
        screenshotButton.addActionListener(new ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                writeImageToFile(getScreenshot());
            }
        });        

        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(screenshotButton);

        pack();
    }

    /* Modified method from pointed solution. */
    private BufferedImage getScreenshot() {
        Dimension dim = this.getContentPane().getSize();
        BufferedImage image =
                new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_RGB);
        this.getContentPane().paint(image.getGraphics());
        return image;
    }

    /* Write image to png file in current dir.*/
    private void writeImageToFile(BufferedImage image) {
        try {
            File file = new File("JFrameScreenshot.png");
            file.createNewFile();
            ImageIO.write(image, "png", file);
        } catch (IOException ex) {/*do smth*/ }
    }
}

Does this what you want, if_zero_equals_one? If not, maybe you could add some code to your question, that tries to do what you want.

P.S. Thanks to Darien and camickr, who pointed where to find the source for that example. Maybe this should be a comment. But it's clearer with such formatting.


BufferedImage image = (BufferedImage)createImage(getContentPane().getSize().width, getContentPane().getSize().height);
getContentPane().paint(image.getGraphics());

This is what I believe I was looking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号