java applet 雷达图
时间: 2023-10-30 15:04:55 浏览: 89
Java Applet 是一种运行在网页上的小程序,可以实现动态效果和交互功能。要画雷达图,可以使用Java Graphics2D API来绘制图形,其中包括多边形、线条、文本等元素。以下是一个简单的Java Applet雷达图示例代码:
```java
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
public class RadarChart extends Applet {
private int centerX, centerY; //雷达图中心坐标
private int radius; //雷达图半径
private String[] labels = {"A","B","C","D","E","F"}; //雷达图各维度标签
private int[] data = {80, 60, 50, 40, 70, 90}; //雷达图各维度数据
public void init() {
setBackground(Color.white);
setSize(400, 400);
centerX = getWidth() / 2;
centerY = getHeight() / 2;
radius = (int) (Math.min(getWidth(), getHeight()) * 0.4);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.black);
// 画雷达图多边形
for (int i = 0; i < labels.length; i++) {
int x = (int) (centerX + radius * Math.sin(i * 2 * Math.PI / labels.length));
int y = (int) (centerY - radius * Math.cos(i * 2 * Math.PI / labels.length));
g2d.drawLine(centerX, centerY, x, y);
}
// 画雷达图数据区域
g2d.setColor(Color.blue);
Polygon dataArea = new Polygon();
for (int i = 0; i < labels.length; i++) {
int x = (int) (centerX + data[i] / 100.0 * radius * Math.sin(i * 2 * Math.PI / labels.length));
int y = (int) (centerY - data[i] / 100.0 * radius * Math.cos(i * 2 * Math.PI / labels.length));
dataArea.addPoint(x, y);
}
g2d.fill(dataArea);
// 画雷达图各维度标签
g2d.setColor(Color.black);
for (int i = 0; i < labels.length; i++) {
int x = (int) (centerX + (radius + 20) * Math.sin(i * 2 * Math.PI / labels.length));
int y = (int) (centerY - (radius + 20) * Math.cos(i * 2 * Math.PI / labels.length));
g2d.drawString(labels[i], x, y);
}
}
}
```
在HTML文件中,需要将Applet标签引用Java Applet程序,并设置参数:
```html
<applet code="RadarChart.class" width="400" height="400">
</applet>
```
这样就可以在网页上显示雷达图了。
阅读全文