Java 实现游戏综合战力六边形雷达蛛网图,每边都有标题,可修改网图和数据的背景颜色以及字体,并输出为图片,包含所有代码
时间: 2024-04-05 13:31:59 浏览: 24
好的,下面是实现游戏综合战力六边形雷达蛛网图的 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;
private final Color bgColor;
private final Color labelColor;
private final Color dataColor;
private final Font titleFont;
private final Font labelFont;
private final Font dataFont;
public RadarChart(String title, String[] labels, double[] data, int size, Color bgColor, Color labelColor, Color dataColor, Font titleFont, Font labelFont, Font dataFont) {
this.size = size;
this.data = data;
this.labels = labels;
this.title = title;
this.bgColor = bgColor;
this.labelColor = labelColor;
this.dataColor = dataColor;
this.titleFont = titleFont;
this.labelFont = labelFont;
this.dataFont = dataFont;
}
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(bgColor);
g.fillRect(0, 0, size, size);
g.setColor(labelColor);
g.setFont(titleFont);
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(labelColor);
g.setFont(labelFont);
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(dataColor);
g.fillOval(px - 3, py - 3, 6, 6);
g.setColor(labelColor);
}
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;
Color bgColor = Color.WHITE;
Color labelColor = Color.BLACK;
Color dataColor = Color.BLUE;
Font titleFont = new Font("Arial", Font.BOLD, 14);
Font labelFont = new Font("Arial", Font.PLAIN, 12);
Font dataFont = new Font("Arial", Font.PLAIN, 12);
RadarChart chart = new RadarChart(title, labels, data, size, bgColor, labelColor, dataColor, titleFont, labelFont, dataFont);
chart.save("RadarChart.png");
}
}
```
主要思路和之前的实现类似,只是添加了更多的参数,可以通过调整这些参数来自定义雷达蛛网图的样式。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)