Results 1 to 4 of 4
  1. #1
    aprop is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Taking a screenshot and redisplaying it

    Im trying to use a robot to take an screenshot and paint it on a seperate JFrame.
    I keep getting a blank frame everytime, can anyone help me?

    Event to create a Robot
    Java Code:
    public  Testmouseevents() throws AWTException{
    	GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    	Robot t=new Robot();
    	Dimension d=new Dimension(1200,1200);
    	Rectangle k=new Rectangle(d);
    	try {
    		OutputImage oi=new OutputImage(t.createScreenCapture(k));
    	} catch (IOException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    Output Image should show my image

    Java Code:
    public class OutputImage extends JPanel{
    	BufferedImage j;
    	
    	OutputImage(BufferedImage h) throws IOException{
    		j=h;
    		JFrame f=new JFrame();
    		f.setVisible(true);
    		f.setSize(h.getWidth(), h.getHeight());
    		f.setLocation(0,0);
    		Graphics g=h.getGraphics();
    		
    	}
    	public void paint(Graphics g){
    		
    		g.drawImage(j, 50, 0, null);
    	}
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Taking a screenshot and redisplaying it

    Don't draw directly in a JFrame. Instead you're better off drawing in the paintComponent method of a JPanel and placing that JPanel in your JFrame. Even better for your situation is to simply make an ImageIcon out of your BufferedImage, placing this into a JLabel and displaying the JLabel.

    e.g.,

    Java Code:
    import java.awt.AWTException;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.image.BufferedImage;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    
    public class ShowImage {
       public static void main(String[] args) {
          try {
             Robot robot = new Robot();
             Rectangle screenRect = new Rectangle(1200, 800);
             BufferedImage img = robot.createScreenCapture(screenRect);
             ImageIcon icon = new ImageIcon(img);
             JLabel label = new JLabel(icon);
             JOptionPane.showMessageDialog(null, label);
             
             // or if you need a JFrame:
             JFrame frame = new JFrame("Show Image");
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.getContentPane().add(label);
             frame.pack();
             frame.setLocationRelativeTo(null);
             frame.setVisible(true);
             
          } catch (AWTException e) {
             e.printStackTrace();
          }
          
       }
    }
    Last edited by Fubarable; 03-03-2012 at 05:00 PM.

  3. #3
    aprop is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Taking a screenshot and redisplaying it

    Quote Originally Posted by Fubarable View Post
    Don't draw directly in a JFrame. Instead you're better off drawing in the paintComponent method of a JPanel and placing that JPanel in your JFrame. Even better for your situation is to simply make an ImageIcon out of your BufferedImage, placing this into a JLabel and displaying the JLabel.

    e.g.,

    Java Code:
    import java.awt.AWTException;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.image.BufferedImage;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    
    public class ShowImage {
       public static void main(String[] args) {
          try {
             Robot robot = new Robot();
             Rectangle screenRect = new Rectangle(1200, 800);
             BufferedImage img = robot.createScreenCapture(screenRect);
             ImageIcon icon = new ImageIcon(img);
             JLabel label = new JLabel(icon);
             JOptionPane.showMessageDialog(null, label);
             
             // or if you need a JFrame:
             JFrame frame = new JFrame("Show Image");
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.getContentPane().add(label);
             frame.pack();
             frame.setLocationRelativeTo(null);
             frame.setVisible(true);
             
          } catch (AWTException e) {
             e.printStackTrace();
          }
          
       }
    }
    Thanks that worked perfectly!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Taking a screenshot and redisplaying it

    You're welcome.

Similar Threads

  1. Taking input from use
    By leepikamukharji in forum New To Java
    Replies: 1
    Last Post: 11-08-2011, 02:33 PM
  2. Replies: 2
    Last Post: 05-22-2011, 08:56 AM
  3. Replies: 3
    Last Post: 05-03-2011, 03:24 AM
  4. Capturing a screenshot of a another pc?
    By chyrl in forum Advanced Java
    Replies: 9
    Last Post: 06-08-2010, 05:07 PM
  5. Get screenshot(picture) of own pc
    By shekhar2010 in forum New To Java
    Replies: 5
    Last Post: 01-24-2010, 03:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •