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 8260503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:06:53+00:00 2026-06-08T03:06:53+00:00

Ok my problem is simple: i make Paint application, and i would like to

  • 0

Ok my problem is simple: i make Paint application, and i would like to provide some kind of “undo” functionality. I read about undo managers and so on, but this approach seems to be difficult to understand for me so i decided to design my own. My idea is simple: since i draw on JPanel i will just save its current content just before any drawing operation is made. And undo button will just restore previous one.

The trouble is: I am unable to “restore” the saved image and place it on the JPanel. I am buffled since i have already implemented “saving” the current image on harddrive and also “opening” arbitrary image from harddrive. Both of those work perfectly fine.

Unfortunately trying the exactly the same approach for my “undo” system does not work.

Look at the code: (to simplify, i manually ‘save’ the current picture -> for testing purposes)

public class PictureEdit { //class for saving the current JPanel drawing for undo
    public BufferedImage saveBI;
    public Graphics2D saveG2D;
}

public void actionPerformed(ActionEvent e) { //action for "UNDO" button, not working
    //drawingField - instance of JPanel
    //pe - instance of PictureEdit class
    drawingField.setBufferedImg(drawingField.pe.saveBI);
    drawingField.updateArea(drawingField.getBufferedImg());            
}

public void actionPerformed(ActionEvent e) { //action for manual "save" (for undo)
    drawingField.pe.saveBI=drawingField.getBufferedImg();
    drawingField.pe.saveG2D=(Graphics2D)drawingField.getBufferedImg().getGraphics();

}

Now the example of my working solution but related to woking witk files on HDD:

public void actionPerformed(ActionEvent e){ //Opening file from harddrive - works fine
    JFileChooser jfc = new JFileChooser();
    int selection = jfc.showOpenDialog(PaintUndo.this);
    if (selection == JFileChooser.APPROVE_OPTION){                
        try {
           drawingField.setBufferedImg(ImageIO.read(jfc.getSelectedFile()));
           drawingField.updateArea(drawingField.getBufferedImg());

        } catch (IOException ex) {
           Logger.getLogger(PaintUndo.class.getName()).log(Level.SEVERE, null, ex);
           JOptionPane.showMessageDialog(PaintUndo.this, "Could not open file");
        }
    }                 
}


public void actionPerformed(ActionEvent e) { //Saving to harddrive - works fine
     int n = JOptionPane.showConfirmDialog(PaintUndo.this, "Are you sure you want to save?", "Saving image...", JOptionPane.OK_CANCEL_OPTION);
     if (n == JOptionPane.YES_OPTION){
        JFileChooser jfc = new JFileChooser();
        int nn = jfc.showSaveDialog(PaintUndo.this);
        if (nn == JFileChooser.APPROVE_OPTION){                       
           File saveFile = new File(jfc.getSelectedFile()+".bmp");
           try {
              ImageIO.write(drawingField.getBufferedImg(), "bmp", saveFile);
           } catch (IOException ex) {
              Logger.getLogger(PaintUndo.class.getName()).log(Level.SEVERE, null, ex);
              JOptionPane.showMessageDialog(PaintUndo.this, "Error while saving file");
           }                       
        }
     }
 }

Just in case someone is curious: updateArea function (member of extended JPanel class that represents mentioned drawingField):

public void updateArea(BufferedImage img){ //it is just resizing the current picture (if necessary) and then assigns the new Graphics.
    area.height=img.getHeight();
    area.width=img.getWidth();
    this.setPreferredSize(area);
    g2d = (Graphics2D)this.getGraphics();
}

Basically the procedure is exactly the same, and working with files is ok while i cannot save and restore image when im operating on variables… What could be wrong? any ideas? When i use Undo/UndoSave buttons, absolutely nothing changes in my JPanel (drawingField)

Any help appreciated

  • 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-08T03:06:54+00:00Added an answer on June 8, 2026 at 3:06 am

    In the code you’ve shown, you are copying your reference to the image, but both references still point at the same image object – if you perform any action which doesn’t reassign the reference (=) it will be reflected in both references! In order to save an old version of the image, you need to copy the actual object, not just the reference.

    I do recommend using Java’s established undo management approach but if you want to continue with your own, these are methods you should look at:

    //BufferedImage image;
    Raster raster = image.getData(); // save
    image.setData(raster); // restore
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a school project, I need to make a simple paint application that can
Trying to make simple minesweeper game in python, but have one problem. I have
This one seems to be a simple problem, but I can't make it work
I have to make a simple layout in android but have problem with the
I'm trying to make a simple Python-based HTML parser using regular expressions. My problem
Yo. I'm trying to make a simple login system in PHP and my problem
I have written a C++ application that uses the Qt framework. I would like
I am working on an application that involves some gis stuff. There would be
I am trying to make a simple phone directory program in windows console application.
I have a rather strange problem. I have a very simple application that reads

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.