Jump to content

Java screen capture application in Netbeans.

- - - - -

  • Please log in to reply
1 reply to this topic

#1
farrell2k

farrell2k

    Learning Programmer

  • Members
  • PipPipPip
  • 60 posts
Create a java application that captures screenshots of your desktop and saves them as jpg.

1. Create a new Java application in Netbeans, naming it what you wish. Do not create a main class. Uncheck this option.

2. Right click on the name of your project and click New -> JFrame Form, naming it as you wish.

3. To the right, in your Pallete, under Swing controls, drag a label and a button on to the form, aligning them as you wish. Right click on label, selectv'edit text', then type in some text. I typed "Screen Capture", and did the same for the button, listing it as "save".

3. Right click on any edge of the JFrame form to resize it so that it looks more appropriate. Now right click anywhere on the JFrame form and click Properties. Edit it's title as you wish then close down the properties window.

4. Right above the top-left of the form, click on 'source' to enter the form's source code view. We need to import an AWT class called Robot that makes all of the magic in our application possible. Add: import java.awt.Robot; above the class declaration.

5. Near the 'source' button you just clicked there's a 'design' button which will bring you back to the visual designed. click it. Double click on the button you added. This will create and add an ActionListener to the button and bring you to the button's actionListener method. You need to add the following code:



       //create a BufferedImage to store the screen capture.

       BufferedImage image = null;

        try {

            //Robot().createScreenCapture returns a BufferedImage the size of the screen with Toolkit's getScreenSize().

            image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));

        }

        catch(AWTException e) {

            e.printStackTrace();

        }

        

        //represents file to be saved.

        File file = null;


        //JFileChooser used for openong save dislog in which you type the name of your screenshot.

        JFileChooser choose = new JFileChooser();


        //pop up a save dialog in this JFrame window. 

        int result = choose.showSaveDialog(this);


        //JFileChooser's showSaveDialog() returns an int, and if that int represents the static field CANCEL_OPTION, the user has pressed cancel on the save dialog.  In this case, we simply return.

        if (result == JFileChooser.CANCEL_OPTION)

            return;


        //if user types in a file name and clicks 'save' in the dialog box, then the file typed in becomes the file to be saved.

        else


            file = choose.getSelectedFile();

        

        try {

            //we write the file to be saved with ImageIO.write().  We supply the image, the file type, jpg, and the file, which is 'file'.

            ImageIO.write(image, "jpg", file);

        }

        catch(IOException ioe) {

            ioe.printStackTrace();

        }

Do a CTRL+SHIFT+I to have NetBeans add all of the required imports to the application. Press F6 to compile and run the project. Remember to save your file names appropriately. You need to end them in .jpg.

That's it. Java is awesome, and so are you!

#2
psybercode

psybercode

    Newbie

  • Members
  • Pip
  • 1 posts
does this work in netbeans 7

and do i still need to add a main section?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users