开发者

Java Full Screenshot

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 10开发者_Go百科24 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);


Maybe using something like this:

//get the screen size
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
BufferedImage image = robot.createScreenCapture (dim);
//other code
//...

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:

import java.awt.geom.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;

public class ScreenCapturer
{
    public static void main(String[] args)throws Exception
    {
        Dimension resolution = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle rectangle = new Rectangle(resolution);
        Robot robot = new Robot();
        BufferedImage bufferedImage = robot.createScreenCapture(rectangle);
        Graphics g = bufferedImage.getGraphics();
        //g.drawImage(bufferedImage.getScaledInstance(bufferedImage.getWidth(), bufferedImage.getHeight(), Image.SCALE_DEFAULT), 0, 0, null);
        File out = new File("image.png");
        ImageIO.write(bufferedImage,"png",out);
    }
}

I saved to an png image file instead of drawing it on the screen or the frame.


Use java.awt.Toolkit.getDefaultToolkit().getScreenSize() to get the size of the screen: http://www.roseindia.net/java/java-get-example/screen-dimensions.shtml


g.drawImage(bufferedImage.getScaledInstance(bufferedImage.getWidth(), bufferedImage.getHeight(), Image.SCALE_DEFAULT), 0, 0, null);

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:

Image scaled = bufferedImage.getScaledInstance(1024, 768, Image.SCALE_DEFAULT);

Now instead of doing custom painting you can just add your image to a JLabel:

ImageIcon icon = new ImageIcon( scaled );
JLabel label = new JLabel( icon );
frame.add( label );
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