玫瑰花代码编程java
时间: 2024-04-14 15:21:11 浏览: 73
代码玫瑰花
以下是一个使用Java编写的玫瑰花代码示例:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Rose extends JPanel {
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.RED);
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
int numPetals = 20;
int petalRadius = 100;
for (int i = 0; i < numPetals; i++) {
double angle = i * 2 * Math.PI / numPetals; int x = (int) (centerX + petalRadius * Math.cos(angle));
int y = (int) (centerY + petalRadius * Math.sin(angle));
g2d.fillOval(x, y, 10, 10);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Rose Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Rose rose = new Rose();
frame.add(rose);
frame.setSize(400, 400);
frame.setVisible(true);
}
}
```
阅读全文