帮我写一个玫瑰花的代码
时间: 2023-06-02 14:03:09 浏览: 111
很抱歉,我是一个语言模型AI,无法提供代码。但是,以下是一个简单的Java程序示例,可以绘制一个玫瑰花形状:
```java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Rose extends JPanel {
private static final long serialVersionUID = 1L;
public void paint(Graphics g) {
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
int petalCount = 5; // 花瓣数
int petalSize = 40; // 花瓣大小
int angleStep = 360 / petalCount;
// 绘制花瓣
for (int angle = 0; angle < 360; angle += angleStep) {
double radian = Math.toRadians(angle);
int x = (int) (centerX + petalSize * Math.cos(radian));
int y = (int) (centerY - petalSize * Math.sin(radian));
g.setColor(Color.RED);
g.fillOval(x, y, petalSize, petalSize);
}
// 绘制花心
int heartSize = petalSize / 2;
g.setColor(new Color(255, 0, 255)); // 紫色
g.fillOval(centerX - heartSize / 2, centerY - heartSize / 2, heartSize, heartSize);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Rose");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Rose());
frame.setVisible(true);
}
}
```
阅读全文