用java写一个基础款彩色心形代码
时间: 2023-12-31 19:04:16 浏览: 30
下面是用Java语言写的基础款彩色心形代码:
```
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ColorfulHeartShape {
public static void main(String[] args) throws IOException {
int width = 500;
int height = 500;
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (double y = 1.5; y > -1.5; y -= 0.1) {
for (double x = -1.5; x < 1.5; x += 0.05) {
double a = x * x + y * y - 1;
if (a * a * a - x * x * y * y * y <= 0.0) {
int red = (int) ((x + 1.5) / 3 * 255);
int green = (int) ((y + 1.5) / 3 * 255);
int blue = (int) (Math.abs(x - y) * 255);
Color color = new Color(red, green, blue);
img.setRGB((int) ((x + 1.5) * width / 3), (int) ((1.5 - y) * height / 3), color.getRGB());
}
}
}
File file = new File("heart.png");
ImageIO.write(img, "png", file);
}
}
```
运行这个程序,它会生成一个名为`heart.png`的PNG格式图片文件,里面包含了一个基础款彩色心形。图片中的颜色是根据心形上每一个点的位置计算出来的,其中红色和绿色分别根据点在x轴和y轴上的位置计算,蓝色则取x轴和y轴之差的绝对值。