undo, redo and delete function
hi there,
I need to write these 3 functions, but i got stuck at redo and delete. Redo is showing error when there is nothing to redo and i don't know how to write delete function. Thank you
undo
Code:
package zadani21.commands;
import zadanie21.Command;
import zadanie21.Main;
import zadanie21.MyCanvas;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.KeyStroke;
public class Undo extends AbstractAction {
private MyCanvas myCanvas;
public Undo(MyCanvas myCanvas) {
this.myCanvas = myCanvas;
this.putValue(NAME, "Undo");
this.putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl Z"));
this.putValue(SMALL_ICON, new ImageIcon(Main.class.getResource("/icons/Undo24.gif")));
}
public void actionPerformed(ActionEvent e) {
if (!myCanvas.commands.isEmpty()) {
Command cmd = myCanvas.commands.pop();
cmd.undo();
myCanvas.undoneCommands.add(cmd);
myCanvas.repaint();
}
else
System.out.println();
}
}
redo
Code:
package zadani21.commands;
import zadanie21.Command;
import zadanie21.Main;
import zadanie21.MyCanvas;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.KeyStroke;
public class Redo extends AbstractAction {
private MyCanvas myCanvas;
public Redo(MyCanvas myCanvas) {
this.myCanvas = myCanvas;
this.putValue(NAME, "Redo");
this.putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl Y"));
this.putValue(SMALL_ICON, new ImageIcon(Main.class.getResource("/icons/Redo16.gif")));
}
public void actionPerformed(ActionEvent e) {
//if (!myCanvas.commands.isEmpty()) {
Command cmd = myCanvas.undoneCommands.pop();
cmd.execute();
myCanvas.commands.add(cmd);
myCanvas.repaint();
//}
// else
System.out.println();
}
}
Re: undo, redo and delete function
Quote:
Originally Posted by
ivan12yu2000
Redo is showing error when there is nothing to redo
That's probably because the check to see if the command stack is empty has been commented out. Are you sure this is your code?
Quote:
i don't know how to write delete function.
Perhaps if you explained what this program was supposed to do, and what the delete method was supposed to do, we might be able to suggest something.
Why do we never have time to do it right, but always have time to do it over?
Anon.