Java jdk生成テーブルピクチャ
6832 ワード
public static String graphicsGeneration(List>> allValue,List titles,List headers ,String receiver,int totalcol) throws Exception {
int rows = 0;
for (List> typeV : allValue) {
if (typeV != null && typeV.size() > 0) {
rows += (2+typeV.size());
}
}
// + +
int totalrow = 1+rows;
int imageWidth = 800;
int imageHeight = totalrow * 30 + 20;
int rowheight = 30;
int startHeight = 10;
int startWidth = 10;
int colwidth = ((imageWidth - 20) / totalcol);
BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
Graphics graphics = image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, imageWidth, imageHeight);
//
graphics.setColor(new Color(0, 112, 192));
int startH = 1;
for (List> typeV : allValue) {
if (typeV != null && typeV.size() > 0) {
graphics.fillRect(startWidth+1, startHeight+startH*rowheight+1, imageWidth - startWidth-5-1,rowheight-1);
startH+=2+typeV.size();
}
}
graphics.setColor(new Color(220, 240, 240));
//
for (int j = 0; j < totalrow - 1; j++) {
graphics.setColor(Color.black);
graphics.drawLine(startWidth, startHeight + (j + 1) * rowheight, imageWidth - 5,
startHeight + (j + 1) * rowheight);
}
//
graphics.setColor(Color.black);
startH = 1;
int rightLine = 0 ;
for (List> typeV : allValue) {
if (typeV != null && typeV.size() > 0) {
for (int k = 0; k < totalcol+1; k++) {
rightLine = getRightMargin(k,startWidth, colwidth,imageWidth);
graphics.drawLine(rightLine, startHeight + startH*rowheight, rightLine,
startHeight + (typeV.size()+1+startH)*rowheight);
}
startH+=2+typeV.size();
}
}
//
Font font = new Font(" ", Font.BOLD, 20);
graphics.setFont(font);
//
startH = 1;
int i = 0;
for (List> typeV : allValue) {
if (typeV != null && typeV.size() > 0) {
graphics.drawString(titles.get(i), imageWidth / 3 + startWidth+30, startHeight + startH*rowheight - 10);
startH+=2+typeV.size();
}
i++;
}
//
graphics.setColor(Color.WHITE);
font = new Font(" ", Font.BOLD, 20);
graphics.setFont(font);
startH = 2;
i = 0;
for (List> typeV : allValue) {
if (typeV != null && typeV.size() > 0) {
String[] headCells = headers.get(i);
for (int m = 0; m < headCells.length; m++) {
rightLine = getRightMargin(m,startWidth, colwidth,imageWidth)+5;
graphics.drawString(headCells[m].toString(), rightLine,
startHeight + rowheight * startH - 10);
}
startH+=2+typeV.size();
}
i++;
}
//
graphics.setColor(Color.black);
font = new Font(" ", Font.PLAIN, 20);
graphics.setFont(font);
startH = 3;
i = 0;
for (List> typeV : allValue) {
if (typeV != null && typeV.size() > 0) {
for (int n = 0; n < typeV.size(); n++) {
List arr = typeV.get(n);
for (int l = 0; l < arr.size(); l++) {
rightLine = getRightMargin(l,startWidth, colwidth,imageWidth)+5;
graphics.drawString(arr.get(l).toString(), rightLine,
startHeight + rowheight * (n + startH) - 10);
}
}
startH+=2+typeV.size();
}
i++;
}
String path = "1.jpg";
ImageIO.write(image, "jpg", new File(path));
return path;
}
/**
*
* @param k
* @param startWidth
* @param colwidth
* @param imageWidth
* @return
*/
private static int getRightMargin(int k, int startWidth, int colwidth, int imageWidth) {
int rightLine = 0;
if (k == 0) {
rightLine = startWidth;
} else if (k == 1) {
rightLine = startWidth + colwidth / 2;
} else if (k == 2) {
rightLine = startWidth + 3 * colwidth / 2;
} else if (k == 3) {
rightLine = startWidth + 7 * colwidth / 2;
} else if (k == 4) {
rightLine = imageWidth - 5;
}
return rightLine;
}
2.グラフデータの転送
public void initChartData(){
List>> allValue = new ArrayList<>();
List content1 =Arrays.asList(new String[]{" ","25","163cm"," "});
List content2 = Arrays.asList(new String[]{" ","25","163cm"," "});
List content3 = Arrays.asList(new String[]{" "," "," "," "});
List> contentArray1 = new ArrayList<>();
contentArray1.add(content1);
contentArray1.add(content2);
List> contentArray2 = new ArrayList<>();
contentArray2.add(content3);
allValue.add(contentArray1);
allValue.add(contentArray2);
List headTitles = new ArrayList<>();
String[] h1 = new String[]{" "," "," "," "};
String[] h2 = new String[]{" "," "," "," "};
headTitles.add(h1);
headTitles.add(h2);
List titles = new ArrayList<>();
titles.add(" ");
titles.add("SQE ");
graphicsGeneration(allValue,titles,headTitles ,"",4)
}
3.テストコード
public class ImageTest {
public static void main(String[] args) {
initChartData();
}
}
実行すると、生成されたグラフ画像が表示されます.