java - 有没有办法在 imageView 上实现重做和撤消

标签 java javafx

我使用 slider 接受输入并在颜色调整的帮助下在 ImageView 上应用效果

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());
    }

最佳答案

据我所知,没有简单的内置机制可以做你想做的事情。为了撤消和重做应用的颜色调整,您需要将颜色调整的副本保存到 ArrayDeque 之类的文件中。要撤消和重做,您可以在撤消和重做双端队列之间移动调整并应用调整。

一个简单的保存函数会将当前调整保存到撤消双端队列:

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()
}

然后在每个调整函数中,更改 colorAdjust 后调用保存,以便最近的更改可用于重做:

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

然后撤消和重做将在双端队列之间移动调整并根据需要设置颜色调整。

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());
    }
}

这应该给你基本的想法。当然,您需要处理撤消为空时的情况,以不应用颜色调整或从 ImageView 中删除效果。

这只是一种方法。 Swing 提供了一个 UndoManager,您可以在其中为要应用的每个操作(例如 UndoColorAdjustment())创建一个类,并处理管理撤消和重做列表。您可以使用该 UndoManager 或创建自己的 UndoManager。否则,我这里有一个简单直接的方法。

关于java - 有没有办法在 imageView 上实现重做和撤消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62150859/

相关文章:

JavaFX FXML-SceneBuilder-图像可调整大小

java - 将图标按钮添加到 ControlsFX TableFilter 的列标题

java - 如何在Java中向下转换数组?(多态)

java - 是否可以在 Java 中按值传递数组(而不是引用)?

java - 从 Java 8u72 开始,向 ComboBox#box.getSelectionModel()#selectedItemProperty() 添加监听器会导致异常

java - 如何停止 "JavaFX Application Thread"

java - 将调用哪个方法?

java - 如何将 HBase 的扫描限制为仅与 MapReduce 作业相关(未过滤)的区域

java - 来自java大脑的Struts 2简单应用

java - 如何使用 PixelReader 的 getPixels() 方法?