0

This is my Main class

public class Gui {
public static void main(String[] args) {

    Model model = new Model();
    Controller controller = new Controller(model);
    View view = new View(model, controller);
}
}

This is my Controller Class

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Controller implements MouseListener {

private Model model = new Model();

public Controller(Model model) {
    this.model = model;
}

public static ActionListener actionListenerCircle = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
            Model.flag = "circle";
    }
};

public static ActionListener actionListenerBox = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        Model.flag = "box";
    }
};

public static ActionListener actionListenerUndo = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        Model.flag = "undo";
    }
};

public static ActionListener actionListenerRedo = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        Model.flag = "redo";
    }
};

@Override
public void mouseClicked(MouseEvent e) {

    if(model.flag.equals("circle")) {
        Circle circle = new Circle(e.getX(), e.getY());
    }

    if(model.flag.equals("box")) {
        Box box = new Box(e.getX(), e.getY());
    }

}

@Override
public void mousePressed(MouseEvent e) {

}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {

}

@Override
public void mouseExited(MouseEvent e) {

}
}

This is my View Class

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class View extends JFrame{

private Model model = new Model();
private Controller controller = new Controller(model);
private Drawing drawing = new Drawing();

public View(Model model, Controller controller) {

    JFrame frame = new JFrame("Assignment1");

    /**
     * Size of the frame
     * */
    frame.setSize(600,400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

    /**
     * Creating buttons to draw shapes
     * */
    JButton circleButton = new JButton("Circle");
    JButton boxButton = new JButton("Box");
    JButton redoButton = new JButton("Redo");
    JButton undoButton = new JButton("Undo");

    /**
     * Creating a Panel in the left to add Buttons in it
     * */
    JPanel leftPanel = new JPanel();
    leftPanel.setBackground(Color.LIGHT_GRAY);
    leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));

    /**
     * Panel where the shapes will be drawn
     * */
    Drawing rightPanel = new Drawing();
    rightPanel.setBackground(Color.white);

    /**
     * Add buttons to the leftPanel
     * */
    leftPanel.add(circleButton,BorderLayout.WEST);
    leftPanel.add(boxButton,BorderLayout.WEST);
    leftPanel.add(undoButton, BorderLayout.WEST);
    leftPanel.add(redoButton, BorderLayout.WEST);

    /**
     * Adding panel to the frame
     * */
    frame.add(leftPanel,BorderLayout.WEST);
    frame.add(rightPanel);

    circleButton.addActionListener(controller.actionListenerCircle);
    boxButton.addActionListener(controller.actionListenerBox);
    

    undoButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            drawing.undo();
        }
    });

    redoButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            drawing.redo();
        }
    });
}
}

This is my Drawing Class

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

public class Drawing extends JPanel implements Command{

    private Model model = new Model();
    private Controller controller = new Controller(model);
    public ArrayList<Shape> redrawCoordinates= new ArrayList<Shape>();

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    Shape points;

    /***
     * This is my Iterator Pattern
     * **/
    Iterator iterator = model.getCoordinates().iterator();

    while(iterator.hasNext()) {
        points = (Shape)iterator.next();
        points.Draw(g2);
    }
}

public Drawing() {
    addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            if(model.flag.equals("circle")) {
                Circle circle = new Circle(e.getX(), e.getY());
                model.addCoordinates(circle);
            }

            if(model.flag.equals("box")) {
                Box box = new Box(e.getX(), e.getY());
                model.addCoordinates(box);
            }
            repaint();
        }
    });
}

/**
 * Below is the Implementation of Command Pattern to support the Undo & Redo Operation
 * */

@Override
public void undo() {
    if(model.getCoordinates().size() > 0) {
        System.out.println("Undo Operation");
        redrawCoordinates.add(model.getCoordinates().get(model.getCoordinates().size()-1));
        model.getCoordinates().remove(model.getCoordinates().size()-1);
        repaint();
    } else {
        JOptionPane.showMessageDialog(null, "There is Nothing Left to Undo!!!");
    }
}

@Override
public void redo() {
    if(redrawCoordinates.size() > 0) {
        System.out.println("Redo Operation");
        Shape toReDraw =redrawCoordinates.get(redrawCoordinates.size()-1);
        model.getCoordinates().add(toReDraw);
        redrawCoordinates.remove(toReDraw);
        repaint();
    } else{
        JOptionPane.showMessageDialog(null, "There is nothing left to Redraw!!!");
    }

}
}

This is Model Class

import java.util.ArrayList;

public class Model {

private static ArrayList<Shape> coordinates;
public static String flag = "";

public Model() {
    coordinates = new ArrayList<Shape>();
}

public void addCoordinates(Shape shape) {
    coordinates.add(shape);
}

public void getCoordinates(int i) {
    coordinates.get(i);
}

public ArrayList<Shape> getCoordinates() {
    return coordinates;
}

}

This is my Shape Interface

public interface Command {

void undo();
void redo();
}

This is my Command Interface

public interface Command {

void undo();
void redo();
}

Whenever I try to make the shapes it works fine, but when I do the undo operation, it doesn't reflect at that particular time, it removes all the shapes that I have drawn but it only reflects when I click on the drawing area, then only all the shapes are removed and I end up getting one shape drawn.

I tried to debug my code, what I could understand was, my repaint() function which is in my undo() function doesn't work.

  • You're recreating the model, view, and controller in your classes. You should create one instance of the model, one instance of the view, and one instance of each of your controller classes. The view needs an instance of the model so it has the information to draw, and your controller classes need an instance of the model and the view so the controller classes can update the model and repaint the view. – Gilbert Le Blanc Mar 17 '21 at 02:57
  • Here's a bit more MVC explanation in a [Stack Overflow answer](https://stackoverflow.com/questions/27657336/mvc-and-jframes/27660945#27660945). – Gilbert Le Blanc Mar 17 '21 at 03:12

0 Answers0