Forums Register Login

Writing out the contents of a JFrame to a file

+Pie Number of slices to send: Send
This should be easy, but it turns out not to be.. I obtained the following code online which purports to get the content of the JFrame:

Then I write out the image with the following code:

When I double-click on the resulting file, all the images show up as black rectangles in Windows Photo Viewer. Yet the associated buffered image displays correctly in the window. What am I doing wrong or not doing? How may I fix this to work? I've tried 'jpg, 'png', and 'bmp' extensions, with matching formats in the write. I am using Win-7 64-bit. and jdk8 with matching jre.

Thanks for any help.
+Pie Number of slices to send: Send
Basic code looks reasonable but without a proper SSCCE we can't tell how you actually use the code.

You may also want to check out Screen Image which basically does the same thing except that when writing the file it just specifies the filename, not the OutputStream.
+Pie Number of slices to send: Send
I prepared an SSCCE, and was trying to post it. Then I met the following problems:
1. I could not post a .zip file.
2. Although it met the size limits unzipped ( I had changed it so it would), it was 10 files, so it failed the file count.
3. Only 2 of the files are Java, so I tried to post them alone. I could not. After attaching the first file, the second file replaced the first rather than adding to it. So then I tried selecting both files, but it will not permit multiple select.
4. I searched for guidelines as to how to upload multiple files, but only found the Front Page, which did not appear to contain Upload Instructions.

It baffles me how to attach more than 1 file. Though best would be a compilable and executable example. Total example size is quite small: 93.4KB, of which 14k is Java: 424 lines of code; the rest is data. I did see the upload file link near Contact Us. If that is to be used, how do I identify what the file contains? No zip (or rar or gz) files there also?
1
+Pie Number of slices to send: Send
 

it was 10 files,



That is NOT a SSCCE. We are not interested in your real application, only code that demonstrates the problem.

424 lines of code; the rest is data.



That is way too many lines of code to look at. Also, the data is irrelevant for your question.

You question is about creating an image of a screen, any screen. So all you need to do create a frame with a single button. In the ActionListener for the button you print the screen. It should be about 10-20 lines of code. That is the point of creating a SSCCE, to simplify the problem. Then once you get this simple demo working you apply the knowledge to your real application to see what you are doing differently.
+Pie Number of slices to send: Send
All is solved. I am posting here to hopefully help others with the same problem. I also want to thank Rob Camick, who goosed me out of my laziness to think more creatively about the situation. The code posted previously and below for copying a screenshot does indeed work. The reason that all of the screenshots were coming out black is that it was being invoked before the screen was actualized then displayed by Java. So the problem became one of finding when the screen had been displayed.

My solution can probably be improved greatly and ant improvements would be happily received.. One might, of course put up an 'isShowing' loop for the entire period of construction, but that seems less good because it involves more execution. I am certain that there are better ways than my solution, but it does work, and only the very last part involves an 'isShowing' loop. The earlier and larger part is covered by a HierarchyListener. Before settling upon HierarchyListener, I tried ComponentListener, but after it failed to work, I found a number of posts online explaining that it will never work, and suggesting a Hierarchy Listener. So early in the construction of the JFrame, the following was executed.

The variable wdisp sits there waiting for the HierarchyListener to fire:

In the code below, main is the JFrame and cPane is the Component to be snapshotted, derived from the ContentPane of the JFrame. A Thread is started, waiting for the HierarchyListener to fire, which it does upon execution of 'setVisible(true)' for the JFrame. But there is still a period during which the ContentPane is still not showing, although it is on its way up. This explains the double loop. And WatchDisplay is:


Why fit in when you were born to stand out? - Seuss. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1008 times.
Similar Threads
How can I reduce the image size of a PNG file
Writing Image onto a WordDoc
How to retrieve file contents using sockets
help me to fix memory leak in ImageIO.write() and ImageIO.read()
Setting the DPI value on a generated PNG image
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 21:37:24.