Thursday, February 21, 2013

Capturing a Screen Shot in JAVA

This tutorial provides you with code to capture a screen shot using java. You can capture a part of screen or the whole screen as well. Just copy the below code and save the file as ScreenShotPanel.java compile and run it using java SDK.

import java.awt.*;
import javax.swing.*;

/**
 *
 * @author Originative
 */

class ScreenShotPanel extends JPanel{
    Image screenShot=null;
    Rectangle area = null;
    private Image image;
    public ScreenShotPanel(){
        JLabel imagePanel=new JLabel();
        
        // Capture a particular area on the screen
//        int x = 100;
//        int y = 100;
//        int width = 400;
//        int height = 400;
//        area = new Rectangle(x, y, width, height);
//        screenShot=ScreenShotPanel.captureImage(area);
        
        // Capture the whole screen
        area = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        screenShot=ScreenShotPanel.captureImage(area);
    }
    
    
    public static Image captureImage(Rectangle area){
        Image img=null;
        try{
            Robot robot = new Robot();
            img = (Image)robot.createScreenCapture(area);   
        }
        catch(Exception e){
            System.out.println(e);
        }
        return img;
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(screenShot, 0, 0, null);
    }
    public static void main(String []nix){
        ScreenShotPanel screenShotPanel=new ScreenShotPanel();
        JFrame screenShotFrame=new JFrame();
        screenShotFrame.add(screenShotPanel);
        screenShotFrame.setSize(800, 800);
        screenShotFrame.setVisible(true);
        screenShotFrame.setTitle("Capturing a Screen Shot - Tutorial Jinni");
        screenShotFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}


if every thing works as excepted then you will see a similar window as below.

Capturing a Screen Shot in java

1 comments:

Unknown on March 3, 2020 at 5:15 PM said...

Easy "water hack" burns 2 lbs OVERNIGHT

At least 160 000 women and men are using a easy and SECRET "water hack" to lose 1-2lbs every night as they sleep.

It's painless and works on everybody.

Just follow these easy step:

1) Hold a clear glass and fill it up half full

2) Then follow this weight loss HACK

and you'll be 1-2lbs lighter as soon as tomorrow!

Post a Comment

 

Blog Info

A Pakistani Website by Originative Systems

Total Pageviews

Tutorial Jinni Copyright © 2015 WoodMag is Modified by Originative Systems