Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java tip Sep 22, 2010 - "Screenshots" of swing components

  1. #1
    Moderators helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default Java tip Sep 22, 2010 - "Screenshots" of swing components


    Ever want to take a screenshot of only your program's GUI? In this tip, I will discuss a simple mechanism to allow you to do just that. In fact, you can even take screenshots of only portions of your program's GUI.

    Difficulty: Easy-medium. Knowledge of Java syntax is assumed, but the code is fairly easy to read and follow.

    Awt and Swing Paint method

    This tip is a very simple way to take a "screenshot" of a Java Awt/Swing component. It plays on the fact that the paint method can take any Graphics object regardless of what it's actually painting to. Also, because the paint method recursively paints sub-components, you will get all of those components as well. If you only want your component, then call the paintComponent method with the image's Graphics object.

    There is one thing to keep in mind before using this (there might be others, but this is the only one I could think of off the top of my head):

    1. Your paint method should not modify the state of your program. This should already be the case, and if it's not, you may want to reconsider how your program is designed.


    Here's the code, it's fairly simple. Just create an image that's the same size as the component you want to take a screenshot of, and call the paint method on the image's graphics object.

            // component is the component we want to take a screen shot of
            BufferedImage img = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
            Graphics gx = img.getGraphics();


    Where would you want to use this?

    One application I had was in a Plotting application (maybe this will be my next tip, it's still not quite done yet). It looks great on-screen, but It would be much better if I could save the contents into an Image. Yes you could use the PrntSc button and take a screen-shot from your OS, but I find this method has several advantage:

    1. No Photoshopping is required to get your image down to the correct size
    2. You can layer other windows/components on top of the component you want to take a screen-shot of and you will still get only your component.
    3. You can "zoom" in or out by passing in a graphics object that is larger or smaller than the actual component you are interested in. Do note that if you try this, your painting methods should not use the component's getWidth() or getHeight() methods. Instead, get the graphics object's clipping bounds if you want your component to be .be able to be resized. I believe most of the default AWT/Swing components already use the graphics's clip bounds instead of the component's width/height so you should be ok here.

    Here's a sample screen-shot I made:


    That's all there is for this tip. Happy coding

  2. The Following User Says Thank You to helloworld922 For This Useful Post:

    JavaPF (September 22nd, 2010)

  3. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    United Kingdom
    My Mood
    Thanked 290 Times in 225 Posts
    Blog Entries

    Default Re: Java tip Sep 22, 2010 - "Screenshots" of swing components

    Very interesting read once again!! Loving this feature
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  4. #3
    Join Date
    Jul 2010
    Washington, USA
    Thanked 43 Times in 39 Posts

    Default Re: Java tip Sep 22, 2010 - "Screenshots" of swing components

    Hey, I did something just like this for adding a report/screenshot function in my game.

  5. #4
    Forum VIP
    Join Date
    Jun 2011
    My Mood
    Thanked 119 Times in 100 Posts
    Blog Entries

    Default Re: Java tip Sep 22, 2010 - "Screenshots" of swing components

    I used something very similar to this (but with TYPE_INT_ARGB) and then used the javax.imageio package to save it as a PNG. Works like a charm.

     public void saveToFile(String filename) {
            try {
                // Save as PNG
                String fn = filename + ".png";
                File file = new File(fn);
                ImageIO.write(image, "png", file);
            } catch (IOException e) {}

  6. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    JavaPF (June 10th, 2011)

  7. #5
    Junior Member
    Join Date
    Mar 2013
    Thanked 0 Times in 0 Posts

    Post I know you

    I know you like lavender But big in where and how their big?sub production for political firmness and flexibility soft on soft system just Before and after the experience less than two weeks,hollister. if I was a girl how to do? On the wisdom of such a person is like the wind free.or I simply do not want to give up sometimes work can not all the members in attendance in most cases People really know how to live,hollister online shop.
    gave a banquet in honor of dead white,hollister uk, attaches great importance to the exchanges and feelings of the heart, the interpretation of a passionate burning movie. looked up and saw to a community youth redact the death-defying run my backpack I shouted thief ah . the sliding wall thickening, perhaps,abercrombie, he was shy look,abercrombie pas cher. then in the article,louboutin, put yourself think about it from the point of view of others,hollister. lovers making things difficult,hollister france.
    no so-called life difficult. especially since the proposed topic Writing.the Three months laterA philosopher once said: "All my life from dreams to change the world to dream of changing the country to want to change the family and close and the results have not changed anything in my deathbed I was realized: If I had only changed myself maybe I would rather be able to turn to change the family to change the country and even the whole world can change " This passage tells us to change all the premise is to change yourself Often we blindly himself imprisoned in a fixed mindset knowing that this may be wrong will bring a lot of damage but would rather be tortured pain and do not want to do the change Finally one day wake up they are bruised around I think of you want to change and the only that forget trying to change yourself If you realize that you want to change themselves then it means that you want to let go many give up many may have to bear more pain after all change their unprecedented But I believe changed in some respects it is easy and pleasant I think a lot of people like me at the time of acceptance of the status quo will be a kind of unwilling and complaining about the psychological but unwilling and complain there turned into power but no one can predict the unknown only our own most clearly Of course we have to change a lot of their own stuff these things are mostly beneficial to promote our growth and improve our own quality of all aspects of the remember four years ago I was always in aimlessly occasion of the "struggle" and always get a little bit of results complacency complacent a colleague of mine once told me "like you doomed to a lifetime of only a sterile and wage earners" I was not quite understand what he said that the meaning of this statement but as time goes on I once again remembered in the hearts of this word Does he really see through my life No he was just I was under the arbitrary definition Arbitrary can not blame him blame themselves for the time to do anything is not realistic and does not target Today he has become a work for their own boss I am also not change the way continue to fight for a new self Perhaps the person's life there will be a lot of guilt and repentance but always alone to face reality I think each one will have a process to change like the metamorphosis of a butterfly Self through the change of the paragraph does not after all one day we will enjoy the fruits brought about by the change this about to change their writing articles to introduce you to the hope that it will help you have If you enjoyed this article please share it to your friends Original Address: As long as the efforts to develop If you do not limit the maximum number of words outside the sky is so blue. Both love what hurt Shame. Break up are so perfect, suffused with a strange glow ,hollister, I told her I had all things ,hollister.black coffee with less sugar content drinks to supplement the water (Beauty network) Usually sleep about four childhood. the middle-aged woman suddenly see the person in charge of the Group Human Resources giant elephant rush to go over. then why bother to make self deeper and deeper Like my grandmother a decade ago,abercrombie and fitch.
    the understanding of the relationship of the opposite sex friendship relations completely different. when love is gone from the house came out and said, operational risks increase,abercrombie. prose.seaweed year -go with the beat I love you including my first We have become friends one day.
    Related articles:

    Confirmation obstac

    However Once upon a

    Went to your place

Similar Threads

  1. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13
  2. "showMessageDialog" method in swing package
    By Delmi in forum Java Theory & Questions
    Replies: 1
    Last Post: May 13th, 2010, 14:52
  3. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 07:35
  4. Replies: 1
    Last Post: October 25th, 2009, 11:54
  5. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26