Graphic Rendering System 1.0
A Java-based graphic rendering system implementing various design patterns
Loading...
Searching...
No Matches
SwingRenderer.java
Go to the documentation of this file.
1package com.example.graphics.render;
2
3import com.example.graphics.model.Circle;
4import com.example.graphics.model.Line;
5import com.example.graphics.model.Rectangle;
6import com.example.graphics.model.Triangle;
7
8import javax.swing.*;
9import java.awt.*;
10import java.awt.geom.Ellipse2D;
11import java.awt.geom.Line2D;
12import java.awt.geom.Path2D;
13import java.util.ArrayList;
14import java.util.List;
15
19public class SwingRenderer implements Renderer {
20 private final int width;
21 private final int height;
22 private final List<java.awt.Shape> shapes;
23 private final List<Color> shapeColors;
24 private JPanel renderPanel;
25
26 public SwingRenderer(int width, int height) {
27 this.width = width;
28 this.height = height;
29 this.shapes = new ArrayList<>();
30 this.shapeColors = new ArrayList<>();
31 }
32
33 @Override
34 public void renderCircle(Circle circle) {
35 shapes.add(new Ellipse2D.Double(
36 circle.getX() - circle.getRadius(),
37 circle.getY() - circle.getRadius(),
38 circle.getRadius() * 2,
39 circle.getRadius() * 2
40 ));
41 shapeColors.add(Color.BLUE);
42
43 // 添加一个小点表示圆心
44 shapes.add(new Ellipse2D.Double(
45 circle.getX() - 2,
46 circle.getY() - 2,
47 4,
48 4
49 ));
50 shapeColors.add(Color.RED);
51 }
52
53 @Override
54 public void renderRectangle(Rectangle rectangle) {
55 shapes.add(new java.awt.Rectangle(
56 rectangle.getX(),
57 rectangle.getY(),
58 rectangle.getWidth(),
59 rectangle.getHeight()
60 ));
61 shapeColors.add(Color.GREEN);
62 }
63
64 @Override
65 public void renderLine(Line line) {
66 shapes.add(new Line2D.Double(
67 line.getX1(),
68 line.getY1(),
69 line.getX2(),
70 line.getY2()
71 ));
72 shapeColors.add(Color.BLACK);
73
74 // 添加两个小点表示线的端点
75 shapes.add(new Ellipse2D.Double(
76 line.getX1() - 3,
77 line.getY1() - 3,
78 6,
79 6
80 ));
81 shapeColors.add(Color.RED);
82
83 shapes.add(new Ellipse2D.Double(
84 line.getX2() - 3,
85 line.getY2() - 3,
86 6,
87 6
88 ));
89 shapeColors.add(Color.RED);
90 }
91
92 @Override
93 public void renderTriangle(Triangle triangle) {
94 // 创建三角形路径
95 Path2D path = new Path2D.Double();
96 path.moveTo(triangle.getX1(), triangle.getY1());
97 path.lineTo(triangle.getX2(), triangle.getY2());
98 path.lineTo(triangle.getX3(), triangle.getY3());
99 path.closePath();
100
101 shapes.add(path);
102 shapeColors.add(Color.ORANGE);
103
104 // 添加三个小点表示三角形的顶点
105 shapes.add(new Ellipse2D.Double(
106 triangle.getX1() - 3,
107 triangle.getY1() - 3,
108 6,
109 6
110 ));
111 shapeColors.add(Color.RED);
112
113 shapes.add(new Ellipse2D.Double(
114 triangle.getX2() - 3,
115 triangle.getY2() - 3,
116 6,
117 6
118 ));
119 shapeColors.add(Color.RED);
120
121 shapes.add(new Ellipse2D.Double(
122 triangle.getX3() - 3,
123 triangle.getY3() - 3,
124 6,
125 6
126 ));
127 shapeColors.add(Color.RED);
128 }
129
130 @Override
131 public void clear() {
132 shapes.clear();
133 shapeColors.clear();
134 if (renderPanel != null) {
135 renderPanel.repaint();
136 }
137 }
138
139 @Override
140 public void display() {
141 if (renderPanel != null) {
142 renderPanel.repaint();
143 }
144 }
145
150 public JPanel getRenderPanel() {
151 if (renderPanel == null) {
152 renderPanel = new JPanel() {
153 @Override
154 protected void paintComponent(Graphics g) {
155 super.paintComponent(g);
156 Graphics2D g2d = (Graphics2D) g;
157 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
158
159 // 绘制背景网格
160 drawGrid(g2d);
161
162 // 绘制所有形状
163 for (int i = 0; i < shapes.size(); i++) {
164 g2d.setColor(shapeColors.get(i));
165 g2d.setStroke(new BasicStroke(2));
166 g2d.draw(shapes.get(i));
167 }
168 }
169
170 private void drawGrid(Graphics2D g2d) {
171 g2d.setColor(new Color(240, 240, 240));
172 g2d.setStroke(new BasicStroke(1));
173
174 // 绘制水平线
175 for (int y = 0; y < height; y += 20) {
176 g2d.drawLine(0, y, width, y);
177 }
178
179 // 绘制垂直线
180 for (int x = 0; x < width; x += 20) {
181 g2d.drawLine(x, 0, x, height);
182 }
183 }
184 };
185 renderPanel.setPreferredSize(new Dimension(width, height));
186 renderPanel.setBackground(Color.WHITE);
187 }
188 return renderPanel;
189 }
190}
void renderRectangle(Rectangle rectangle)
final List< java.awt.Shape > shapes