Reputation: 49
I am try to do an undo button
using stack
so that it would remove the effect applied such as blur
etc , but when i try to pop out
the image
i stored in the stack
something goes wrong , please help me , thanks in advance!.
Applying effect:
if (e.getSource() == btnGrayscale) {
if (buffImage != null) {
Imagesteps.push(buffImage);
ImageEffects ie = new GrayscaleEffect();
buffImage = ie.GrayscaleEffect(buffImage);
Icon icon = new ImageIcon(buffImage);
lblImage.setIcon(icon);
statusBar.setText("Image is now Grayscaled");
}
}
UNDO method:
if (e.getSource() == undoimageitem) {
BufferedImage temp = (BufferedImage) Imagesteps.pop();
Icon icon = new ImageIcon(temp);
lblImage.setIcon(icon);
statusBar.setText("Undo");
}
Upvotes: 0
Views: 394
Reputation: 52185
From the exception you are getting it seems that you are trying to pop from an empty stack. To avoid this error you should ideally, before popping, make sure that the size of the stack is at least greater or equal to 1.
As to why you are getting the exception, the reasons could be many. Two that come to mind are these:
You are re-initialzing/clearing the stack at some point in your code between pushing items and popping them from the stack.
Assuming that, like most buttons, the method is activated by one click, you are double clicking the button, thus causing the event to fire twice, which could lead into the code performing two pop operations which is causing the issue.
Upvotes: 1