Java 实现游戏综合战力六边形雷达蛛网图,包含标题,并输出为图片,包含所有代码
时间: 2024-04-05 19:32:06 浏览: 98
基于java实现游戏雷霆战机
4星 · 用户满意度95%
好的,下面是实现游戏综合战力六边形雷达蛛网图的 Java 代码,包含标题并输出为图片:
```
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Path2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class RadarChart {
private final int size;
private final double[] data;
private final String[] labels;
private final String title;
public RadarChart(String title, String[] labels, double[] data, int size) {
this.size = size;
this.data = data;
this.labels = labels;
this.title = title;
}
private double getMaxValue() {
double max = Double.MIN_VALUE;
for (double d : data) {
max = Math.max(max, d);
}
return max;
}
private void drawPolygon(Graphics g, int sides, int x, int y, int r) {
Path2D path = new Path2D.Double();
double angle = 2 * Math.PI / sides;
for (int i = 0; i < sides; i++) {
double px = x + r * Math.cos(i * angle);
double py = y + r * Math.sin(i * angle);
if (i == 0) {
path.moveTo(px, py);
} else {
path.lineTo(px, py);
}
}
path.closePath();
((Graphics2D) g).draw(path);
}
public void save(String filename) throws IOException {
BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.WHITE);
g.fillRect(0, 0, size, size);
g.setColor(Color.BLACK);
g.setFont(new Font("Arial", Font.BOLD, 14));
g.drawString(title, 10, 20);
int cx = size / 2;
int cy = size / 2;
int r = size / 3;
for (int i = 3; i <= 8; i++) {
drawPolygon(g, i, cx, cy, r * i / 8);
}
double max = getMaxValue();
g.setColor(Color.GRAY);
g.setFont(new Font("Arial", Font.PLAIN, 12));
for (int i = 0; i < labels.length; i++) {
double value = data[i];
double angle = 2 * Math.PI * i / labels.length;
int px = (int) (cx + r * value / max * Math.cos(angle));
int py = (int) (cy + r * value / max * Math.sin(angle));
g.drawString(labels[i], px, py);
g.setColor(Color.BLUE);
g.fillOval(px - 3, py - 3, 6, 6);
g.setColor(Color.GRAY);
}
ImageIO.write(image, "PNG", new File(filename));
}
public static void main(String[] args) throws IOException {
String title = "综合战力六边形雷达蛛网图";
String[] labels = { "攻击力", "防御力", "生命值", "速度", "技能伤害", "控制能力", "团队贡献" };
double[] data = { 80.0, 70.0, 90.0, 60.0, 75.0, 85.0, 80.0 };
int size = 400;
RadarChart chart = new RadarChart(title, labels, data, size);
chart.save("RadarChart.png");
}
}
```
主要思路是绘制多边形,然后根据数据计算每个点的位置,最后绘制标签和数据点。可以通过调整 `labels` 和 `data` 数组来生成不同的雷达蛛网图。
阅读全文