使用命令模式设计一个图形绘制系统,该系统可以绘制矩形、椭圆、三角形等图形,恢复和撤销功能。绘制类图并编程实现。java实现
时间: 2024-06-08 15:05:36 浏览: 15
首先,我们需要定义一个命令接口,该接口包含了执行和撤销两个方法:
```java
public interface Command {
void execute();
void undo();
}
```
然后,我们定义具体的绘制命令类,如矩形绘制命令类、椭圆绘制命令类、三角形绘制命令类等。这些类实现了上述命令接口,用于执行具体的绘制操作和实现恢复和撤销功能。
接下来,我们定义一个图形绘制器类,该类负责管理所有的绘制命令,包括执行、撤销和恢复上一步操作等功能。该类可以被其他业务逻辑调用,完成图形绘制的任务。
具体代码实现如下:
```java
// 定义绘制命令接口
public interface Command {
void execute();
void undo();
}
// 定义矩形绘制命令类
public class RectangleCommand implements Command {
private final Graphics graphics;
private final int x;
private final int y;
private final int width;
private final int height;
public RectangleCommand(Graphics graphics, int x, int y, int width, int height) {
this.graphics = graphics;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
@Override
public void execute() {
graphics.drawRect(x, y, width, height);
}
@Override
public void undo() {
graphics.clearRect(x, y, width, height);
}
}
// 定义椭圆绘制命令类
public class EllipseCommand implements Command {
private final Graphics graphics;
private final int x;
private final int y;
private final int width;
private final int height;
public EllipseCommand(Graphics graphics, int x, int y, int width, int height) {
this.graphics = graphics;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
@Override
public void execute() {
graphics.drawOval(x, y, width, height);
}
@Override
public void undo() {
graphics.clearRect(x, y, width, height);
}
}
// 定义三角形绘制命令类
public class TriangleCommand implements Command {
private final Graphics graphics;
private final int[] xPoints;
private final int[] yPoints;
public TriangleCommand(Graphics graphics, int[] xPoints, int[] yPoints) {
this.graphics = graphics;
this.xPoints = xPoints;
this.yPoints = yPoints;
}
@Override
public void execute() {
graphics.drawPolygon(xPoints, yPoints, 3);
}
@Override
public void undo() {
graphics.setColor(Color.WHITE);
graphics.fillPolygon(xPoints, yPoints, 3);
graphics.setColor(Color.BLACK);
}
}
// 定义图形绘制器类
public class GraphicsDrawer {
private final List<Command> commandList = new ArrayList<>();
private final Stack<Command> undoStack = new Stack<>();
public void addCommand(Command command) {
commandList.add(command);
command.execute();
undoStack.clear();
}
public void undo() {
if (!commandList.isEmpty()) {
Command command = commandList.remove(commandList.size() - 1);
command.undo();
undoStack.push(command);
}
}
public void redo() {
if (!undoStack.isEmpty()) {
Command command = undoStack.pop();
commandList.add(command);
command.execute();
}
}
}
```
在上面的代码中,我们定义了三个具体的绘制命令类,分别是矩形绘制命令类、椭圆绘制命令类和三角形绘制命令类。这些类实现了命令接口,用于执行具体的绘制操作和实现恢复和撤销功能。
我们还定义了一个图形绘制器类 GraphicsDrawer,该类负责管理所有的绘制命令,包括执行、撤销和恢复上一步操作等功能。该类可以被其他业务逻辑调用,完成图形绘制的任务。
最后,我们可以使用以下代码来测试我们的图形绘制系统:
```java
public static void main(String[] args) {
GraphicsDrawer drawer = new GraphicsDrawer();
JFrame frame = new JFrame();
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel panel = new JPanel() {
private int[] xPoints = new int[3];
private int[] yPoints = new int[3];
private int pointIndex = 0;
private Command command;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Command command : drawer.getCommandList()) {
command.execute();
}
if (command != null) {
command.execute();
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
if (pointIndex == 0) {
xPoints[0] = e.getX();
yPoints[0] = e.getY();
pointIndex = 1;
} else if (pointIndex == 1) {
xPoints[1] = e.getX();
yPoints[1] = e.getY();
pointIndex = 2;
} else {
xPoints[2] = e.getX();
yPoints[2] = e.getY();
command = new TriangleCommand(getGraphics(), xPoints, yPoints);
drawer.addCommand(command);
pointIndex = 0;
}
} else if (e.getButton() == MouseEvent.BUTTON2) {
command = new EllipseCommand(getGraphics(), e.getX() - 25, e.getY() - 25, 50, 50);
drawer.addCommand(command);
} else if (e.getButton() == MouseEvent.BUTTON3) {
command = new RectangleCommand(getGraphics(), e.getX() - 25, e.getY() - 25, 50, 50);
drawer.addCommand(command);
}
}
};
frame.add(panel);
panel.addMouseListener(panel);
panel.addMouseMotionListener(panel);
}
```
在上面的代码中,我们创建了一个 JFrame 窗口,并添加了一个 JPanel 面板用于绘图。我们通过鼠标事件监听器来对鼠标点击事件进行处理,根据不同的鼠标按键来创建不同的绘制命令,并将其添加到图形绘制器中。
当需要恢复或撤销绘制操作时,我们可以调用图形绘制器的 undo() 和 redo() 方法来实现。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)