ava使用Graphics2D刻画六边形菱形蛛网图,样式类似游戏人物属性能力图,所有能力项再在蛛网图内绘制一幅图,所有代码
时间: 2024-04-05 16:32:54 浏览: 209
好的,请看下面的代码。这里提供了一个完整的例子,包括绘制蛛网图和能力项图形。
```
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SpiderWebExample extends JPanel {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
private static final int PADDING = 50;
private static final int AXIS_NUM = 6;
private static final String[] LABELS = { "力量", "敏捷", "体质", "智力", "精神", "幸运" };
// 数据数组
private int[] data = { 4, 3, 2, 4, 5, 3 };
public SpiderWebExample() {
setPreferredSize(new java.awt.Dimension(WIDTH, HEIGHT));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// 设置绘图属性
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(2));
// 计算多边形的顶点坐标
Point[] points = getPolygonPoints(AXIS_NUM, getWidth() / 2 - PADDING);
// 绘制轴线
for (int i = 0; i < AXIS_NUM; i++) {
g2d.draw(new Line2D.Double(getWidth() / 2, getHeight() / 2, points[i].x, points[i].y));
}
// 绘制多边形
Polygon polygon = new Polygon();
for (int i = 0; i < AXIS_NUM; i++) {
int x = (int) (getWidth() / 2 + data[i] * (getWidth() / 2 - PADDING) / 5 * Math.cos(2 * Math.PI / AXIS_NUM * i));
int y = (int) (getHeight() / 2 + data[i] * (getHeight() / 2 - PADDING) / 5 * Math.sin(2 * Math.PI / AXIS_NUM * i));
polygon.addPoint(x, y);
}
g2d.draw(polygon);
// 绘制数据点
for (int i = 0; i < AXIS_NUM; i++) {
int x = (int) (getWidth() / 2 + data[i] * (getWidth() / 2 - PADDING) / 5 * Math.cos(2 * Math.PI / AXIS_NUM * i));
int y = (int) (getHeight() / 2 + data[i] * (getHeight() / 2 - PADDING) / 5 * Math.sin(2 * Math.PI / AXIS_NUM * i));
g2d.fill(new Ellipse2D.Double(x - 4, y - 4, 8, 8));
}
// 绘制标签
for (int i = 0; i < AXIS_NUM; i++) {
int x = (int) (getWidth() / 2 + (getWidth() / 2 - PADDING) * Math.cos(2 * Math.PI / AXIS_NUM * i));
int y = (int) (getHeight() / 2 + (getHeight() / 2 - PADDING) * Math.sin(2 * Math.PI / AXIS_NUM * i));
g2d.drawString(LABELS[i], x, y);
}
// 绘制能力项图形
for (int i = 0; i < AXIS_NUM; i++) {
Shape shape = null;
switch (i) {
case 0:
shape = new Rectangle2D.Double(0, 0, 50, 50);
break;
case 1:
shape = new Ellipse2D.Double(0, 0, 50, 50);
break;
case 2:
shape = new Polygon(new int[] { 0, 25, 50 }, new int[] { 0, 50, 0 }, 3);
break;
case 3:
shape = new Path2D.Double();
((Path2D) shape).moveTo(0, 0);
((Path2D) shape).lineTo(50, 50);
((Path2D) shape).moveTo(50, 0);
((Path2D) shape).lineTo(0, 50);
break;
case 4:
shape = new Ellipse2D.Double(0, 0, 50, 25);
break;
case 5:
shape = new Polygon(new int[] { 0, 25, 50, 25 }, new int[] { 0, 25, 0, 25 }, 4);
break;
}
// 根据数据计算变换矩阵
AffineTransform transform = new AffineTransform();
transform.translate(getWidth() / 2, getHeight() / 2);
transform.rotate(2 * Math.PI / AXIS_NUM * i);
transform.translate((getWidth() / 2 - PADDING) * data[i] / 5, 0);
// 应用变换矩阵并绘制图形
g2d.setColor(new Color(0, 0, 255, 50));
g2d.fill(transform.createTransformedShape(shape));
g2d.setColor(Color.BLACK);
g2d.draw(transform.createTransformedShape(shape));
}
}
// 获取多边形的顶点坐标
private Point[] getPolygonPoints(int n, int r) {
Point[] points = new Point[n];
double angle = 2 * Math.PI / n;
for (int i = 0; i < n; i++) {
int x = (int) (getWidth() / 2 + r * Math.cos(angle * i));
int y = (int) (getHeight() / 2 + r * Math.sin(angle * i));
points[i] = new Point(x, y);
}
return points;
}
public static void main(String[] args) {
JFrame frame = new JFrame("Spider Web Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SpiderWebExample());
frame.pack();
frame.setVisible(true);
}
}
```
这个例子中,我们创建了一个自定义的面板类SpiderWebExample,用于绘制蛛网图和能力项图形。在paintComponent()方法中,我们按照前面所述的步骤,先绘制蛛网图,然后再绘制能力项图形。对于能力项图形,我们使用了不同的形状,包括矩形、椭圆、三角形、十字形、椭圆弧和菱形等,并根据数据计算出变换矩阵,将其应用到图形上。
运行这个例子,你就可以看到一个类似游戏人物属性能力图的六边形蛛网图,以及在其中绘制的能力项图形了。
阅读全文