If you want to take screenshot, what will you do ? Click on printScreen button and paste in Microsoft Paint and then save that screenshot to some folder. If you are testing any software then you have to take plenty of screenshots then this process will be irritating. By using below java program you can easily take screenshots with single click.
Here the Directory Path represents destination folder to store screenshots 
import java.awt.AWTException;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
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;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Screen2Image {
    JFrame jFrame = new JFrame("Take Screen Shots");  // Create Frame        
    JLabel dirPathLb = new JLabel("Directory Path");
    JLabel error = new JLabel("");
    JTextField dirFld = new JTextField(20);
    JButton takeScrButton = new JButton("Take Screen Shot");
    int count = 0;
    
    public void showPanel() throws HeadlessException, AWTException, IOException {
        jFrame.setLayout(new FlowLayout());
        JPanel jp =new JPanel(); 
        jp.add(dirPathLb);
        jp.add(dirFld);
        dirFld.setText("C:\\");
        error.setForeground(Color.red);
        jFrame.add(jp);
        jFrame.add(takeScrButton);
        jFrame.add(error);
        jFrame.setSize(400,120);    // Set size of frame
        jFrame.setVisible(true);    // Show the window
        takeScrButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                BufferedImage image;
                try {
                    image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
                    count++;
                    String dirStr = dirFld.getText();
                    File dir = new File(dirStr);
                    if(!dir.exists()){
                        dir.mkdirs();
                    }
                    File file = new File(dir+File.separator+"screen"+count+".png");
                    if(!file.exists()) {
                        file.createNewFile();
                    }
                    ImageIO.write(image, "png", file);
                    error.setText("");
                } catch (HeadlessException e1) {
                    e1.printStackTrace();
                    error.setText(e1.getMessage());
                } catch (AWTException e1) {
                    e1.printStackTrace();
                    error.setText(e1.getMessage());
                } catch (IOException e1) {
                    e1.printStackTrace();
                    error.setText(e1.getMessage());
                }
                
                
            }
         });
        
    }

    public static void main(String[] args) throws HeadlessException, AWTException, IOException {
        new Screen2Image().showPanel();
    }
}

2 comments:



  1. Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well.
    I wanted to thank you for this websites! Thanks for sharing. Great websites!

    https://happychick.one
    happychick pc


    ReplyDelete
  2. Good blog. Keep sharing. I love them Are you also searching for Pay For Essay Online? we are the best solution for you. We are best known for delivering online essay writing services to students without having to break the bank

    ReplyDelete

Blogroll

Popular Posts