java 玫瑰花代码
时间: 2024-04-14 14:21:33 浏览: 39
Java版的玫瑰花程序代码可以用来表白,具有很好的参考价值。以下是Java版的玫瑰花程序代码:
```
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Rose extends JFrame {
private static final long serialVersionUID = 1L;
public Rose() {
super("玫瑰花");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public void paint(Graphics g) {
double R = 200;
double r = 0;
double x = 0;
double y = 0;
double k = 0;
double j = 0;
double d = 0;
double pi = Math.PI;
int n = 360;
int red = 0;
int green = 0;
int blue = 0;
int color = 0;
for (int i = 0; i < n; i++) {
r = R * Math.sin(k * pi);
x = r * Math.cos(j * pi) + 400;
y = r * Math.sin(j * pi) + 300;
d = Math.sqrt((x - 400) * (x - 400) + (y - 300) * (y - 300));
color = (int) (d / R * 255);
red = color;
green = 255 - color;
blue = 255 - color;
g.setColor(new Color(red, green, blue));
g.fillOval((int) x, (int) y, 5, 5);
k += 1.0 / n;
j += 1.0 / n;
}
}
public static void main(String[] args) {
new Rose();
}
}
```
这段代码使用了Java的图形库,绘制了一个玫瑰花的图案。具体实现过程可以参考代码注释。