首页 > 解决方案 > 无法重做和撤消为形状(圆形和框)实现的操作以实现设计模式

问题描述

这是我的主要课程

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

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

这是我的控制器类

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) {

}
}

这是我的视图类

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();
        }
    });
}
}

这是我的绘画课

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!!!");
    }

}
}

这是模型类

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;
}

}

这是我的形状界面

public interface Command {

void undo();
void redo();
}

这是我的命令界面

public interface Command {

void undo();
void redo();
}

每当我尝试使形状正常工作时,但是当我执行撤消操作时,它不会在那个特定时间反映,它会删除我绘制的所有形状,但它只会在我单击绘图区域时反映,然后只删除所有形状,我最终得到一个形状。

我试图调试我的代码,我能理解的是,我的 undo() 函数中的 repaint() 函数不起作用。

标签: javaswinguser-interfaceactionlistenerpaintcomponent

解决方案


推荐阅读