Java使用Graphics2D刻画六边形菱形蛛网图并生成图片,包含pom文件
时间: 2024-03-19 19:43:32 浏览: 131
好的,以下是Java使用Graphics2D刻画六边形菱形蛛网图并生成图片的代码及pom文件:
代码:
```java
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class HexagonWeb {
public static void main(String[] args) throws IOException {
int width = 600;
int height = 600;
int centerX = width / 2;
int centerY = height / 2;
int radius = 260;
int sides = 6;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(2));
for (int i = 0; i < sides; i++) {
int x = (int) (centerX + radius * Math.cos(i * 2 * Math.PI / sides));
int y = (int) (centerY + radius * Math.sin(i * 2 * Math.PI / sides));
g2d.drawLine(centerX, centerY, x, y);
for (int j = 1; j < 5; j++) {
int x1 = (int) (centerX + radius / 5 * j * Math.cos(i * 2 * Math.PI / sides));
int y1 = (int) (centerY + radius / 5 * j * Math.sin(i * 2 * Math.PI / sides));
int x2 = (int) (centerX + radius / 5 * j * Math.cos((i + 1) * 2 * Math.PI / sides));
int y2 = (int) (centerY + radius / 5 * j * Math.sin((i + 1) * 2 * Math.PI / sides));
g2d.drawLine(x1, y1, x2, y2);
}
}
g2d.dispose();
File output = new File("hexagonweb.png");
ImageIO.write(image, "png", output);
}
}
```
pom文件:
```xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>hexagon-web</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.swinglabs</groupId>
<artifactId>swingx-all</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>
</project>
```
注意:这里使用了swingx-all依赖,如果你的项目已经有了swingx-all依赖,可以省略这个依赖。
阅读全文