编写程序实现绘制三角形、正方形、五边形、六边形以及圆,并填充不同的颜色,程序也为图形添加文 本标题
时间: 2024-10-05 13:00:30 浏览: 39
Wincc7.5中如何绘制不规则图形并填充背景颜色?.docx
5星 · 资源好评率100%
编写一个简单的图形绘制程序,可以使用各种编程语言如Python的turtle模块或者Java的Graphics API。以下是使用Python turtle模块的一个基本示例:
```python
import turtle
# 创建画笔对象
window = turtle.Screen()
pen = turtle.Turtle()
# 定义函数绘制形状并填色
def draw_shape(shape, color):
pen.fillcolor(color)
pen.begin_fill()
if shape == 'triangle':
for _ in range(3):
pen.forward(100)
pen.left(120)
elif shape == 'square':
for _ in range(4):
pen.forward(100)
pen.right(90)
elif shape == 'pentagon':
angle = 360 / 5
for _ in range(5):
pen.forward(100)
pen.left(angle)
elif shape == 'hexagon':
angle = 360 / 6
for _ in range(6):
pen.forward(100)
pen.left(angle)
else: # 圆
pen.circle(50)
pen.end_fill()
# 绘制图形及其标题
shapes = ['triangle', 'square', 'pentagon', 'hexagon', 'circle']
colors = ['red', 'blue', 'green', 'yellow', 'purple']
for i in range(len(shapes)):
draw_shape(shapes[i], colors[i])
pen.penup() # 提起画笔
pen.goto(i * 200, -200) # 移动到新位置
pen.pendown() # 放下画笔
pen.write(f"{shapes[i]} ({colors[i]})", align="center", font=("Arial", 18, "bold"))
# 关闭窗口
window.exitonclick()
阅读全文