Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Capturing screen shot

                         

In this section, you will learn how 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 shows you how several methods and APIs are used for capturing screen shot. This program capture the screen and makes the "jpg" file. It show 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.". For this purpose following methods and APIs have been used:

createScreenCapture():
This 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.

write():
This is the method of ImageIO class. This method 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.

ImageIO:
This is the class of javax.imageio.*; package. This class is used for reading an image or writing an image.

Here is the code of the program:

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[] argsthrows 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(400400);
    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 : ",

 "Roseindia.net"1);
        if (!fileName.toLowerCase().endsWith(".gif")){
          JOptionPane.showMessageDialog(null, "Error: file name must end with 
\".gif\"."
"Roseindia.net"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.",
 
"Roseindia.net"1);
        }
      }
      catch(Exception e){}
    }
  }
}

Screen shot for the result of the above program:

Entering new file name for captured screen shot

Message after capturing the screen shot

Download this example.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (post your own) View All Comments Latest 10 Comments:

I have a requirement as, to capture the screen of different client browser from server using tomcat as servlet container.

Please any one help me in this regard

Posted by arun on Saturday, 05.26.07 @ 19:14pm | #17363

Hi
Joseph!
I got you comment. For knowing about the table cell renderer just click the given link. There you will find the complete description of JTable in which you will get the topic table cell renderer. Thanks for visiting out site and our comment.
Jtable Description

vinod kumar

Posted by vinod kumar on Friday, 03.23.07 @ 18:30pm | #12570

please send some table cell renderer examples in java. and also table listeners...

Posted by joseph on Monday, 02.26.07 @ 15:34pm | #9658

please send me a program that is to get data from another dialog or frame.. and inserting a frame in another frame. thanking you

Posted by Joseph on Monday, 02.26.07 @ 15:32pm | #9656

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.