8

I'm making a Java based screen shot application, and I want to make it so when you push a combination of keys on your keyboard something like this video happens where you select and area on your screen, and it takes a screen shot of the selected area.

How to select an area to capture using the mouse?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jonathan Beaudoin
  • 2,158
  • 4
  • 27
  • 63

2 Answers2

18

Start with something like this.

Screen Capture Rectangle

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

/** Getting a Rectangle of interest on the screen.
Requires the MotivatedEndUser API - sold separately. */
public class ScreenCaptureRectangle {

    Rectangle captureRect;

    ScreenCaptureRectangle(final BufferedImage screen) {
        final BufferedImage screenCopy = new BufferedImage(
                screen.getWidth(),
                screen.getHeight(),
                screen.getType());
        final JLabel screenLabel = new JLabel(new ImageIcon(screenCopy));
        JScrollPane screenScroll = new JScrollPane(screenLabel);

        screenScroll.setPreferredSize(new Dimension(
                (int)(screen.getWidth()/3),
                (int)(screen.getHeight()/3)));

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(screenScroll, BorderLayout.CENTER);

        final JLabel selectionLabel = new JLabel(
                "Drag a rectangle in the screen shot!");
        panel.add(selectionLabel, BorderLayout.SOUTH);

        repaint(screen, screenCopy);
        screenLabel.repaint();

        screenLabel.addMouseMotionListener(new MouseMotionAdapter() {

            Point start = new Point();

            @Override
            public void mouseMoved(MouseEvent me) {
                start = me.getPoint();
                repaint(screen, screenCopy);
                selectionLabel.setText("Start Point: " + start);
                screenLabel.repaint();
            }

            @Override
            public void mouseDragged(MouseEvent me) {
                Point end = me.getPoint();
                captureRect = new Rectangle(start,
                        new Dimension(end.x-start.x, end.y-start.y));
                repaint(screen, screenCopy);
                screenLabel.repaint();
                selectionLabel.setText("Rectangle: " + captureRect);
            }
        });

        JOptionPane.showMessageDialog(null, panel);

        System.out.println("Rectangle of interest: " + captureRect);
    }

    public void repaint(BufferedImage orig, BufferedImage copy) {
        Graphics2D g = copy.createGraphics();
        g.drawImage(orig,0,0, null);
        if (captureRect!=null) {
            g.setColor(Color.RED);
            g.draw(captureRect);
            g.setColor(new Color(255,255,255,150));
            g.fill(captureRect);
        }
        g.dispose();
    }

    public static void main(String[] args) throws Exception {
        Robot robot = new Robot();
        final Dimension screenSize = Toolkit.getDefaultToolkit().
                getScreenSize();
        final BufferedImage screen = robot.createScreenCapture(
                new Rectangle(screenSize));

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ScreenCaptureRectangle(screen);
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Wow thanks so much man! So helpful. Just wondering did you make that? – Jonathan Beaudoin Jun 13 '12 at 02:44
  • This will only work if you are capturing a screenshot of the Java application you are running. You won't be able to screen grab other applications running on the user's desktop. I thought you wanted a general purpose screen capture app which means you can't use Swing. – chubbsondubs Jun 13 '12 at 15:22
  • @chubbard *"This will only work if you are capturing a screenshot of the Java application you are running"* Your application needs to be hidden before the screenshot is taken. I discuss that in separate answers in response to that (separate) question. – Andrew Thompson Jun 13 '12 at 15:36
  • Wow! Never thought, that it would be that easy... Perfect answer! – Sauer Apr 16 '13 at 13:13
  • @Andrew Thompson .. Its working fine.. How to work with our own predefined image instead of capturing a screenshot of the Java application. – Revan Oct 15 '14 at 07:30
  • @tHiNk_OuT_oF_bOx 'How to load an image?' is an entirely different question (that has already been answered ***many*** times). – Andrew Thompson Oct 15 '14 at 21:45
  • Dear @Andrew Thompson.. As this discussion of question is "Select an area to capture using the mouse".. In this question,he has not mentioned its from image or from existing screen. Then how you can say that it is an entirely different question. Can you please explain it ? – Revan Oct 16 '14 at 05:50
  • *"Can you please explain it ?"* Given I don't understand any of the logic behind your comment, no. – Andrew Thompson Oct 16 '14 at 06:11
  • Thanks for this very useful snippet. It appears to work as it stands only when dragging from top-left to bottom-right. I changed the mouseDragged method so it works in all directions. Easy to do. I can post the code if of interest. Also any bufferedImage passed to the constructor will work-it doesn't have to be a screenshot. That's just how the main method is setup. – torwalker Jul 08 '15 at 13:57
1

This is pretty simple to do:

http://www.javalobby.org/forums/thread.jspa?threadID=16400&tstart=0

http://www.javaworld.com/javaworld/jw-04-2006/jw-0424-funandgames.html

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138