© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
Quick Links
Easy Studies

Capturing Screen Shot Using Java


This program is to capture the screen. Whatever you want to do by pressing the "Print Screen" button on the keyboard, same thing is applied by the given program. This program captures the screen and makes the "jpg" file. It shows a frame which holds a command button labeled by "Capture Screen Shot". When you click on the button then a input dialog box will be opened for inputting the file name which has to be created after capturing the screen. And then a message dialog box is opened with message "Screen captured successfully”.

createScreenCapture () is the method of the Robot class. This method is used to create an image read by the screen. It takes the area which has been created using the getDefaultToolkit (). getScreenSize () method of the Toolkit class get the size of the screen which has to be captured.write () method of ImageIO class is used to make image file for the captured image. Before creating new image file the captured image is stored in the created image buffer. This method takes three arguments as follows: First is the rendered image. Second is the file format. And last is the output file name.

Capturing Screen Shot Using Java:

import javax.swing.*; import javax.imageio.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; public class Screenshot { public static void main(String[] args) throws Exception { Screenshot ss = new Screenshot(); } public Screenshot(){ JFrame frame = new JFrame("Screen Shot Frame."); JButton button = new JButton("Capture Screen Shot"); button.addActionListener(new MyAction()); JPanel panel = new JPanel(); panel.add(button); frame.add(panel, BorderLayout.CENTER); frame.setSize(400, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public class MyAction implements ActionListener{ public void actionPerformed(ActionEvent ae){ try{ String fileName = JOptionPane.showInputDialog(null, "Enter file name : ", "Get Input File", 1); if (!fileName.toLowerCase().endsWith(".gif")){ JOptionPane.showMessageDialog(null, "Error: file name must end with \".gif\".", "Dialog", 1); } else{ Robot robot = new Robot(); BufferedImage image = robot.createScreenCapture(new Rectangle( Toolkit.getDefaultToolkit().getScreenSize())); ImageIO.write(image, "gif", new File(fileName)); JOptionPane.showMessageDialog(null, "Screen captured successfully.", "Success Message", 1); } } catch(Exception e){} } } }

Sample ScreenShot:

SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