Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8581503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:04:03+00:00 2026-06-11T21:04:03+00:00

I am working on a white board project and I encountered a problem when

  • 0

I am working on a white board project and I encountered a problem when implementing the Save function.

Here is how I implement the draw function

Graphics2D g2d = (Graphics2D) frm.getGraphics();
g2d.setColor(Current_Color);
Line2D p2d = new Line2D.Double(StartPoint.getX(),StartPoint.getY(), e.getX() 
     + Xoffset, e.getY() + Yoffset);
g2d.setStroke(new BasicStroke(Integer.parseInt(choice_size.getSelectedItem())));
g2d.draw(p2d);

I am using JFileChooser for the file dialog

            int returnVal = saveFileChooser.showSaveDialog(frm);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File currentDir = saveFileChooser.getCurrentDirectory();
                String fileName = saveFileChooser.getSelectedFile()
                        .getName();
                String savePath = currentDir + "\\" + fileName + ".jpg";

                try {
                    ImageIO.write(<image>,<suffix>,<file>);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }

There is no method like Frame.getImage() for JFrame, I am wondering how can I save what I draw on the JFrame as an image ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:04:04+00:00Added an answer on June 11, 2026 at 9:04 pm

    You need to paint the frame’s content to a BufferedImage first. Try something like…

    Container content = frm.getContentPane();
    BufferedImage img = new BufferedImage(container.getWidth(), container.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = img.createGraphics();
    
    content.printAll(g2d);
    
    g2d.dispose();
    

    Once you have that, you can use the ImageIO.write method, passing the img to it.

    UPDATE

    So, I did a really quick test…

    I started out with this background image…

    background

    Which I loaded into my frame and laid a JLabel ontop

    Frame

    And then saved to a file…

    Output

    All of which worked fine.

    This is the code that I used.

    public class TestSaveFrame extends JFrame {
    
        public static void main(String[] args) {
    
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
                    new TestSaveFrame();
                }
            });
        }
    
        public TestSaveFrame() {
    
            setTitle("Save me");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLayout(new BorderLayout());
    
            BackgroundPane pane = new BackgroundPane();
            pane.setLayout(new GridBagLayout());
    
            JLabel label = new JLabel("I'm sitting on top");
            label.setFont(label.getFont().deriveFont(Font.BOLD, 24f));
            label.setForeground(Color.WHITE);
    
            pane.add(label);
    
            add(pane);
    
            pack();
            setLocationRelativeTo(null);
            setVisible(true);
    
            pane.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
    
                    if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1) {
                        Container content = getContentPane();
                        BufferedImage img = new BufferedImage(content.getWidth(), content.getHeight(), BufferedImage.TYPE_INT_RGB);
                        Graphics2D g2d = img.createGraphics();
                        content.printAll(g2d);
                        g2d.dispose();
    
                        try {
                            ImageIO.write(img, "png", new File("C:/PrintMe.png"));
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            });
    
        }
    
        public class BackgroundPane extends JPanel {
    
            private Image background = null;
    
            public BackgroundPane() {
                try {
                    background = ImageIO.read(getClass().getResource("/MT015.jpg"));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
    
            @Override
            public Dimension getPreferredSize() {
                return background == null ? new Dimension(200, 200) : new Dimension(background.getWidth(this), background.getHeight(this));
            }
    
            @Override
            protected void paintComponent(Graphics g) {
    
                super.paintComponent(g);
                if (background != null) {
                    int x = (getWidth() - background.getWidth(this)) / 2;
                    int y = (getHeight() - background.getHeight(this)) / 2;
    
                    g.drawImage(background, x, y, this);
                }
            }
        }
    }
    

    Without an example of the work flow, it’s going to be tough to work out where you’re going wrong.

    I should note that I use printAll over paint because I’ve had issues with doing this using paint recently (throwing exceptions and the like)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While working on my final project for my AS/400 Course, I encountered this problem
Here is my working page . I attached buttons to white bishop and you
So while working through an implementation of OAuth using Django-Piston I encountered the error
While working a project tonight, I ended up using one .js resource file for
I'm working on a job board in Codeigniter PHP + jQuery where employers enter
I'm working on a program for class that involves solving the Chinese Postman problem
So I'm trying to implement a recursive function that generates the entire game tree
I was just merrily working on a CakePHP site and suddenly... white screen of
I am working on a project which needs me to configure the gpio pins
I'm working on a checkers game in java. I'm representing my board like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.