网站首页 网站地图
网站首页 > 娱乐人生 > 编程画个立方体怎么画的

编程画个立方体怎么画的

时间:2026-03-20 15:57:28

使用Java和Swing

创建一个窗体

新建一个名为`drawing_box`的包。

绘制小立方体

使用`Polygon`类填充三面颜色,并绘制边线。

示例代码如下:

```java

import javax.swing.*;

import java.awt.*;

public class ShapeLFT {

public void drawLFT(Graphics g, int x, int y) {

g.setColor(new Color(100, 255, 100));

g.fillRect(x, y, 30, 30); // 正面填充颜色

}

public static void main(String[] args) {

JFrame frame = new JFrame("Draw Cube");

ShapeLFT shape = new ShapeLFT();

frame.add(new JPanel() {

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

shape.drawLFT(g, 50, 50); // 在(50,50)位置绘制小立方体

}

});

frame.setSize(400, 400);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

```

使用Python和Matplotlib

导入必要的库

```python

import matplotlib.pyplot as plt

import numpy as np

```

绘制立方体

```python

x, y, z = np.indices((2, 2, 2))

filled = np.ones((1, 1, 1))

ax = plt.subplot(projection='3d')

ax.voxels(x, y, z, filled=filled)

plt.show()

```

使用Python和PyOpenGL

安装PyOpenGL

```bash

pip install PyOpenGL PyOpenGL_accelerate

```

创建窗口并绘制立方体

```python

from OpenGL.GL import *

from OpenGL.GLUT import *

def draw_cube():

vertices = (

(1, -1, -1), (1, 1, -1),

(1, 1, 1), (1, -1, 1),

(-1, -1, -1), (-1, 1, -1),

(-1, 1, 1), (-1, -1, 1),

(1, 0, -1), (1, 0, 1),

(-1, 0, -1), (-1, 0, 1),

(0, 1, -1), (0, 1, 1),

(0, -1, -1), (0, -1, 1)

glBegin(GL_QUADS)

for vertex in vertices:

glVertex3fv(vertex)

glEnd()

def main(argv):

glutInit(argv)

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)

glutInitWindowSize(800, 600)

glutCreateWindow("Draw Cube")

glEnable(GL_DEPTH_TEST)

glutDisplayFunc(draw_cube)

glutMainLoop()

if __name__ == "__main__":

main(sys.argv)

```

使用CSS动画

HTML和CSS代码