java: Composite Pattern

 

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
/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 合成模式 Composite Patterns
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 Shape
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc Shape.java
 * from: https://refactoring.guru/design-patterns/composite/java/example
 * */
 
 
package com.javapatterns.composite;
 
 
import java.awt.*;
/**
 *
 * @author geovindu
 * */
public interface Shape {
    /**
     *
     * */
    int getX();
    /**
     *
     * */
    int getY();
    /**
     *
     * */
    int getWidth();
    /**
     *
     * */
    int getHeight();
    /**
     *
     * */
    void move(int x, int y);
    /**
     *
     * */
    boolean isInsideBounds(int x, int y);
    /**
     *
     * */
    void select();
    /**
     *
     * */
    void unSelect();
    /**
     *
     * */
    boolean isSelected();
    /**
     *
     * */
    void paint(Graphics graphics);
 
 
}

  

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
/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 合成模式 Composite Patterns
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 Shape
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc BaseShape.java
 *
 * */
 
package com.javapatterns.composite;
 
import java.awt.*;
 
 
/**
 *
 * @author geovindu
 * */
abstract  class BaseShape implements Shape{
 
    public int x;
    public int y;
    public Color color;
    private boolean selected = false;
    /**
     *
     * */
    BaseShape(int x, int y, Color color) {
        this.x = x;
        this.y = y;
        this.color = color;
    }
    /**
     *
     * */
    @Override
    public int getX() {
        return x;
    }
    /**
     *
     * */
    @Override
    public int getY() {
        return y;
    }
    /**
     *
     * */
    @Override
    public int getWidth() {
        return 0;
    }
    /**
     *
     * */
    @Override
    public int getHeight() {
        return 0;
    }
    /**
     *
     * */
    @Override
    public void move(int x, int y) {
        this.x += x;
        this.y += y;
    }
    /**
     *
     * */
    @Override
    public boolean isInsideBounds(int x, int y) {
        return x > getX() && x < (getX() + getWidth()) &&
                y > getY() && y < (getY() + getHeight());
    }
    /**
     *
     * */
    @Override
    public void select() {
        selected = true;
    }
    /**
     *
     * */
    @Override
    public void unSelect() {
        selected = false;
    }
    /**
     *
     * */
    @Override
    public boolean isSelected() {
        return selected;
    }
    /**
     *
     * */
    void enableSelectionStyle(Graphics graphics) {
        graphics.setColor(Color.LIGHT_GRAY);
 
        Graphics2D g2 = (Graphics2D) graphics;
        float dash1[] = {2.0f};
        g2.setStroke(new BasicStroke(1.0f,
                BasicStroke.CAP_BUTT,
                BasicStroke.JOIN_MITER,
                2.0f, dash1, 0.0f));
    }
    /**
     *
     * */
    void disableSelectionStyle(Graphics graphics) {
        graphics.setColor(color);
        Graphics2D g2 = (Graphics2D) graphics;
        g2.setStroke(new BasicStroke());
    }
 
    /**
     *
     * */
    @Override
    public void paint(Graphics graphics) {
        if (isSelected()) {
            enableSelectionStyle(graphics);
        }
        else {
            disableSelectionStyle(graphics);
        }
 
        // ...
    }
 
}

  

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
/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 合成模式 Composite Patterns
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 Shape
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc Dot.java
 *
 * */
 
package com.javapatterns.composite;
 
import java.awt.*;
/**
 *画点
 * @author geovindu
 * */
public class Dot extends BaseShape{
 
    private final int DOT_SIZE = 3;
    /**
     *
     * */
    public Dot(int x, int y, Color color) {
        super(x, y, color);
    }
    /**
     *
     * */
    @Override
    public int getWidth() {
        return DOT_SIZE;
    }
    /**
     *
     * */
    @Override
    public int getHeight() {
        return DOT_SIZE;
    }
    /**
     *
     * */
    @Override
    public void paint(Graphics graphics) {
        super.paint(graphics);
        graphics.fillRect(x - 1, y - 1, getWidth(), getHeight());
    }
 
}

  

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
/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 合成模式 Composite Patterns
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 Shape
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc Circle.java
 *
 * */
 
