Reputation: 1
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:
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");
}
Upvotes: 0
Views: 2223
Reputation: 12092
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);
}
}
Upvotes: 1
Reputation: 128827
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?
Upvotes: 1
Reputation: 109813
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();
}
});
}
}
Upvotes: 2