• 0

[JAVA]how to implement undo/redo?


Question

6 answers to this question

Recommended Posts

  • 0

Keep a collection of some kind (maybe a Vector) of things that have been done. Then also keep an index of the current step (i.e. if they've undone something, it would go back in the list until they added/changed something and then it would go to the end again).

Link to comment
Share on other sites

  • 0

I'd agree with jordan. A vector is your best option.

Every time the user modifies the image, add a record to the vector containing a copy of the image at this stage. Once the vector gets over a certain size (the undo limit), then also remove the first record from the vector whenever you add a record so as to keep it at a static size.

Then when the user presses undo, you revert to the previous item in the vector. If he presses redo, jump to the next item. Once he starts working on the image again, then you remove any records that come after the currrent record and start adding to the vector again.

I could give code if it would be useful.

Link to comment
Share on other sites

  • 0

Also consider to use the undo redo framework of Java Swing. All commands have to provide a code path for applying a effect or change, and one for reversing the effect. So in the apply code path you need to save any information that is needed to reverse the change.

Link to comment
Share on other sites

  • 0

It's the Command design pattern. If you think about it, what you'd do for each task is pass Command objects, that store the values in it, and everytime you perform a task you push make that command, execute the command by passing it to the respective method, and then you push that onto a stack, then when a next action is performed you do the same, to reverse the effect, you pop the undo stack, and grab that command object, process it, then push it onto the redo stack.

Link to comment
Share on other sites

  • 0

Implementing Undo/Redo in Java is quite easy when using the UndoManager related classes from the Java framework.

I have written a kind of tutorial on Undo/Redo in Java that explains the simple concepts.

Don't worry about Vectors or Command patterns - it's all included for free in the Java framework.

In the case of an image editor you might want to concentrate on some memory management mechanism that buffers your undo information on disk (as PhotoShop does), because you probably will end upwith a copy of a part or all of your image for every undoable edit step.

Cheers,

Dominik

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.