Results 1 to 18 of 18
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By Fubarable

Thread: Screen Capture Recursion

  1. #1
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default Screen Capture Recursion



    Hey guys I saw this image today and randomly decided I wanted to try it in java. It's a little rough, but I ran it anyways and the image won't even show up, so I put the draw call in the constructor but it still won't draw, rookie mistake? And are there any other obvious mistakes?

    Main class:
    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
     
    public class RecurseScreen{
    	private JFrame frame;
    	private RecursePanel panel;
    	private Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    	private BufferedImage image;
    	
    	public static void main(String[] args){
    		try{
    			new RecurseScreen();
    		} catch(Exception e){
    			e.printStackTrace();
    		}
    	}
    	
    	public RecurseScreen() throws Exception{
    		frame = new JFrame();
    		panel = new RecursePanel();
    
    		
    		Rectangle screenRectangle = new Rectangle(screenSize);
    		Robot robot = new Robot();
    		image = robot.createScreenCapture(screenRectangle);
    		
    		frame.setSize(screenSize.width - 100, screenSize.height -100);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setResizable(false);
    		frame.setTitle("Yo dawg");
    		frame.setVisible(true);
    		frame.setLocationRelativeTo(null);
    		frame.add(panel);
    		
    		panel.paintComponent(image, 0, 0, image.getWidth(), image.getHeight()); //test draw
    		
    		//captureScreen(0, 0);
    	}
     
    	public void captureScreen(int x, int y) throws Exception{
    
    		if(x > frame.getWidth()/2){
    		}else{
    			panel.paintComponent(image, x, y, image.getWidth(), image.getHeight());
    		
    			BufferedImage img = new BufferedImage(image.getWidth() - 2*x, image.getHeight() - 2*y, image.getType()); 
    			image = img;
    			captureScreen(x/2 + 15, y/2 + 15);
    		}
    	}
    }
    Custom JPanel:
    Java Code:
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    
    import javax.swing.*;
    
    public class RecursePanel extends JPanel{
    	private Graphics g;
    	
    	public void paintComponent(BufferedImage img, int x, int y, int width, int height){
    		super.paintComponent(g);
    		
    		g.drawImage(img, x, y, width, height, null);
    		repaint();
    	}
    }

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

    Default Re: Screen Capture Recursion

    I'm greatly surprised you're not getting a NullPointerException with that code.

  3. #3
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default Re: Screen Capture Recursion

    Haha why?

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

    Default Re: Screen Capture Recursion

    Where do you initialize your Graphics object?

    But regardless, if this were my program, I'd put all my energy into creating a BufferedImage, and when done, display or store the BufferedImage.

  5. #5
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default Re: Screen Capture Recursion

    Oh I wasn't even looking in that class, good point, but I can't instantiate Graphics? And as for the BufferedImage, I thought that's what I was doing? Taking a screenshot and drawing it on the panel right?

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

    Default Re: Screen Capture Recursion

    Quote Originally Posted by Bestsanchez View Post
    Oh I wasn't even looking in that class, good point, but I can't instantiate Graphics? And as for the BufferedImage, I thought that's what I was doing? Taking a screenshot and drawing it on the panel right?
    Well, is your code as written working? If not, then no you're not doing this.

    Again, I'd work with just the BufferedImage, including extracting its Graphics object and drawing with that.

  7. #7
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default Re: Screen Capture Recursion

    In the constructor:
    Java Code:
    		ImageIO.write(image, "png", new File("img"));
    		
    		panel.paintComponent(image.getGraphics(), image, 0, 0, image.getWidth(), image.getHeight()); //test draw
    It's writing just fine but still not displaying, even now that i'm extracting the graphics from the image and using that.

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

    Default Re: Screen Capture Recursion

    Again, I would separate out your Swing code from your recursive image creation code. In fact consider creating a static method that creates and returns the BufferedImage and that has no Swing code in it. Then have your GUI call the method when it wishes, and take the image and either write it to disk or display it in a JLabel's ImageIcon. Your current code can't display anything since your JPanel doesn't have a valid paintComponent override method.

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

    Default Re: Screen Capture Recursion

    A pseudo-code solution that just now worked for me is to create a recursiveDraw method with this signature:

    Java Code:
    private static void recursiveDraw(BufferedImage img, Graphics imgG, double scale) {
    and with the following logic

    Java Code:
    start recursiveDraw method
      // most important: all recursions must have a good ending condition:
      get img height and width. If either <= a min, return 
    
      create a BufferedImage, smlImg, for the smaller image using the height, width and scale factor
      Get the Graphics object, smlG, from the small image
      Use smlG.drawImage(...) overload to draw the big image in shrunken form onto the little image
      recursively call recursiveDraw passing in smlImg, smlG, and scale.
      dispose of smlG
      draw smlImg (the small image) onto the bigger one using the bigger's Graphics object (passed
         into this method) and a different overload of the drawImage method.
    end recursiveDraw method
    Bestsanchez likes this.

  10. #10
    Bestsanchez is offline Member
    Join Date
    Jan 2012
    Location
    The Coffee Pot
    Posts
    36
    Rep Power
    0

    Default Re: Screen Capture Recursion

    Thanks the image is actually showing up now and your pseudo-code looks helpful

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

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,247
    Rep Power
    19

    Default Re: Screen Capture Recursion

    My eyes! My eyes!

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: Screen Capture Recursion

    Tasteful; and so restful for the eyes ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,247
    Rep Power
    19

    Default Re: Screen Capture Recursion

    Wha'? Cant read what you said, my eyes are focused at infinity
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default Re: Screen Capture Recursion

    Quote Originally Posted by DarrylBurke View Post
    Wha'? Cant read what you said, my eyes are focused at infinity
    You're lucky the 'camera' wasn't slightly rotated while filming the recursive snap shot; that is where the real fun begins! ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Screen Capture Recursion

    Cool, but how about a kaleidoscope now?

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

    Default Re: Screen Capture Recursion

    Quote Originally Posted by DarrylBurke View Post
    My eyes! My eyes!

    db
    Don't worry -- I can produce even scarier pictures!

    Screen Capture Recursion-darrylnew.jpgScreen Capture Recursion-spot-tartannew.jpg

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

Similar Threads

  1. Automated screen capture app possible in Java?
    By leke in forum New To Java
    Replies: 1
    Last Post: 11-27-2010, 02:05 PM
  2. Screen Capture with WebSpec(Watij).
    By sagngh8 in forum Advanced Java
    Replies: 0
    Last Post: 11-24-2010, 11:50 AM
  3. How to create off screen BufferedImage capture
    By crikey in forum New To Java
    Replies: 7
    Last Post: 09-07-2010, 12:59 PM
  4. How to do a screen capture of content on web page/join 2 images
    By raabie in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 08-27-2010, 01:02 PM
  5. Need Help on Remote Host Screen Capture
    By krishnaraoveera1294 in forum AWT / Swing
    Replies: 4
    Last Post: 03-13-2009, 10:56 PM

Posting Permissions

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