Results 1 to 18 of 18
Thread: Screen Capture Recursion
- 03-18-2012, 02:30 AM #1
Member
- Join Date
- Jan 2012
- Location
- The Coffee Pot
- Posts
- 36
- Rep Power
- 0
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:
Custom JPanel: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); } } }
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(); } }
-
Re: Screen Capture Recursion
I'm greatly surprised you're not getting a NullPointerException with that code.
- 03-18-2012, 03:04 AM #3
Member
- Join Date
- Jan 2012
- Location
- The Coffee Pot
- Posts
- 36
- Rep Power
- 0
Re: Screen Capture Recursion
Haha why?
-
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.
- 03-18-2012, 03:23 AM #5
Member
- Join Date
- Jan 2012
- Location
- The Coffee Pot
- Posts
- 36
- Rep Power
- 0
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?
-
Re: Screen Capture Recursion
- 03-18-2012, 03:37 AM #7
Member
- Join Date
- Jan 2012
- Location
- The Coffee Pot
- Posts
- 36
- Rep Power
- 0
Re: Screen Capture Recursion
In the constructor:
It's writing just fine but still not displaying, even now that i'm extracting the graphics from the image and using that.Java Code:ImageIO.write(image, "png", new File("img")); panel.paintComponent(image.getGraphics(), image, 0, 0, image.getWidth(), image.getHeight()); //test draw
-
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.
-
Re: Screen Capture Recursion
A pseudo-code solution that just now worked for me is to create a recursiveDraw method with this signature:
and with the following logicJava Code:private static void recursiveDraw(BufferedImage img, Graphics imgG, double scale) {
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
- 03-18-2012, 04:29 AM #10
Member
- Join Date
- Jan 2012
- Location
- The Coffee Pot
- Posts
- 36
- Rep Power
- 0
Re: Screen Capture Recursion
Thanks the image is actually showing up now and your pseudo-code looks helpful
-
Re: Screen Capture Recursion
- 03-18-2012, 07:39 AM #12
Re: Screen Capture Recursion
My eyes! My eyes!
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-18-2012, 09:00 AM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Screen Capture Recursion
Tasteful; and so restful for the eyes ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-18-2012, 12:06 PM #14
Re: Screen Capture Recursion
Wha'? Cant read what you said, my eyes are focused at infinity
Why do they call it rush hour when nothing moves? - Robin Williams
- 03-18-2012, 12:21 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
-
Re: Screen Capture Recursion
Cool, but how about a kaleidoscope now?
-
Re: Screen Capture Recursion
-
Re: Screen Capture Recursion
To see my code, please have a look here: stackoverflow.com: trying-to-paint-image-to-jframe-with-java-bufferedimage-graphics
Similar Threads
-
Automated screen capture app possible in Java?
By leke in forum New To JavaReplies: 1Last Post: 11-27-2010, 02:05 PM -
Screen Capture with WebSpec(Watij).
By sagngh8 in forum Advanced JavaReplies: 0Last Post: 11-24-2010, 11:50 AM -
How to create off screen BufferedImage capture
By crikey in forum New To JavaReplies: 7Last Post: 09-07-2010, 12:59 PM -
How to do a screen capture of content on web page/join 2 images
By raabie in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 08-27-2010, 01:02 PM -
Need Help on Remote Host Screen Capture
By krishnaraoveera1294 in forum AWT / SwingReplies: 4Last Post: 03-13-2009, 10:56 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote




Bookmarks