Java Language - Undo and Redo functions

Asked

Viewed 347 times

-2

I’m doing a project where I develop functions to apply effects to photos (such as Contrast, Vignette, Sepia...).

As I apply effects to photos, the various states of the photos are being stored in an image array.

In this same project, I need two functions, Undo and Redo. Undo will be used to obtain the photo with the previously applied effect and Redo will be used to obtain the last applied effect.

Someone can help me in the development of these functions (Undo and Redo)?

Thank you.

Here’s what I got in class:

class Historico {

    private ColorImage [] VCI;
    private int atual;
    boolean undo = false;
    private int undos;
    private int redos;

    public Historico(ColorImage [] VCI) {
        this.VCI = VCI;
        atual = 0;
        undos = 0;
        redos = 0;
    }

    static ColorImage copy (ColorImage CI) {
        ColorImage NCI = new ColorImage(CI.getWidth(),CI.getHeight());
        for(int x = 0; x < CI.getWidth(); x++){
            for(int y = 0; y < CI.getHeight(); y++){
                Color c = CI.getColor(x,y);
                NCI.setColor(x,y,c);
                }
            }
        return NCI;
        } 

    void saveImage(ColorImage CI) {
        VCI [atual] = copy(CI);
        atual++;
        undo = false;
        undos = 0;
        redos=0;
    }

    public void undo() {
        ColorImage CI = copy(VCI[atual-2-undos]);
        VCI[atual] = CI;
        atual = atual + 1;
        undo = true;
        undos = undos + 2;
    }

    public void redo() {
        if(undo = true) {
            ColorImage CI = copy(VCI[atual-2-redos]);
            VCI[atual] = CI;
            atual = atual + 1; 
            undos--;
            if(undos<0){
                undos = 0;
                undo = false;
            }
            redos = redos + 2;
        }
    }   
}
  • 1

    Have you ever heard of the design pattern memento?

  • Hello Victor Stafusa. I haven’t heard of it. I’ve already done the Undo function. My Redo function I can apply it twice but the third one stops working (nothing happens at all), can you help me? If necessary I copy the code I already have of the 2 functions. Thank you

  • Yes, it is recommended that you post your code: https://pt.meta.stackoverflow.com/a/5484/132

  • Okay, I’m sorry. I just edited the question and there’s the code I have in the class. What is malfunctioning is the Redo function (I can use it twice but the third time it stops working (nothing happens))

2 answers

1


You can use two lists as if they were stacks, one for the "undo" and one for the "redo". Also, when using lists instead of arrays, things will become much simpler to work with.

import java.util.List;
import java.util.ArrayList;

public class Historico {

    private final List<ColorImage> undos;
    private final List<ColorImage> redos;
    private ColorImage atual;

    public Historico() {
        this.undos = new ArrayList<>();
        this.redos = new ArrayList<>();
        this.atual = emBranco();
    }

    private static ColorImage emBranco() {
        return new ColorImage(256, 256);
    } 

    private static ColorImage copy(ColorImage ci) {
        ColorImage nci = new ColorImage(ci.getWidth(), ci.getHeight());
        for (int x = 0; x < ci.getWidth(); x++){
            for (int y = 0; y < ci.getHeight(); y++) {
                Color c = ci.getColor(x, y);
                nci.setColor(x, y, c);
            }
        }
        return nci;
    } 

    public void saveImage(ColorImage ci) {
        undos.add(atual);
        redos.clear();
        atual = ci;
    }

    public ColorImage undo() {
        if (!undos.isEmpty()) {
            redos.add(atual);
            atual = undos.remove(undos.size() - 1);
        }
        return atual;
    }

    public ColorImage redo() {
        if (!redos.isEmpty()) {
            undos.add(atual);
            atual = redos.remove(redos.size() - 1);
        }
        return atual;
    }

    public ColorImage getAtual() {
        return atual;
    }
}
  • Hello Victor Stafusa! Helped a lot! Thank you very much. My compliments.

0

You have reported that the states of the photos with each modification are stored in an image array, correct? I believe that your work would be simplified if, instead of using a vector, you used a double-chained list (Linkedlist). As it has references to both the previous and the later state of your image, the implementation of its Redo and Undo functions could be simplified only to the pointer movement that indicates the current position in your list through the Previous and Next methods of a Listiterator.

  • Hello Marcos Andrade. Thanks for your collaboration. It turns out I haven’t yet learned how to use that double-chained list, but I believe it would be a good solution.

  • I understand. Are you aware of the operating logic of a chained list? If so, it is not too difficult to manually assemble a double-chained list, which works quite simply and would solve your problem. I can help you with that if you’re interested. If you’re in college, it’s kind of interesting because it usually goes down a lot in data structures.

  • Marcos, using the code I entered (I edited the question) still thinks it would be simpler ?

  • I believe so. Honestly, the biggest advantage you could get out of this solution would be semantic improvement in your code. It has "version control" variables (undos and redos) that are unnecessary if you use a more appropriate data structure, and this can cause major difficulties if you want to expand the code further by adding more functionality or using it as part of another project. If you do not plan to expand the code further, it is OK to keep it that way, but I do consider this a certain gambit... haha

  • Thank you for your attention Marcos. I do not intend to expand more code, by the way Marcos this class is just the end of my project. This is what I am being asked to do, because I started a short time ago. So, with the code I wrote, what I ask myself to modify for the Redo function to work Marcos?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.