如何消除通过将 jframe 图像捕获到文件而产生的边框?

发布于 2024-11-15 01:45:48 字数 193 浏览 3 评论 0原文

因此,我制作了一个应用程序,可以从 csv 文件创建图形时间线。我现在已经完成了那部分,我只需要帮助让图像变得“漂亮”。捕获图像时,JFrame 的边框也会被捕获! 如何使边界不被捕获?或者我如何摆脱它并保持图像大小? 在此处输入图像描述

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 make it such that the border is not captured? Or how do I get rid of it and keep the image size?
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哎呦我呸! 2024-11-22 01:45:48

这是一个简单的例子。只是为了澄清您的需求。基于解决方案如何删除JFrame 屏幕截图的标题栏?

以下程序截取其 JFrame 的屏幕截图并将其写入文件。

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*/ }
    }
}

这是您想要的吗,if_zero_equals_one?如果没有,也许您可​​以在您的问题中添加一些代码,以尝试执行您想要的操作。

PS感谢 Dariencamickr ,谁指出了在哪里可以找到该来源
例子。
也许这应该是一个评论。但这样的格式就更清晰了。

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.

╰沐子 2024-11-22 01:45:48
BufferedImage image = (BufferedImage)createImage(getContentPane().getSize().width, getContentPane().getSize().height);
getContentPane().paint(image.getGraphics());

我相信这就是我一直在寻找的东西。

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

This is what I believe I was looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文