Results 1 to 8 of 8

Thread: Saving JFrame

  1. #1
    jralexander is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default Saving JFrame

    Hi,
    I am wondering if it is possible to save a JFrame as a picture file? If so some help on doing this would be great as I am unsure if you can do this let alone know where to begin. Thanks in advance for the help.

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Yes,you can do a screenshot ,try to find on google how to make a screenshot in java in special area on desktop

  3. #3
    jralexander is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default

    I've looked on google on how to do this with no luck if you have a link to a guide or something i would greatly appreciatte it if you could post it. THnkas

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

    Default

    I haven't done this but you can draw the JFrame I believe using a BufferedImage's Graphics object and the JFrame's paint command and then save this as a JPG. Again, I've not done this but I think I've seen it somewhere.

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

    Default

    This program will capture a JFrame except not the title bar, and I haven't yet figured out how to do that:
    Java Code:
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class PaintJFrame
    {
      private static final Dimension MAIN_SIZE = new Dimension(400, 300);
      private JPanel mainPanel = new JPanel();
      
      public PaintJFrame()
      {
        JButton makeImageBtn = new JButton("Make Image");
        makeImageBtn.addActionListener(new BtnListener());
        mainPanel.add(makeImageBtn);
        mainPanel.setPreferredSize(MAIN_SIZE);
      }
      
      public JComponent getPanel()
      {
        return mainPanel;
      }
      
      private class BtnListener implements ActionListener
      {
        public void actionPerformed(ActionEvent arg0)
        {
          JFrame win = (JFrame)SwingUtilities.getWindowAncestor(mainPanel);
          Dimension size = win.getSize();
          //BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
          BufferedImage image = (BufferedImage)win.createImage(size.width, size.height);
          Graphics g = image.getGraphics();
          win.paint(g);
          g.dispose();
          try
          {
            ImageIO.write(image, "jpg", new File("MyFrame2.jpg"));
          }
          catch (IOException e)
          {
            e.printStackTrace();
          }
        }
      }
      
      private static void createAndShowGUI()
      {
        JFrame frame = new JFrame("PaintJFrame Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new PaintJFrame().getPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowGUI();
          }
        });
    
      }
    }
    Also check out camickr's ScreenImage class which you can find here.

  6. #6
    jralexander is offline Member
    Join Date
    Nov 2008
    Posts
    11
    Rep Power
    0

    Default

    I'll try the code you posted I am making a program that allows the user to create a family tree and I just need to save the picture of the tree so this should work I'll try it out and post the results.

  7. #7
    timkd127 is offline Member
    Join Date
    Dec 2008
    Posts
    41
    Rep Power
    0

    Default

    in the top right corner of ur keyboard(PC) theres a button that says

    Prt Sc
    Sys Rq

    when u have the JFrame up hit that button open up paint hit the edit button and then copy. then u can save it as .jpg or .gif or wutever else u like.

  8. #8
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    Quote Originally Posted by timkd127 View Post
    in the top right corner of ur keyboard(PC) theres a button that says

    Prt Sc
    Sys Rq

    when u have the JFrame up hit that button open up paint hit the edit button and then copy. then u can save it as .jpg or .gif or wutever else u like.
    I think he means he wants a save option so the user doesn't have to do that because the save command would automatically do it for them

Similar Threads

  1. Saving changes done through a program
    By xcallmejudasx in forum New To Java
    Replies: 0
    Last Post: 12-02-2008, 05:53 PM
  2. Saving data...?
    By easyRyder in forum New To Java
    Replies: 8
    Last Post: 07-15-2008, 04:14 AM
  3. Saving Values
    By Sysem in forum New To Java
    Replies: 10
    Last Post: 06-02-2008, 07:29 PM
  4. Saving application settings
    By mwildam in forum New To Java
    Replies: 0
    Last Post: 11-16-2007, 03:30 PM
  5. Saving Variables
    By Fish in forum New To Java
    Replies: 6
    Last Post: 06-25-2007, 09:20 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
  •