java使用Graphics2D刻画六边形菱形蛛网图,样式类似游戏人物属性能力图,所有能力项再在蛛网图内绘制一幅图,所有代码并生成图片
时间: 2024-04-05 12:32:43 浏览: 286
java Graphics2D 画流程图全代码,包含封装类工具类枚举的全部应用 并生成图片
好的,我来为您提供Java代码。首先,我们需要创建一个 `SpiderWeb` 类,用于绘制蛛网图。代码如下:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import javax.swing.JPanel;
public class SpiderWeb extends JPanel {
private static final long serialVersionUID = 1L;
private int[] values;
private String[] labels;
private int maxValue;
private int sides;
public SpiderWeb(int[] values, String[] labels, int maxValue) {
this.values = values;
this.labels = labels;
this.maxValue = maxValue;
this.sides = values.length;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
// 计算每个顶点的坐标
Point[] points = new Point[sides];
double angle = Math.PI * 2 / sides;
for (int i = 0; i < sides; i++) {
double theta = angle * i;
int x = (int) (centerX + Math.cos(theta) * centerX);
int y = (int) (centerY + Math.sin(theta) * centerY);
points[i] = new Point(x, y);
}
// 绘制六边形
Polygon hexagon = new Polygon();
for (Point p : points) {
hexagon.addPoint(p.x, p.y);
}
g2d.setColor(Color.GRAY);
g2d.drawPolygon(hexagon);
// 绘制蛛网线
for (int i = 0; i < sides; i++) {
g2d.drawLine(centerX, centerY, points[i].x, points[i].y);
}
// 绘制数据点
Polygon dataPolygon = new Polygon();
g2d.setColor(Color.BLUE);
for (int i = 0; i < sides; i++) {
double percent = (double) values[i] / maxValue;
int x = (int) (centerX + Math.cos(angle * i) * centerX * percent);
int y = (int) (centerY + Math.sin(angle * i) * centerY * percent);
dataPolygon.addPoint(x, y);
g2d.fillOval(x - 5, y - 5, 10, 10);
}
g2d.setColor(Color.BLUE);
g2d.drawPolygon(dataPolygon);
// 绘制标签
for (int i = 0; i < sides; i++) {
double theta = angle * i;
int x = (int) (centerX + Math.cos(theta) * centerX * 1.1);
int y = (int) (centerY + Math.sin(theta) * centerY * 1.1);
if (theta >= 0 && theta < Math.PI / 2) {
g2d.drawString(labels[i], x, y + 15);
} else if (theta >= Math.PI / 2 && theta < Math.PI) {
g2d.drawString(labels[i], x - g2d.getFontMetrics().stringWidth(labels[i]), y + 15);
} else if (theta >= Math.PI && theta < Math.PI * 3 / 2) {
g2d.drawString(labels[i], x - g2d.getFontMetrics().stringWidth(labels[i]), y - 5);
} else {
g2d.drawString(labels[i], x, y - 5);
}
}
g2d.dispose();
}
}
```
接下来,我们需要创建一个测试类,用于生成蛛网图。代码如下:
```java
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SpiderWebTest {
public static void main(String[] args) {
// 6项属性值
int[] values = { 90, 70, 80, 60, 50, 70 };
// 6项属性标签
String[] labels = { "力量", "敏捷", "体质", "智力", "精神", "幸运" };
// 最大属性值
int maxValue = 100;
SpiderWeb spiderWeb = new SpiderWeb(values, labels, maxValue);
// 生成图片
BufferedImage image = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB);
spiderWeb.paint(image.getGraphics());
// 保存图片
try {
File file = new File("spider-web.png");
ImageIO.write(image, "png", file);
System.out.println("已保存图片:" + file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
将以上两段代码保存到同一个项目中,运行 `SpiderWebTest` 类即可生成蛛网图。生成的图片将保存在项目根目录下的 `spider-web.png` 文件中。
阅读全文