Java 完整截图
我想在程序的 JFrame 上显示屏幕的完整屏幕截图。 到目前为止,使用下面的代码,我只能显示屏幕的一部分。 下面的代码是paint(Graphics g)的内容。 我怎样才能让它全屏显示?
// the screen resolution is 1280 x 1024 while the JPanel size is only 1024 x 768
Dimension resolution = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rectangle = new Rectangle(resolution);
robot = new Robot();
BufferedImage bufferedImage = robot.createScreenCapture(rectangle);
g.drawImage(bufferedImage.getScaledInstance(bufferedImage.getWidth(), bufferedImage.getHeight(), Image.SCALE_DEFAULT), 0, 0, null);
I wanted to display on the JFrame on the program the full screenshot of my screen.
So far using the code below, I was able only to display part of the screen.
The code below is the content of the paint(Graphics g).
How can I make it full screen?
// the screen resolution is 1280 x 1024 while the JPanel size is only 1024 x 768
Dimension resolution = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rectangle = new Rectangle(resolution);
robot = new Robot();
BufferedImage bufferedImage = robot.createScreenCapture(rectangle);
g.drawImage(bufferedImage.getScaledInstance(bufferedImage.getWidth(), bufferedImage.getHeight(), Image.SCALE_DEFAULT), 0, 0, null);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许使用这样的东西:
我看到你有一些错误,我不知道你的代码是否可以编译,因为引用似乎没有被声明,但与此类似的代码将捕获你的桌面的屏幕截图:
我保存到 png 图像文件,而不是将其绘制在屏幕或框架上。
Maybe using something like this:
I see you have some errors, I don't know if your code even compiles, 'cause references seems not to be declared, but a code similar to this one will caputure a screenshoot of your desktop:
I saved to an png image file instead of drawing it on the screen or the frame.
使用 java.awt.Toolkit.getDefaultToolkit().getScreenSize() 来获取屏幕的大小: http://www.roseindia.net/java/java-get-example/screen-dimensions.shtml
Use
java.awt.Toolkit.getDefaultToolkit().getScreenSize()
to get the size of the screen: http://www.roseindia.net/java/java-get-example/screen-dimensions.shtml通过每行仅使用一个语句来简化代码,然后您就可以理解代码。
为什么要设置图像的宽度和高度?如果指定图像的完整尺寸,如何缩放图像?我猜你想要:
现在你可以将图像添加到 JLabel,而不是进行自定义绘画:
Simplify your code by only using one statement per line then you might be able to understand the code.
Why are you ue the width and height of the image? How does that scale the image if you specify the full size of the image? I would guess you want:
Now instead of doing custom painting you can just add your image to a JLabel: