|
|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
package xyz.wbsite.jmacro.util;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.convert.Convert;
|
|
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
@ -39,12 +40,12 @@ public class TableData {
|
|
|
|
|
/**
|
|
|
|
|
* 表格表头
|
|
|
|
|
*/
|
|
|
|
|
private final List<String> head = new ArrayList<>();
|
|
|
|
|
private final List<Object> head = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 表格数据行
|
|
|
|
|
*/
|
|
|
|
|
private final List<List<String>> body = new ArrayList<>();
|
|
|
|
|
private final List<List<Object>> body = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 私有构造函数
|
|
|
|
|
@ -79,13 +80,13 @@ public class TableData {
|
|
|
|
|
* @param rowData 行数据数组
|
|
|
|
|
* @return Builder实例
|
|
|
|
|
*/
|
|
|
|
|
public TableData addData(String... rowData) {
|
|
|
|
|
List<String> row = new ArrayList<>(Arrays.asList(rowData));
|
|
|
|
|
public TableData addData(Object... rowData) {
|
|
|
|
|
List<Object> row = new ArrayList<>(Arrays.asList(rowData));
|
|
|
|
|
this.body.add(row);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String[][] convertToArrays() {
|
|
|
|
|
private Object[][] convertToArrays() {
|
|
|
|
|
// 验证数据完整性
|
|
|
|
|
if (head.isEmpty()) {
|
|
|
|
|
throw new IllegalStateException("必须添加表头");
|
|
|
|
|
@ -94,7 +95,7 @@ public class TableData {
|
|
|
|
|
int rows = this.body.size() + 1;
|
|
|
|
|
int cols = this.head.size();
|
|
|
|
|
|
|
|
|
|
String[][] data = new String[rows][cols];
|
|
|
|
|
Object[][] data = new Object[rows][cols];
|
|
|
|
|
|
|
|
|
|
// 填充表头
|
|
|
|
|
for (int j = 0; j < cols; j++) {
|
|
|
|
|
@ -103,7 +104,7 @@ public class TableData {
|
|
|
|
|
|
|
|
|
|
// 填充数据行
|
|
|
|
|
for (int i = 0; i < this.body.size(); i++) {
|
|
|
|
|
List<String> row = this.body.get(i);
|
|
|
|
|
List<Object> row = this.body.get(i);
|
|
|
|
|
for (int j = 0; j < cols; j++) {
|
|
|
|
|
data[i + 1][j] = row.get(j);
|
|
|
|
|
}
|
|
|
|
|
@ -118,7 +119,7 @@ public class TableData {
|
|
|
|
|
* @param data 表格数据
|
|
|
|
|
* @return 每列的宽度数组
|
|
|
|
|
*/
|
|
|
|
|
private int[] calculateColumnWidths(String[][] data) {
|
|
|
|
|
private int[] calculateColumnWidths(Object[][] data) {
|
|
|
|
|
int rows = data.length;
|
|
|
|
|
int cols = data[0].length;
|
|
|
|
|
|
|
|
|
|
@ -133,7 +134,7 @@ public class TableData {
|
|
|
|
|
for (int j = 0; j < cols; j++) {
|
|
|
|
|
int maxWidth = 0;
|
|
|
|
|
for (int i = 0; i < rows; i++) {
|
|
|
|
|
int textWidth = metrics.stringWidth(data[i][j]);
|
|
|
|
|
int textWidth = metrics.stringWidth(Convert.toStr(data[i][j]));
|
|
|
|
|
if (textWidth > maxWidth) {
|
|
|
|
|
maxWidth = textWidth;
|
|
|
|
|
}
|
|
|
|
|
@ -154,7 +155,7 @@ public class TableData {
|
|
|
|
|
*/
|
|
|
|
|
private Dimension calculateTableDimension() {
|
|
|
|
|
// 将数据转换为二维数组格式
|
|
|
|
|
String[][] data = convertToArrays();
|
|
|
|
|
Object[][] data = convertToArrays();
|
|
|
|
|
|
|
|
|
|
int rows = data.length;
|
|
|
|
|
|
|
|
|
|
@ -191,7 +192,7 @@ public class TableData {
|
|
|
|
|
*/
|
|
|
|
|
private BufferedImage drawTable() {
|
|
|
|
|
// 将数据转换为二维数组格式
|
|
|
|
|
String[][] data = convertToArrays();
|
|
|
|
|
Object[][] data = convertToArrays();
|
|
|
|
|
|
|
|
|
|
// 计算自适应的图像尺寸
|
|
|
|
|
Dimension dimension = calculateTableDimension();
|
|
|
|
|
@ -238,7 +239,7 @@ public class TableData {
|
|
|
|
|
g2d.drawRect(x, y, cellWidth, cellHeight);
|
|
|
|
|
|
|
|
|
|
// 绘制单元格内容(居中显示)
|
|
|
|
|
String text = data[i][j];
|
|
|
|
|
String text = Convert.toStr(data[i][j]);
|
|
|
|
|
FontMetrics fm = g2d.getFontMetrics();
|
|
|
|
|
int textX = x + (cellWidth - fm.stringWidth(text)) / 2;
|
|
|
|
|
int textY = y + (cellHeight + fm.getAscent()) / 2 - fm.getDescent();
|
|
|
|
|
|