nikhil gupta
nikhil gupta

Reputation: 5

is there a way to implement redo and undo on imageView

I have used a slider to take input and apply effects on the Image View with help of Color Adjust

public void adjustBrightness() {
        colorAdjust.setBrightness(brightnessSlider.getValue());
        imageView.setEffect(colorAdjust);
        addDrawOperation(imageView.getImage());
    }

    public void adjustSaturation() {
        colorAdjust.setSaturation(saturationSlider.getValue());
        imageView.setEffect(colorAdjust);
        addDrawOperation(imageView.getImage());
    }

    public void adjustContrast() {
        colorAdjust.setContrast(contrastSlider.getValue());
        imageView.setEffect(colorAdjust);
        addDrawOperation(imageView.getImage());
    }

    public void adjustHue() {
        colorAdjust.setHue(hueSlider.getValue());
        imageView.setEffect(colorAdjust);
        addDrawOperation(imageView.getImage());
    }

Upvotes: 0

Views: 165

Answers (1)

maroc
maroc

Reputation: 466

There is no simple built in mechanism to do what you want as far as I know. In order to undo and redo the applied color adjustments, you will need to save a copy of the color adjustment to something like an ArrayDeque. To undo and redo you move the adjustments between the undo and redo double ended queues and apply the adjustments.

A simple save function would save the current adjustment to the undo deque:

private Deque<ColorAdjustment> undos = new ArrayDeque<>();
private Deque<ColorAdjustment> redos = new ArrayDeque<>();

public void saveColorAdjustment(ColorAdjust save) {
    ColorAdjust copy = new ColorAdjust(save.getHue(), save.getSaturation(), save.getBrightness(), save.getContrast());
    undos.addLast(copy)
    // clear any redos since we added a new undo
    redos.clear()
}

Then in each of your adjust functions, call save after changing colorAdjust so that the most recent change will be available for redo:

    public void adjustHue() {
        colorAdjust.setHue(hueSlider.getValue());
        saveColorAdjustment(colorAdjust);
        imageView.setEffect(colorAdjust);
        addDrawOperation(imageView.getImage());
    }

Then undo and redo will move adjustments between the deques and set the color adjustment as needed.

public void undoColorAdjustment() {
    if (undos.size() > 0) {
        // move last undo (which is current applied adjustment) to redo
        redos.addFirst(undos.removeLast())
        // apply last color effect
        imageView.setEffect(undos.getLast());
        addDrawOperation(imageView.getImage());
    }
}

public void redoColorAdjustment() {
    if (redos.size() > 0 ) {
        // move first redo to undos
        undos.addLast(redos.removeFirst());
        // apply last color effect
        imageView.setEffect(undos.getLast());
        addDrawOperation(imageView.getImage());
    }
}

That should give you the basic idea. Of course you will need to handle the case when undos is empty to apply no colorAdjustments or remove effects from the image view.

This is just one way to do it. Swing provides an UndoManager where you create a class for each action (e.g. UndoColorAdjustment()) that you want to apply and it handles managing the undo and redo lists. You could possibly use that UndoManager or create your own. Otherwise, what I have here is a simple straight forward way to go.

Upvotes: 0

Related Questions