package com.javapatterns.composite;
 
 
import java.awt.*;
 
/**
 *画圆
 * @author geovindu
 * */
public class Circle extends BaseShape{
 
    public int radius;
    /**
     *
     * */
    public Circle(int x, int y, int radius, Color color) {
        super(x, y, color);
        this.radius = radius;
    }
    /**
     *
     * */
    @Override
    public int getWidth() {
        return radius * 2;
    }
    /**
     *
     * */
    @Override
    public int getHeight() {
        return radius * 2;
    }
    /**
     *
     * */
    public void paint(Graphics graphics) {
        super.paint(graphics);
        graphics.drawOval(x, y, getWidth() - 1, getHeight() - 1);
    }
}

  

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
/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 合成模式 Composite Patterns
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 Shape
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc Rectangle.java
 *
 * */
 
 
package com.javapatterns.composite;
 
import java.awt.*;
 
/**
 *画线
 * @author geovindu
 * */
public class Rectangle extends BaseShape{
 
    public int width;
    public int height;
    /**
     *
     * */
    public Rectangle(int x, int y, int width, int height, Color color) {
        super(x, y, color);
        this.width = width;
        this.height = height;
    }
    /**
     *
     * */
    @Override
    public int getWidth() {
        return width;
    }
    /**
     *
     * */
    @Override
    public int getHeight() {
        return height;
    }
    /**
     *
     * */
    @Override
    public void paint(Graphics graphics) {
        super.paint(graphics);
        graphics.drawRect(x, y, getWidth() - 1, getHeight() - 1);
    }
}

  

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
/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 合成模式 Composite Patterns
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 Shape
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc CompoundShape.java
 *
 * */
 
package com.javapatterns.composite;
 
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 *
 * @author geovindu
 * */
public class CompoundShape  extends BaseShape{
 
 
    protected List<Shape> children = new ArrayList<>();
    /**
     *
     * */
    public CompoundShape(Shape... components) {
        super(0, 0, Color.BLACK);
        add(components);
    }
    /**
     *
     * */
    public void add(Shape component) {
        children.add(component);
    }
    /**
     *
     * */
    public void add(Shape... components) {
        children.addAll(Arrays.asList(components));
    }
    /**
     *
     * */
    public void remove(Shape child) {
        children.remove(child);
    }
    /**
     *
     * */
    public void remove(Shape... components) {
        children.removeAll(Arrays.asList(components));
    }
    /**
     *
     * */
    public void clear() {
        children.clear();
    }
    /**
     *
     * */
    @Override
    public int getX() {
        if (children.size() == 0) {
            return 0;
        }
        int x = children.get(0).getX();
        for (Shape child : children) {
            if (child.getX() < x) {
                x = child.getX();
            }
        }
        return x;
    }
    /**
     *
     * */
    @Override
    public int getY() {
        if (children.size() == 0) {
            return 0;
        }
        int y = children.get(0).getY();
        for (Shape child : children) {
            if (child.getY() < y) {
                y = child.getY();
            }
        }
        return y;
    }
    /**
     *
     * */
    @Override
    public int getWidth() {
        int maxWidth = 0;
        int x = getX();
        for (Shape child : children) {
            int childsRelativeX = child.getX() - x;
            int childWidth = childsRelativeX + child.getWidth();
            if (childWidth > maxWidth) {
                maxWidth = childWidth;
            }
        }
        return maxWidth;
    }
    /**
     *
     * */
    @Override
    public int getHeight() {
        int maxHeight = 0;
        int y = getY();
        for (Shape child : children) {
            int childsRelativeY = child.getY() - y;
            int childHeight = childsRelativeY + child.getHeight();
            if (childHeight > maxHeight) {
                maxHeight = childHeight;
            }
        }
        return maxHeight;
    }
    /**
     *
     * */
    @Override
    public void move(int x, int y) {
        for (Shape child : children) {
            child.move(x, y);
        }
    }
    /**
     *
     * */
    @Override
    public boolean isInsideBounds(int x, int y) {
        for (Shape child : children) {
            if (child.isInsideBounds(x, y)) {
                return true;
            }
        }
        return false;
    }
    /**
     *
     * */
    @Override
    public void unSelect() {
        super.unSelect();
        for (Shape child : children) {
            child.unSelect();
        }
    }
    /**
     *
     * */
    public boolean selectChildAt(int x, int y) {
        for (Shape child : children) {
            if (child.isInsideBounds(x, y)) {
                child.select();
                return true;
            }
        }
        return false;
    }
    /**
     *
     * */
    @Override
    public void paint(Graphics graphics) {
        if (isSelected()) {
            enableSelectionStyle(graphics);
            graphics.drawRect(getX() - 1, getY() - 1, getWidth() + 1, getHeight() + 1);
            disableSelectionStyle(graphics);
        }
 
        for (Shape child : children) {
            child.paint(graphics);
        }
    }
 
}

  

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
/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * 合成模式 Composite Patterns
 * 历史版本: JDK 14.02
 * 2022-09-12 创建者 Shape
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc ImageEditor.java
 *
 * */
 
 
