简单编程画图代码可以根据你选择的编程语言和绘图库有所不同。以下是几种不同编程语言的简单画图代码示例:
使用Python的Turtle库
```python
import turtle
创建一个画布和一个海龟
window = turtle.Screen()
t = turtle.Turtle()
绘制一个正方形
for _ in range(4):
t.forward(100) 向前移动100个像素
t.right(90) 向右转90度
关闭画布
window.mainloop()
```
使用Java的Swing库
```java
import javax.swing.*;
import java.awt.*;
public class SimpleDrawingProgram extends JFrame {
public SimpleDrawingProgram() {
setTitle("简单画图程序");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new DrawingPanel());
setVisible(true);
}
class DrawingPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(50, 50, 100, 100);
g.setColor(Color.BLUE);
g.drawOval(200, 200, 100, 100);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new SimpleDrawingProgram());
}
}
```
使用Python的Matplotlib库
```python
import matplotlib.pyplot as plt
import numpy as np
创建数据
x = np.linspace(-10*np.pi, 10*np.pi, 100, endpoint=True)
y = np.tan(x)/x
绘制折线图
plt.plot(x, y)
设置图表标题和坐标轴标签
plt.title('我的第一个折线图')
plt.xlabel('x轴')
plt.ylabel('y轴')
显示图表
plt.show()
```
使用C语言的ASCII字符
```c
include
int main() {
int size = 5; // 正方形的边长
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf(" "); // 使用字符绘制正方形
}
printf("\n"); // 换行
}
return 0;
}
```
这些示例展示了如何使用不同的编程语言和绘图库来创建简单的图形。你可以根据自己的需求和兴趣选择合适的工具和库来进行编程画图。