gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:25:50 26 4
gpt4 key购买 nike

我使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com