package com.javapatterns.composite;
 
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
 
/**
 *图形编辑
 * @author geovindu
 * */
public class ImageEditor {
 
    private EditorCanvas canvas;
    private CompoundShape allShapes = new CompoundShape();
    /**
     *
     * */
    public ImageEditor() {
        canvas = new EditorCanvas();
    }
    /**
     *
     * */
    public void loadShapes(Shape... shapes) {
        allShapes.clear();
        allShapes.add(shapes);
        canvas.refresh();
    }
    /**
     *
     * */
    private class EditorCanvas extends Canvas {
        JFrame frame;
 
        private static final int PADDING = 10;
 
        EditorCanvas() {
            createFrame();
            refresh();
            addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    allShapes.unSelect();
                    allShapes.selectChildAt(e.getX(), e.getY());
                    e.getComponent().repaint();
                }
            });
        }
        /**
         *
         * */
        void createFrame() {
            frame = new JFrame();
            frame.setName("画图测试");
            frame.setTitle("画图测试");
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
 
            JPanel contentPanel = new JPanel();
            Border padding = BorderFactory.createEmptyBorder(PADDING, PADDING, PADDING, PADDING);
            contentPanel.setBorder(padding);
            frame.setContentPane(contentPanel);
 
            frame.add(this);
            frame.setVisible(true);
            frame.getContentPane().setBackground(Color.LIGHT_GRAY);
        }
        /**
         *
         * */
        public int getWidth() {
            return allShapes.getX() + allShapes.getWidth() + PADDING;
        }
        /**
         *
         * */
        public int getHeight() {
            return allShapes.getY() + allShapes.getHeight() + PADDING;
        }
        /**
         *
         * */
        void refresh() {
            this.setSize(getWidth(), getHeight());
            frame.pack();
        }
        /**
         *
         * */
        public void paint(Graphics graphics) {
            allShapes.paint(graphics);
        }
    }
 
}

  

调用测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//合成模式
            ImageEditor editor = new ImageEditor();
            editor.loadShapes(
                    new Circle(10, 10, 10, Color.BLUE),
 
                    new CompoundShape(
                            new Circle(110, 110, 50, Color.RED),
                            new Dot(160, 160, Color.RED)
                    ),
 
                    new CompoundShape(
                            new com.javapatterns.composite.Rectangle(250, 250, 100, 100, Color.GREEN),
                            new Dot(240, 240, Color.GREEN),
                            new Dot(240, 360, Color.GREEN),
                            new Dot(360, 360, Color.GREEN),
                            new Dot(360, 240, Color.GREEN)
                    )
            );

  

输出:

 

posted @   ®Geovin Du Dream Park™  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2012-09-17 SQL 生成公曆和農曆對照數據,公曆查找農曆和農曆查找公曆函數
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示