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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
| // GUI 컴포넌트 시스템
abstract class UIComponent {
protected String name;
protected int x, y, width, height;
protected boolean visible = true;
public UIComponent(String name, int x, int y, int width, int height) {
this.name = name;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
// 모든 컴포넌트가 구현해야 하는 기본 메서드들
public abstract void render(Graphics g);
public abstract void handleEvent(Event event);
public abstract Rectangle getBounds();
// Composite 전용 메서드들
public void add(UIComponent component) {
throw new UnsupportedOperationException("Cannot add children to leaf component");
}
public void remove(UIComponent component) {
throw new UnsupportedOperationException("Cannot remove children from leaf component");
}
public List<UIComponent> getChildren() {
return Collections.emptyList();
}
// 공통 기능
public void setVisible(boolean visible) {
this.visible = visible;
}
public boolean isVisible() {
return visible;
}
public String getName() {
return name;
}
}
// Leaf 컴포넌트들
class Button extends UIComponent {
private String text;
private Runnable clickHandler;
public Button(String name, int x, int y, String text) {
super(name, x, y, 100, 30);
this.text = text;
}
@Override
public void render(Graphics g) {
if (!visible) return;
g.drawRect(x, y, width, height);
g.drawString(text, x + 10, y + 20);
System.out.println("Rendering button: " + text + " at (" + x + "," + y + ")");
}
@Override
public void handleEvent(Event event) {
if (!visible) return;
if (event.getType() == EventType.CLICK &&
event.getX() >= x && event.getX() <= x + width &&
event.getY() >= y && event.getY() <= y + height) {
System.out.println("Button clicked: " + text);
if (clickHandler != null) {
clickHandler.run();
}
}
}
@Override
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
public void setClickHandler(Runnable handler) {
this.clickHandler = handler;
}
}
class Label extends UIComponent {
private String text;
public Label(String name, int x, int y, String text) {
super(name, x, y, text.length() * 8, 20);
this.text = text;
}
@Override
public void render(Graphics g) {
if (!visible) return;
g.drawString(text, x, y + 15);
System.out.println("Rendering label: " + text + " at (" + x + "," + y + ")");
}
@Override
public void handleEvent(Event event) {
// 라벨은 이벤트를 처리하지 않음
}
@Override
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
}
// Composite 컴포넌트들
class Panel extends UIComponent {
private List<UIComponent> children = new ArrayList<>();
private Color backgroundColor;
public Panel(String name, int x, int y, int width, int height) {
super(name, x, y, width, height);
}
@Override
public void add(UIComponent component) {
children.add(component);
}
@Override
public void remove(UIComponent component) {
children.remove(component);
}
@Override
public List<UIComponent> getChildren() {
return new ArrayList<>(children);
}
@Override
public void render(Graphics g) {
if (!visible) return;
// 자신의 배경 렌더링
if (backgroundColor != null) {
g.setColor(backgroundColor);
g.fillRect(x, y, width, height);
}
g.drawRect(x, y, width, height);
System.out.println("Rendering panel: " + name + " at (" + x + "," + y + ")");
// 모든 자식 컴포넌트 렌더링 (재귀적)
for (UIComponent child : children) {
child.render(g);
}
}
@Override
public void handleEvent(Event event) {
if (!visible) return;
// 이벤트를 모든 자식에게 전달 (재귀적)
for (UIComponent child : children) {
child.handleEvent(event);
}
}
@Override
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
public void setBackgroundColor(Color color) {
this.backgroundColor = color;
}
}
class Window extends UIComponent {
private List<UIComponent> children = new ArrayList<>();
private String title;
private boolean minimized = false;
public Window(String name, int x, int y, int width, int height, String title) {
super(name, x, y, width, height);
this.title = title;
}
@Override
public void add(UIComponent component) {
children.add(component);
}
@Override
public void remove(UIComponent component) {
children.remove(component);
}
@Override
public List<UIComponent> getChildren() {
return new ArrayList<>(children);
}
@Override
public void render(Graphics g) {
if (!visible) return;
// 윈도우 프레임 렌더링
g.drawRect(x, y, width, height);
g.fillRect(x, y, width, 25); // 타이틀 바
g.drawString(title, x + 5, y + 18);
System.out.println("Rendering window: " + title + " at (" + x + "," + y + ")");
if (minimized) return;
// 클라이언트 영역의 자식 컴포넌트들 렌더링
for (UIComponent child : children) {
child.render(g);
}
}
@Override
public void handleEvent(Event event) {
if (!visible || minimized) return;
// 타이틀 바 클릭 확인
if (event.getType() == EventType.CLICK &&
event.getX() >= x && event.getX() <= x + width &&
event.getY() >= y && event.getY() <= y + 25) {
System.out.println("Window title bar clicked: " + title);
return;
}
// 이벤트를 자식들에게 전달
for (UIComponent child : children) {
child.handleEvent(event);
}
}
@Override
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
public void minimize() {
this.minimized = true;
}
public void restore() {
this.minimized = false;
}
}
// 복잡한 GUI 구조 생성 예시
public class GUIExample {
public static void main(String[] args) {
// 메인 윈도우 생성
Window mainWindow = new Window("mainWindow", 100, 100, 400, 300, "My Application");
// 상단 패널 (버튼들)
Panel topPanel = new Panel("topPanel", 10, 35, 380, 50);
topPanel.add(new Button("saveBtn", 10, 10, "Save"));
topPanel.add(new Button("loadBtn", 120, 10, "Load"));
topPanel.add(new Button("exitBtn", 230, 10, "Exit"));
// 중앙 패널 (내용)
Panel centerPanel = new Panel("centerPanel", 10, 95, 380, 150);
centerPanel.add(new Label("titleLabel", 10, 10, "Document Title:"));
centerPanel.add(new Label("contentLabel", 10, 40, "Content goes here..."));
// 하단 패널 (상태)
Panel bottomPanel = new Panel("bottomPanel", 10, 255, 380, 30);
bottomPanel.add(new Label("statusLabel", 10, 5, "Ready"));
// 윈도우에 패널들 추가
mainWindow.add(topPanel);
mainWindow.add(centerPanel);
mainWindow.add(bottomPanel);
// 중첩된 윈도우 추가
Window dialogWindow = new Window("dialog", 200, 150, 200, 150, "Settings");
Panel dialogPanel = new Panel("dialogPanel", 10, 35, 180, 80);
dialogPanel.add(new Label("settingLabel", 10, 10, "Setting:"));
dialogPanel.add(new Button("okBtn", 10, 40, "OK"));
dialogPanel.add(new Button("cancelBtn", 100, 40, "Cancel"));
dialogWindow.add(dialogPanel);
// 전체 화면 렌더링
Graphics mockGraphics = new MockGraphics();
System.out.println("=== Rendering Main Window ===");
mainWindow.render(mockGraphics);
System.out.println("\n=== Rendering Dialog Window ===");
dialogWindow.render(mockGraphics);
// 이벤트 처리 테스트
System.out.println("\n=== Event Handling Test ===");
Event clickEvent = new Event(EventType.CLICK, 120, 110); // Save 버튼 클릭
mainWindow.handleEvent(clickEvent);
}
}
// 재귀적 구조 순회 유틸리티
public class CompositeUtils {
// 깊이 우선 탐색으로 모든 컴포넌트 찾기
public static List<UIComponent> findAll(UIComponent root, Predicate<UIComponent> condition) {
List<UIComponent> result = new ArrayList<>();
findAllRecursive(root, condition, result);
return result;
}
private static void findAllRecursive(UIComponent component, Predicate<UIComponent> condition, List<UIComponent> result) {
if (condition.test(component)) {
result.add(component);
}
for (UIComponent child : component.getChildren()) {
findAllRecursive(child, condition, result);
}
}
// 특정 이름으로 컴포넌트 찾기
public static Optional<UIComponent> findByName(UIComponent root, String name) {
return findAll(root, comp -> comp.getName().equals(name))
.stream()
.findFirst();
}
// 트리 구조 출력
public static void printTree(UIComponent root) {
printTreeRecursive(root, 0);
}
private static void printTreeRecursive(UIComponent component, int depth) {
String indent = " ".repeat(depth);
System.out.println(indent + component.getClass().getSimpleName() + ": " + component.getName());
for (UIComponent child : component.getChildren()) {
printTreeRecursive(child, depth + 1);
}
}
// 총 컴포넌트 개수 계산
public static int countComponents(UIComponent root) {
return 1 + root.getChildren().stream()
.mapToInt(CompositeUtils::countComponents)
.sum();
}
// 최대 깊이 계산
public static int maxDepth(UIComponent root) {
if (root.getChildren().isEmpty()) {
return 1;
}
return 1 + root.getChildren().stream()
.mapToInt(CompositeUtils::maxDepth)
.max()
.orElse(0);
}
}
|