使用Graphics2D画表格

在机器人需要发表格图片需求,我搜索了一些第三方包,最终使用了java内置的Graphics2D来画表格生成jpg图片,再通过cq语句发送。

表格图片,需要有标题,表格头,表格内容,将表格头和表格前三名设置背景色。

使用Graphics2D画图,需要画横线,竖线,还有字体,其中选择位置进行渲染比较繁琐。所以我将画表格分为几部分来画

1、先定图片大小

图片的高度,等于标题加表格头加表格内容所有高度再加上余留边角部分大概20个像素

图片的宽度,等于表格头的项数加上余留边角部分大概20个像素

 int rows = 0;
        int maxfont = 0;
        if (allValue != null && allValue.size() > 0) {
            rows += (2+allValue.size());
        }
        for (List<String> strings : allValue) {
            maxfont = strings.get(0).length()>maxfont?strings.get(0).length():maxfont;
        }
        // 实际数据行数+标题+备注
    //初始化宽度
        int numwidth = 50;//序号宽度
        int totalrow = 1+rows;//总行数
        int namewidth = maxfont * 22;//名字列宽度
        int otherwidth = 80;//其他列宽度

        int imageWidth = numwidth + namewidth  + 
        otherwidth*(headers.length-2) + 20;//图片总宽度
    //初始化高度
        int imageHeight = totalrow * 30 + 20;//图片总高度
        int rowheight = 30;//行高
    //图片边框留白
        int startHeight = 10;//余留上方10像素
        int startWidth = 10;//余留左方10像素
    //创建Graphics2D对象,用图片缓存流
    BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = image.createGraphics();

2、画背景

通过刚刚计算的图片的高度和宽度,画一张白色的画补作为图片总背景

graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, imageWidth, imageHeight);

再将表格头和前三名的背景画上,通过计算再使用fillRect画上

    //画表头背景
        graphics.setColor(new Color(150,0,0));
        if (allValue != null && allValue.size() > 0) {
            graphics.fillRect(startWidth + 1, startHeight +  rowheight + 1, imageWidth - startWidth - 6, rowheight - 1);
        }
        //画前三名背景
        int redstartH = 2;
        graphics.setColor(new Color(190,25,0));
        for (int temp = 0; temp < allValue.size(); temp++) {
            List strings = allValue.get(temp);
            if (strings != null) {
                graphics.fillRect(startWidth + 1,startHeight + redstartH*rowheight +1 , imageWidth - startWidth - 6,rowheight - 1);
            }
            redstartH++;
            if(temp==2)break;
        }

3、画表格体

画表格体,先画横线再画竖线,通过上方留白高度+行高*n来画横线,通过左方留白宽度+各列宽度来画竖线。

// 画横线
        for (int j = 0; j < totalrow - 1; j++) {
            graphics.setColor(Color.gray);
            graphics.drawLine(startWidth, startHeight + (j + 1) * rowheight, imageWidth - 5,
                    startHeight + (j + 1) * rowheight);
        }
        int rightLine;
        // 画竖线
        graphics.setColor(Color.gray);
        if (allValue != null && allValue.size() > 0) {
            for (int k = 0; k < headers.length+1; k++) {
                rightLine = getRightMargin(k,startWidth, namewidth,otherwidth,imageWidth);
                graphics.drawLine(rightLine, startHeight + rowheight, rightLine,
                        startHeight + (allValue.size()+2)*rowheight);
            }
        }

4、写标题,表头

已计算出每行的高度,把标题写在第一行,表头写在第二行。通过累加行高得出些的位置。

因为标题从第一行开始,表格头从第二行开始,内容从第三行开始,所以设置了startH来控制画图位置

// 设置字体,准备写入文字
        Font font = new Font("宋体", Font.BOLD, 20);
        graphics.setFont(font);
        graphics.setColor(Color.black);
        // 写标题
        if (allValue != null && allValue.size() > 0) {
            graphics.drawString(titles, imageWidth / 3 + startWidth+30, startHeight + rowheight - 10);
        }
        // 写入表头
        int startH = 2;
        graphics.setColor(Color.WHITE);
        font = new Font("宋体", Font.BOLD, 20);
        graphics.setFont(font);
        if (allValue != null && allValue.size() > 0) {
            for (int m = 0; m < headers.length; m++) {
                int strWidth = graphics.getFontMetrics().stringWidth(headers[m]);
                rightLine = getRightMargin(m,startWidth, namewidth,otherwidth,imageWidth);
                if(m==0)
                    rightLine = rightLine + (numwidth-strWidth)/2;
                else if(m==1)
                    rightLine = rightLine + (namewidth-strWidth)/2;
                else
                    rightLine = rightLine + (otherwidth-strWidth)/2;
                graphics.drawString(headers[m], rightLine,
                    startHeight + startH*rowheight  - 10);
            }
        }
        // 写入内容
        startH = 3;
        graphics.setColor(Color.white);
        graphics.setFont(new Font("宋体", Font.BOLD, 20));
        if (allValue != null && allValue.size() > 0) {
            for (int n = 0; n < allValue.size(); n++) {
                if (n == 3) {
                    graphics.setColor(Color.black);
                    graphics.setFont(new Font("宋体", Font.PLAIN, 20));
                }
                List<String> arr = allValue.get(n);
                for (int l = 0; l < arr.size() + 1; l++) {
                    rightLine = getRightMargin(l, startWidth, namewidth, otherwidth, imageWidth) + 5;
                    if (l == 0) {
                        int strWidth = graphics.getFontMetrics().stringWidth(String.valueOf(n + 1));
                        graphics.drawString(String.valueOf(n + 1), rightLine + (numwidth - strWidth) / 2 - 5,
                                startHeight + rowheight * (n + startH) - 10);
                    } else {
                        int strWidth = graphics.getFontMetrics().stringWidth(arr.get(l - 1));
                        if (l == 1)
                            graphics.drawString(arr.get(l - 1), rightLine,
                                    startHeight + rowheight * (n + startH) - 10);
                        else
                            graphics.drawString(arr.get(l - 1), rightLine + (otherwidth - strWidth) / 2 - 5,
                                    startHeight + rowheight * (n + startH) - 10);
                    }
                }
            }
        }

