Question: Code in Java pls UndoRedoPainter.java, filling in the undo ( ) method as follows: Your code should get the history stack from the superclass, pop
Code in Java pls
UndoRedoPainter.java, filling in the undo method as follows:
Your code should get the history stack from the superclass, pop off the top Circle object, and then call repaintDont call paintComponent directly the reason is too complicated to explain here.
To test, run UndoRedoPainter.java as an app. Add a red circle, then a green circle, then a blue circle.
If you click the Undo button, what should happen? Now click the Undo button. Is this the result you expect? If not, make sure to go back and debug your code. Using the debugger, will allow you to examine the contents of the history and undoHistory stacks.
Here is the code so far:
package stacklab;
import java.util.;
public class UndoRedoPainter extends Painter
public UndoRedoPainter
setUndoButtonEnabledfalse;
setRedoButtonEnabledfalse;
Called when the user pushes the Undo button.
void undo
Called when the user pushes the Redo button.
@Override
void redo
public static void mainString args
new UndoRedoPaintersetVisibletrue;
package stacklab;
import java.awt.;
import java.awt.event.;
import java.util.;
import javax.swing.;
import javax.swing.event.;
public class Painter extends JFrame implements ActionListener
private Painter outerThis;
private JButton undoBtn;
private JButton redoBtn;
private JButton quitBtn;
private ColorChooser colorChooser;
private Canvas canvas;
private Stack history;
private Stack undoHistory;
private class ColorChooser extends JPanel
JSlider sliders;
SamplerPanel sampler;
ColorChooser
setLayoutnew BorderLayout;
sampler new SamplerPanel;
JPanel west new JPanelnew GridLayout;
sliders new JSlider;
for int i; i; i
JPanel p new JPanel;
paddnew JLabelRGBcharAti;
slidersi new JSlideri : ;
slidersiaddChangeListenersampler;
paddslidersi;
west.addp;
addwest BorderLayout.WEST;
addsampler BorderLayout.CENTER;
Color getColor
int r slidersgetValue;
int g slidersgetValue;
int b slidersgetValue;
return new Colorr g b;
private class SamplerPanel extends JPanel implements ChangeListener
public Dimension getPreferredSize
return new Dimension;
public void stateChangedChangeEvent e
repaint;
public void paintComponentGraphics g
gsetColorgetColor;
gfillRect getWidth getHeight;
private class Canvas extends JPanel implements MouseListener
Canvas
addMouseListenerthis;
public Dimension getPreferredSize
return new Dimension;
public void mouseClickedMouseEvent e
Circle circle new CircleegetPoint colorChooser.getColor;
history.pushcircle;
repaint;
undoBtn.setEnabledtrue;
public void mousePressedMouseEvent e
public void mouseReleasedMouseEvent e
public void mouseEnteredMouseEvent e
public void mouseExitedMouseEvent e
public void paintComponentGraphics g
gsetColorColorWHITE;
gfillRect getWidth getHeight;
for Circle circle: history
circle.paintg;
End of inner class Canvas
Painter
outerThis this;
setResizablefalse;
setDefaultCloseOperationJFrameEXITONCLOSE;
history new Stack;
undoHistory new Stack;
JPanel pan new JPanel;
colorChooser new ColorChooser;
pan.addcolorChooser;
undoBtn new JButtonUndo;
undoBtn.addActionListenerthis;
pan.addundoBtn;
redoBtn new JButtonRedo;
redoBtn.addActionListenerthis;
pan.addredoBtn;
quitBtn new JButtonQuit;
quitBtn.addActionListenerthis;
pan.addquitBtn;
addpan BorderLayout.NORTH;
setCanvasnew Canvas;
addgetCanvas BorderLayout.CENTER;
pack;
public void actionPerformedActionEvent e
if egetSource quitBtn
System.exit;
else if egetSource undoBtn
undo;
else if egetSource redoBtn
redo;
void undo
System.out.printlnUndo not implemented";
void redo
System.out.printlnRedo not implemented";
Stack getHistory
return history;
Stack getUndoHistory
return undoHistory;
void setUndoButtonEnabledboolean b
undoBtn.setEnabledb;
void setRedoButtonEnabledboolean b
redoBtn.setEnabledb;
public static void mainString args
new PaintersetVisibletrue;
public Canvas getCanvas
return canvas;
public void setCanvasCanvas canvas
this.canvas canvas;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
