1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
| /**
* 图形元素抽象原型
*/
public abstract class GraphicElement implements Cloneable {
protected int x, y; // 位置
protected String color;
protected int width, height; // 尺寸
public GraphicElement(int x, int y, String color, int width, int height) {
this.x = x;
this.y = y;
this.color = color;
this.width = width;
this.height = height;
}
public abstract void render();
public abstract GraphicElement clone() throws CloneNotSupportedException;
// 移动元素
public void moveTo(int newX, int newY) {
this.x = newX;
this.y = newY;
}
// getter和setter方法
public int getX() { return x; }
public int getY() { return y; }
public String getColor() { return color; }
public void setColor(String color) { this.color = color; }
public int getWidth() { return width; }
public int getHeight() { return height; }
}
/**
* 文本框元素
*/
public class TextBox extends GraphicElement {
private String text;
private String font;
private int fontSize;
public TextBox(int x, int y, String color, int width, int height,
String text, String font, int fontSize) {
super(x, y, color, width, height);
this.text = text;
this.font = font;
this.fontSize = fontSize;
}
@Override
public void render() {
System.out.println("渲染文本框:");
System.out.println(" 位置: (" + x + ", " + y + ")");
System.out.println(" 尺寸: " + width + "x" + height);
System.out.println(" 颜色: " + color);
System.out.println(" 文本: " + text);
System.out.println(" 字体: " + font + ", 大小: " + fontSize);
}
@Override
public TextBox clone() throws CloneNotSupportedException {
return (TextBox) super.clone();
}
public String getText() { return text; }
public void setText(String text) { this.text = text; }
public String getFont() { return font; }
public void setFont(String font) { this.font = font; }
public int getFontSize() { return fontSize; }
public void setFontSize(int fontSize) { this.fontSize = fontSize; }
}
/**
* 按钮元素
*/
public class Button extends GraphicElement {
private String label;
private String style;
public Button(int x, int y, String color, int width, int height,
String label, String style) {
super(x, y, color, width, height);
this.label = label;
this.style = style;
}
@Override
public void render() {
System.out.println("渲染按钮:");
System.out.println(" 位置: (" + x + ", " + y + ")");
System.out.println(" 尺寸: " + width + "x" + height);
System.out.println(" 颜色: " + color);
System.out.println(" 标签: " + label);
System.out.println(" 样式: " + style);
}
@Override
public Button clone() throws CloneNotSupportedException {
return (Button) super.clone();
}
public String getLabel() { return label; }
public void setLabel(String label) { this.label = label; }
public String getStyle() { return style; }
public void setStyle(String style) { this.style = style; }
}
/**
* 图形编辑器 - 使用原型模式复制元素
*/
public class GraphicEditor {
private List<GraphicElement> elements = new ArrayList<>();
private GraphicElement clipboard; // 剪贴板
// 添加元素
public void addElement(GraphicElement element) {
elements.add(element);
System.out.println("添加了新元素到画布");
}
// 复制元素到剪贴板
public void copyElement(int index) throws CloneNotSupportedException {
if (index >= 0 && index < elements.size()) {
clipboard = elements.get(index).clone();
System.out.println("元素已复制到剪贴板");
}
}
// 粘贴元素从剪贴板
public void pasteElement(int offsetX, int offsetY) throws CloneNotSupportedException {
if (clipboard != null) {
GraphicElement newElement = clipboard.clone();
newElement.moveTo(newElement.getX() + offsetX, newElement.getY() + offsetY);
elements.add(newElement);
System.out.println("从剪贴板粘贴了新元素");
}
}
// 渲染所有元素
public void renderAll() {
System.out.println("\n=== 画布渲染 ===");
for (int i = 0; i < elements.size(); i++) {
System.out.println("元素 " + i + ":");
elements.get(i).render();
System.out.println();
}
}
// 批量复制元素(创建网格布局)
public void createGrid(int sourceIndex, int rows, int cols, int spacingX, int spacingY)
throws CloneNotSupportedException {
if (sourceIndex >= 0 && sourceIndex < elements.size()) {
GraphicElement template = elements.get(sourceIndex);
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (row == 0 && col == 0) continue; // 跳过原始元素
GraphicElement cloned = template.clone();
cloned.moveTo(
template.getX() + col * spacingX,
template.getY() + row * spacingY
);
elements.add(cloned);
}
}
System.out.println("创建了 " + (rows * cols - 1) + " 个网格布局元素");
}
}
}
// 图形编辑器演示
public class GraphicEditorDemo {
public static void main(String[] args) throws CloneNotSupportedException {
System.out.println("=== 图形编辑器原型模式演示 ===");
GraphicEditor editor = new GraphicEditor();
// 创建原始元素
TextBox titleText = new TextBox(10, 10, "黑色", 200, 30,
"标题文本", "Arial", 16);
Button submitButton = new Button(50, 100, "蓝色", 100, 40,
"提交", "圆角");
editor.addElement(titleText);
editor.addElement(submitButton);
System.out.println("\n=== 初始画布 ===");
editor.renderAll();
// 复制粘贴操作
System.out.println("=== 复制粘贴操作 ===");
editor.copyElement(0); // 复制标题文本
editor.pasteElement(0, 50); // 粘贴到下方
// 修改粘贴的文本内容
TextBox copiedText = (TextBox) editor.elements.get(2);
copiedText.setText("副标题文本");
copiedText.setColor("灰色");
System.out.println("\n=== 复制粘贴后画布 ===");
editor.renderAll();
// 创建按钮网格
System.out.println("=== 创建按钮网格 ===");
editor.createGrid(1, 2, 3, 120, 60); // 基于索引1的按钮创建2行3列网格
System.out.println("\n=== 最终画布 ===");
editor.renderAll();
}
}
|