2014-10-31 60 views
0

我想爲我的繪畫應用程序實現撤消和重做操作。使用ArrayList撤消操作

我創建了類從jpanel擴展,這裏我有arraylist保留我的jpanel上的所有元素。

這是如何工作的,當我添加新的元素(FE我畫與鉛筆工具的東西):

this.elements.add(new PencilElement(this.tool.getPPoint(), this.tool.getCPoint(), this.tool.getColor(), this.tool.getStroke())); 

我想用另一個數組列表把所有元素的副本,當我點擊「取消」按鈕: - >臨時數組列表中的最後一個元素將被刪除 - >基本數組列表的內容(在本例中爲「元素」數組列表)將被替換爲臨時數組列表內容。

如果您有其他想法,請分享

謝謝!

+7

外觀上沒有任何動作api/java/util/Stack.html – bobbel 2014-10-31 12:12:16

+3

撤消的經典模式 - http://en.wikipedia.org/wiki/Command_pattern – Leo 2014-10-31 12:12:42

回答

0

創建使用的Deque(Why should I use Deque over Stack?

堆棧

一個建議是使用堆棧的動作也,所以你根本就從普通動作棧中彈出並推動在撤消堆棧上,和周圍的其他方法重做時。

//建議對當前的解決方案

private Deque<PencilElement> undoStack = new ArrayDeque<PencilElement>(); 

//whenUndo 
undoStack.addFirst(myPencilElement); 

//whenRedo 
elements.add(undoStack.removeFirst()); 


//New suggestion 
//whenUndo 
undoStack.addFirst(elements.removeFirst()); 

//whenRedo 
elements.add(undoStack.removeFirst()); 

也請務必禁用重做當http://docs.oracle.com/javase/7/docs/堆棧

0

當你想做一個重做,那麼你不應該刪除第二個ArrayList中的最後一個元素,因爲你需要它來做重做。