0

I am taking screen captures of my screen using the following example:

try {
   Robot robot = new Robot();
   BufferedImage image = robot.createScreenCapture(new Rectangle(0, 0, 200, 200));
} catch java.awt.AWTException exc) {
    System.out.println("error: main");
}

Now I want to draw the screen shots on a component of a GUI. I don't know which component I should use, but I want to draw the images every frame(~ 20 FPS are desired), that it looks like a movie, a camera that films my desktop. And, I want to draw boxes over certain areas of the drawn area.

How could I do this? Every example I was shown did following:

  • An image gets loaded from file system at start up of the program and drawn to a JPanel, therefore one couldn't paint on it that easily I want to do so.

That doesn't work(grey panel):

visualization = new JPanel();
visualization.setLayout(new BorderLayout());
try {
        Robot robot = new Robot();
        BufferedImage image = robot.createScreenCapture(new Rectangle(0, 0, 200, 200));
        Graphics2D g = image.createGraphics();
        visualization.paint(g);
        visualization.setVisible(true);

    }
    catch(java.awt.AWTException exc) {
        System.out.println("error: main");
    }
HiAll2.0
  • 1
  • 2
  • *"An image gets loaded .. one couldn't paint on it that easily"* Sure you could. See [BufferedImage.getGraphics()](http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html#getGraphics%28%29). – Andrew Thompson Dec 16 '11 at 10:18

3 Answers3

2

best way as I know is put image as Icon to the JLabel, simple example

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

public class CaptureScreen implements ActionListener {

    private JFrame f = new JFrame("Screen Capture");
    private JPanel pane = new JPanel();
    private JButton capture = new JButton("Capture");
    private JDialog d = new JDialog();
    private JScrollPane scrollPane = new JScrollPane();
    private JLabel l = new JLabel();
    private Point location;

    public CaptureScreen() {
        capture.setActionCommand("CaptureScreen");
        capture.setFocusPainted(false);
        capture.addActionListener(this);
        capture.setPreferredSize(new Dimension(300, 50));
        pane.add(capture);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(pane);
        f.setLocation(100, 100);
        f.pack();
        f.setVisible(true);
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                createPicContainer();
            }
        });
    }

    private void createPicContainer() {
        l.setPreferredSize(new Dimension(700, 500));
        scrollPane = new JScrollPane(l,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setBackground(Color.white);
        scrollPane.getViewport().setBackground(Color.white);
        d.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
        d.add(scrollPane);
        d.pack();
        d.setVisible(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("CaptureScreen")) {
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); // gets the screen size
            Robot r;
            BufferedImage bI;
            try {
                r = new Robot(); // creates robot not sure exactly how it works
                Thread.sleep(1000); // waits 1 second before capture
                bI = r.createScreenCapture(new Rectangle(d)); // tells robot to capture the screen
                showPic(bI);
                saveImage(bI);
            } catch (AWTException e1) {
                e1.printStackTrace();
            } catch (InterruptedException e2) {
                e2.printStackTrace();
            }
        }
    }

    private void saveImage(BufferedImage bI) {
        try {
            ImageIO.write(bI, "JPG", new File("screenShot.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void showPic(BufferedImage bI) {
        ImageIcon pic = new ImageIcon(bI);
        l.setIcon(pic);
        l.revalidate();
        l.repaint();
        d.setVisible(false);
        location = f.getLocationOnScreen();
        int x = location.x;
        int y = location.y;
        d.setLocation(x, y + f.getHeight());
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                d.setVisible(true);
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                CaptureScreen cs = new CaptureScreen();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

How could I do this? Every example I was shown did following: An image gets loaded from file system at start up of the program and drawn to a JPanel, therfore one couldn't paint on it that easily I want to do so.

That is the way to go. Extend an JPanel and override paintComponent, add mouse listeners for custom drawing, and do the drawing on top of your BufferdImage in the paintComponent method.

UPDATE

If you don't know how to extend an JPanel and implement paintComponent see my answer to How to draw a JPanel as a Nimbus JButton?

Community
  • 1
  • 1
Jonas
  • 121,568
  • 97
  • 310
  • 388
  • I mentioned those methods. They don't work for me, I have no clue how to do this. Why can't I just use something like myFrame.getGraphics().drawImage(image, 0, 0, 200, 200); ? – HiAll2.0 Dec 16 '11 at 10:02
  • @HiAll2.0: "*They don't work for me, I have no clue how to do this.*" - can you describe *what* probelm you have, and *what* doesn't work? – Jonas Dec 16 '11 at 10:06
  • Everything I tried doesn't work. I hoped someone would give me an example of a working piece of code, which does this. – HiAll2.0 Dec 16 '11 at 10:10
  • @HiAll2.0: show the code you have tried so can we help you with the problems. We don't work for you, but we can help you. – Jonas Dec 16 '11 at 10:15
  • @HiAll2.0: You didn't do as I wrote in my answer: **extend** a `JPanel` and **overrride** `paintComponent` – Jonas Dec 16 '11 at 10:26
  • But in the function public void paintComponent(Graphics g), where the hell does g come from? How to actually do the painting every frame? – HiAll2.0 Dec 16 '11 at 10:33
  • @HiAll2.0: `g` is coming from Swing framework, you don't call this method, it's called by Swing automatically. It only paints one frame. You have to use a Swing timer to paint it repeatedly. – Jonas Dec 16 '11 at 10:35
  • @Jonas are you finally to find how to override protected methods in Nimbus???, if aephyr codesource was too complicated (my view) then easiest way to check seaglasslookandfeel codesource, because is based on Nimbus and there create own LazyPainter with own methods that are by default protected in origonal Nimbus L&F – mKorbel Dec 16 '11 at 20:16
  • @mKorbel: Yes, [this answer](http://stackoverflow.com/a/8265872/213269) solved that question again. I stumpled on the problem for the second time ;) I have to learn Swing better ;) Yeah, that code wasn't to much help. thanks anyway. – Jonas Dec 16 '11 at 20:25
  • are you meaning `UIManager.getLookAndFeelDefaults().put("nimbusFocus", Color.RED);` – mKorbel Dec 16 '11 at 20:32
  • @mKorbel: yes, but `.get("key");` – Jonas Dec 16 '11 at 20:40
1

I also suggest the way mKorbel mentioned, btw if you realy want it the way you said, this is the working code..

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ImagePane extends JPanel{

    private BufferedImage image;

    public ImagePane()  {


            try {
               Robot robot = new Robot();
                 image = robot.createScreenCapture(new Rectangle(0, 0, 500, 500));
            } catch (AWTException ex) {
                Logger.getLogger(ImagePanel.class.getName()).log(Level.SEVERE, null, ex);
            }

    }

    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(image, 0, 0, null); 

    }
public static void main(String[] args) {

        JFrame test = new JFrame();

            test.add(new ImagePane());
            Dimension b = new Dimension(500,500);
            test.setMinimumSize(b);

        test.setVisible(true);

    }
}
COD3BOY
  • 11,964
  • 1
  • 38
  • 56