Java Language - Undo and Redo Functions

-2

I'm doing a project where I develop functions to apply effects to photos (like Contrast, Vignette, Sepia ...).

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

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

Can anyone help me with the development of these functions (Undo and Redo)?

Thank you.

Here's what I have in the 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;
        }
    }   
}
    
asked by anonymous 14.12.2017 / 19:53

2 answers

1

You can use two lists as if they were stacks, one for "undo" and one for "redo". Also, using lists instead of arrays will make things much easier 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;
    }
}
    
15.12.2017 / 03:51
0

You have reported that the states of the photos with each modification are stored in an image vector, correct? I believe your work would be simplified if, instead of using a vector, you used a double-chained list ( LinkedList ). Because it has references to both the back and front state of its image, implementing its Redo and Undo functions could be simplified only by moving the pointer that indicates the current position in your list by using the Previous and Next methods of a ListIterator .

    
14.12.2017 / 20:19