所以我制作了一个应用程序,可以从 csv 文件创建图形时间线。我已经完成了那部分现在我只需要帮助获得“漂亮”的图像。捕获图像时,JFrame 的边框也会被捕获!如何使边界不被捕获?或者我如何摆脱它并保持图像大小?
问问题
413 次
2 回答
2
这是一个简单的例子。只是为了澄清您的需求。基于如何从 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 感谢Darien和camickr,他们指出了在哪里可以找到该示例的来源。也许这应该是一个评论。但是使用这种格式会更清楚。
于 2011-06-11T10:09:40.230 回答
0
BufferedImage image = (BufferedImage)createImage(getContentPane().getSize().width, getContentPane().getSize().height);
getContentPane().paint(image.getGraphics());
这就是我相信我一直在寻找的东西。
于 2011-06-13T17:32:42.657 回答