电脑怎么用代码画红底五角星

如题所述

下面是用 Java 语言画一个红底五角星的代码示例:
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics;
import javax.swing.JFrame; import javax.swing.JPanel;
public class FivePointedStar {
public static void main(String[] args) {
JFrame frame = new JFrame("Five Pointed Star");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FivePointedStarPanel panel = new FivePointedStarPanel();
frame.add(panel);

frame.pack();
frame.setVisible(true);
}
}

class FivePointedStarPanel extends JPanel {
public FivePointedStarPanel() {
setPreferredSize(new Dimension(300, 300));
setBackground(Color.WHITE);
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int xPoints[] = {150, 200, 250, 225, 175, 125, 100, 125};
int yPoints[] = {100, 50, 100, 150, 150, 100, 50, 100};
int nPoints = 8; g.setColor(Color.RED);
g.fillPolygon(xPoints, yPoints, nPoints); }
}
这段代码中,我们创建了一个 JFrame 窗口,并在窗口中添加了一个 JPanel 面板。在面板中,我们重写了 paintComponent 方法,使用 Graphics 类的 fillPolygon 方法画出了一个五角星。我们设置了五角星的颜色为红色,并设置了面板的背景色为白色。

运行这段代码后,将会弹出一个窗口,显示一个红底五角星。

希望这个示例能帮助你画出一个红底五角星。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2022-12-22
代码如下:

from turtle import *
pensize(5)
pencolor('green')
fillcolor('red')
begin_fill()
while True:
fd(200)
right(144)
if abs(pos()) < 1:
break
end_fill()

结果
相似回答