5、消除锯齿

因为2D画图画字体会有锯齿,而graphics2D类有抗锯齿和画笔柔顺的开关,设置如下

   graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        Stroke s = new BasicStroke(imageWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
        graphics.setStroke(s);

6、生成图片

然后创建一个1.jpg将图片的缓存来写出到图片文件中,在项目的相对路径中就有一张图片1.jpg

graphics.drawImage(image.getScaledInstance(imageWidth, imageHeight, Image.SCALE_SMOOTH), 0, 0, null);
        String path = "1.jpg";
        ImageIO.write(image, "jpg", new File(path));

测试画图效果

List<List<List<String>>> allValue = new ArrayList<>();
        List<List<String>> contentArray1 = new ArrayList<>();

        for(int i = 0;i<10;i++){
            contentArray1.add(Arrays.asList(
new String[]{"谭宇","300","5.00","1.00","1234","1234","1234","100"}));
        }

        String[] headTitles = 
new String[]{"序号","名字","成绩","击键","码长","退格","回改","选重","错字"};
        String titles = "跟打成绩";
        try {
            graphicsGeneration(contentArray1,titles,headTitles);
        } catch (Exception e) {
            e.printStackTrace();
        }
image.png

例子源码

package ImgTest;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class imatest {

    public static String graphicsGeneration(List<List<String>> allValue, String titles, String[] headers ) throws Exception {
        int rows = 0;
        int maxfont = 0;
        if (allValue != null && allValue.size() > 0) {
            rows += (2+allValue.size());
        }
        for (List<String> strings : allValue) {
            maxfont = strings.get(0).length()>maxfont?strings.get(0).length():maxfont;
        }
        // 实际数据行数+标题+备注
        int numwidth = 50;
        int totalrow = 1+rows;
        int namewidth = maxfont * 22;
        int otherwidth = 80;

        int imageWidth = numwidth + namewidth  + otherwidth*(headers.length-2) + 20;
        int imageHeight = totalrow * 30 + 20;
        int rowheight = 30;
        int startHeight = 10;
        int startWidth = 10;

        BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = image.createGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, imageWidth, imageHeight);
        //画表头背景
        graphics.setColor(new Color(150,0,0));
        if (allValue != null && allValue.size() > 0) {
            graphics.fillRect(startWidth + 1, startHeight +  rowheight + 1, imageWidth - startWidth - 6, rowheight - 1);
        }
        //画前三名背景
        int redstartH = 2;
        graphics.setColor(new Color(190,25,0));
        for (int temp = 0; temp < allValue.size(); temp++) {
            List strings = allValue.get(temp);
            if (strings != null) {
                graphics.fillRect(startWidth + 1,startHeight + redstartH*rowheight +1 , imageWidth - startWidth - 6,rowheight - 1);
            }
            redstartH++;
            if(temp==2)break;
        }
        // 画横线
        for (int j = 0; j < totalrow - 1; j++) {
            graphics.setColor(Color.gray);
            graphics.drawLine(startWidth, startHeight + (j + 1) * rowheight, imageWidth - 5,
                    startHeight + (j + 1) * rowheight);
        }
        int rightLine;
        // 画竖线
        graphics.setColor(Color.gray);
        if (allValue != null && allValue.size() > 0) {
            for (int k = 0; k < headers.length+1; k++) {
                rightLine = getRightMargin(k,startWidth, namewidth,otherwidth,imageWidth);
                graphics.drawLine(rightLine, startHeight + rowheight, rightLine,
                        startHeight + (allValue.size()+2)*rowheight);
            }
        }
        // 设置字体,准备写入文字
        Font font = new Font("宋体", Font.BOLD, 20);
        graphics.setFont(font);
        graphics.setColor(Color.black);
        // 写标题
        if (allValue != null && allValue.size() > 0) {
            graphics.drawString(titles, imageWidth / 3 + startWidth+30, startHeight + rowheight - 10);
        }
        // 写入表头
        int startH = 2;
        graphics.setColor(Color.WHITE);
        font = new Font("宋体", Font.BOLD, 20);
        graphics.setFont(font);
        if (allValue != null && allValue.size() > 0) {
            for (int m = 0; m < headers.length; m++) {
                int strWidth = graphics.getFontMetrics().stringWidth(headers[m]);
                rightLine = getRightMargin(m,startWidth, namewidth,otherwidth,imageWidth);
                if(m==0)
                    rightLine = rightLine + (numwidth-strWidth)/2;
                else if(m==1)
                    rightLine = rightLine + (namewidth-strWidth)/2;
                else
                    rightLine = rightLine + (otherwidth-strWidth)/2;
                graphics.drawString(headers[m], rightLine,
                    startHeight + startH*rowheight  - 10);
            }
        }
        // 写入内容
        startH = 3;
        graphics.setColor(Color.white);
        graphics.setFont(new Font("宋体", Font.BOLD, 20));
        if (allValue != null && allValue.size() > 0) {
            for (int n = 0; n < allValue.size(); n++) {
                if (n == 3) {
                    graphics.setColor(Color.black);
                    graphics.setFont(new Font("宋体", Font.PLAIN, 20));
                }
                List<String> arr = allValue.get(n);
                for (int l = 0; l < arr.size() + 1; l++) {
                    rightLine = getRightMargin(l, startWidth, namewidth, otherwidth, imageWidth) + 5;
                    if (l == 0) {
                        int strWidth = graphics.getFontMetrics().stringWidth(String.valueOf(n + 1));
                        graphics.drawString(String.valueOf(n + 1), rightLine + (numwidth - strWidth) / 2 - 5,
                                startHeight + rowheight * (n + startH) - 10);
                    } else {
                        int strWidth = graphics.getFontMetrics().stringWidth(arr.get(l - 1));
                        if (l == 1)
                            graphics.drawString(arr.get(l - 1), rightLine,
                                    startHeight + rowheight * (n + startH) - 10);
                        else
                            graphics.drawString(arr.get(l - 1), rightLine + (otherwidth - strWidth) / 2 - 5,
                                    startHeight + rowheight * (n + startH) - 10);
                    }
                }
            }
        }

        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        Stroke s = new BasicStroke(imageWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
        graphics.setStroke(s);

        graphics.drawImage(image.getScaledInstance(imageWidth, imageHeight, Image.SCALE_SMOOTH), 0, 0, null);
        String path = "1.jpg";
        ImageIO.write(image, "jpg", new File(path));
        return path;
    }

    /**
     * 获取竖线和文字的水平位置
     * @param k
     * @param startWidth
     * @param namewidth
     * @param otherwidth
     * @param imageWidth
     * @return
     */
    private static int getRightMargin(int k, int startWidth, int namewidth,int otherwidth, int imageWidth) {
        int rightLine = 0;
        if (k == 0) {
            rightLine = startWidth;
        } else if (k == 1) {
            rightLine = startWidth + 50;
        } else if (k == 2) {
            rightLine = startWidth + 50 + namewidth;
        } else if (k >= 3 &&k<9) {
            rightLine = startWidth + +50 + namewidth + (k - 2) * otherwidth;
        } else if (k == 9)
            rightLine = imageWidth - 5;
        return rightLine;
    }
    public static void initChartData(){
        List<List<String>> contentArray1 = new ArrayList<>();

        for(int i = 0;i<10;i++){
            contentArray1.add(Arrays.asList(new String[]{"谭宇","300","5.00","1.00","1234","1234","1234","100"}));
        }

        String[] headTitles = new String[]{"序号","名字","成绩","击键","码长","退格","回改","选重","错字"};

        String titles = "跟打成绩";
        try {
            graphicsGeneration(contentArray1,titles,headTitles);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        initChartData();
    }
}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,843评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,538评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,187评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,264评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,289评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,231评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,116评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,945评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,367评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,581评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,754评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,458评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,068评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,692评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,842评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,797评论 2 369
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,654评论 2 354

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,402评论 0 17
  • 今天主要总结了latex的一些基本配置和使用latex进行科技论文写作的基本要素(书写文档,插入表格,插入图片,插...
    taozhaojun阅读 13,132评论 1 22
  • 一,HTML语言的一般语法: 1,围堵标记:<>… 1)带属性的标记: … 2)无属性的标记:加粗 居中 标题 2...
    清水易蓝阅读 1,264评论 0 2
  • 1.问:WORD里边怎样设置每页不同的页眉?如何使不同的章节显示的页眉不同? 答:分节,每节可以设置不同的页眉。文...
    勤奋的keena阅读 1,766评论 0 10
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,751评论 1 92