Java 绘制马龙六边形战士的蛛网图,并输出为图片
时间: 2024-03-20 08:41:07 浏览: 113
好的,以下是使用Java绘制马龙六边形战士的蛛网图,并输出为图片的代码:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class MaLongHexagonGraph {
private static final int WIDTH = 1000;
private static final int HEIGHT = 1000;
private static final int PADDING = 100;
private static final int SIDES = 6;
private static final double ANGLE = 2 * Math.PI / SIDES;
private static final double RADIUS = 200;
private static final double SCALE_FACTOR = 0.8;
private static final int[] VALUES = {100, 98, 95, 92, 85, 80};
public static void main(String[] args) {
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, WIDTH, HEIGHT);
int centerX = WIDTH / 2;
int centerY = HEIGHT / 2;
int[] xPoints = new int[SIDES];
int[] yPoints = new int[SIDES];
for (int i = 0; i < SIDES; i++) {
double angle = i * ANGLE;
int x = (int) Math.round(centerX + RADIUS * Math.cos(angle));
int y = (int) Math.round(centerY + RADIUS * Math.sin(angle));
xPoints[i] = x;
yPoints[i] = y;
}
g.setColor(Color.BLACK);
for (int i = 0; i < SIDES; i++) {
int x1 = xPoints[i];
int y1 = yPoints[i];
int x2 = xPoints[(i + 1) % SIDES];
int y2 = yPoints[(i + 1) % SIDES];
g.drawLine(x1, y1, x2, y2);
}
for (int i = 0; i < SIDES; i++) {
double angle = i * ANGLE;
double scaleFactor = VALUES[i] / 100.0 * SCALE_FACTOR;
int x = (int) Math.round(centerX + RADIUS * scaleFactor * Math.cos(angle));
int y = (int) Math.round(centerY + RADIUS * scaleFactor * Math.sin(angle));
g.fillOval(x - 5, y - 5, 10, 10);
}
g.dispose();
try {
ImageIO.write(image, "png", new File("malong.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
运行以上代码,会生成一个名为 `malong.png` 的图片文件,内容为马龙六边形战士的蛛网图。
阅读全文