Java 绘制六边形蛛网战力图包含竖线,每个角都有标题,动态能力项绘制未一个不规则的形状并设置背景色为淡蓝色,并输出为图片
时间: 2024-03-30 07:40:51 浏览: 150
制作六边形图片
5星 · 资源好评率100%
好的,这是一个比较复杂的问题,需要一步一步地完成。首先,我们需要使用 Java 的绘图功能来绘制六边形蛛网图。这里我使用 JavaFX 来完成。具体实现如下:
```java
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class SpiderChart extends Application {
private static final int WIDTH = 600;
private static final int HEIGHT = 600;
private static final int RADIUS = 200;
private static final int SIDES = 6;
private static final String[] TITLES = {"力量", "敏捷", "耐力", "智力", "精神", "幸运"};
private static final double[] ABILITIES = {0.8, 0.6, 0.9, 0.7, 0.5, 0.4};
@Override
public void start(Stage primaryStage) throws Exception {
Group root = new Group();
Scene scene = new Scene(root, WIDTH, HEIGHT);
primaryStage.setScene(scene);
Canvas canvas = new Canvas(WIDTH, HEIGHT);
GraphicsContext gc = canvas.getGraphicsContext2D();
root.getChildren().add(canvas);
gc.setStroke(Color.BLACK);
gc.setLineWidth(2);
// 绘制六边形
double centerX = WIDTH / 2;
double centerY = HEIGHT / 2;
double angle = 2 * Math.PI / SIDES;
double[] xs = new double[SIDES];
double[] ys = new double[SIDES];
for (int i = 0; i < SIDES; i++) {
double x = centerX + RADIUS * Math.cos(i * angle);
double y = centerY + RADIUS * Math.sin(i * angle);
xs[i] = x;
ys[i] = y;
gc.strokeLine(centerX, centerY, x, y);
}
for (int i = 0; i < SIDES; i++) {
gc.strokeLine(xs[i], ys[i], xs[(i + 1) % SIDES], ys[(i + 1) % SIDES]);
}
// 绘制竖线
gc.setStroke(Color.LIGHTGRAY);
for (int i = 0; i < SIDES; i++) {
gc.strokeLine(centerX, centerY, xs[i], ys[i]);
}
// 绘制能力项
gc.setFill(Color.rgb(0, 191, 255, 0.3));
gc.setStroke(Color.rgb(0, 191, 255, 0.8));
gc.setLineWidth(3);
for (int i = 0; i < SIDES; i++) {
double x1 = centerX + ABILITIES[i] * RADIUS * Math.cos(i * angle - angle / 2);
double y1 = centerY + ABILITIES[i] * RADIUS * Math.sin(i * angle - angle / 2);
double x2 = centerX + ABILITIES[i] * RADIUS * Math.cos(i * angle + angle / 2);
double y2 = centerY + ABILITIES[i] * RADIUS * Math.sin(i * angle + angle / 2);
double[] xPoints = {xs[i], x1, centerX, x2};
double[] yPoints = {ys[i], y1, centerY, y2};
gc.strokePolygon(xPoints, yPoints, 4);
gc.fillPolygon(xPoints, yPoints, 4);
}
// 绘制标题
gc.setFill(Color.BLACK);
gc.setFont(gc.getFont().font(16));
for (int i = 0; i < SIDES; i++) {
double x = centerX + RADIUS * Math.cos(i * angle);
double y = centerY + RADIUS * Math.sin(i * angle);
gc.fillText(TITLES[i], x, y);
}
// 输出为图片
ImageUtils.saveImage(canvas.snapshot(null, null), "spider_chart.png");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
上面的代码中,我使用了一个 ImageUtils 工具类来保存图像,具体实现可以参考下面的代码:
```java
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.Image;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
public class ImageUtils {
public static void saveImage(Image image, String filename) throws IOException {
File file = new File(filename);
ImageIO.write(SwingFXUtils.fromFXImage(image, null), "PNG", file);
}
}
```
这样,我们就可以将六边形蛛网图输出为图片了。
阅读全文