上传备份

master
王兵 2 weeks ago
parent cb2f87b9fa
commit e6224d4111

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

Loading…
Cancel
Save

Powered by TurnKey Linux.