请写出用Graphics2D类的新方法画一个图形的步骤(表格如何画斜线)

在机器人需要发表格图片需求,我搜索了一些第三方包,最终使用了java内置的Graphics2D来画表格生成jpg图片,再通过cq语句发送。表格图片,需要有标题,表格头,表格内容,将表格头和表格前三名设置背景色。使用Graphics2D画图,需要画横线,竖线,还有字体,其中选择位置进行渲染比较繁琐。所以我将画表格分为几部分来画1、先定图片大小图片的高…

大家好,又见面了,我是你们的朋友全栈君。

在机器人需要发表格图片需求,我搜索了一些第三方包,最终使用了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();
        }
14899865-56be6c0dad522194.png

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();
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/129178.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Recall(检出率)和 Precision(准确性)

    Recall(检出率)和 Precision(准确性)

    2022年1月1日
    44
  • Eclipse:Target runtime XXX is not defined

    Eclipse:Target runtime XXX is not defined

    2021年10月4日
    44
  • 2021纪念品csgo_csgo最便宜的开箱网站

    2021纪念品csgo_csgo最便宜的开箱网站2021已知目前最全的国内CSGO开箱网站大全!!incsgo国内CSGO饰品皮肤开箱网站官方链接:www.incsgo.gg注册登录自动免费获得$1.00美金取回状态:直接取回**优惠码:**csgogo(充值使用csgogo可增加5%充值金额)skinsdog狗网CSGO饰品皮肤开箱网站可直接取回官方链接:skinsdog.cc注册登录自动免费获得$0.8美金取回状态:直接取回**优惠码:**csgogo(注册使用送0.8美金)coolkaixiang.

    2022年10月6日
    0
  • finalize方法_final与finalize区别

    finalize方法_final与finalize区别当对象没有引用指向时,虚拟机会按照一定的垃圾回收机制算法来调用finalize方法将该对象回收,并不是只要没有引用对象就会被回收。因此,可以调用System.gc()方法来主动调用垃圾回收机制,但也并不能保证一定能成功。在调用时,程序并不会阻塞在此处,而是会继续向下执行。默认的object类中的finalize方法是不作其余处理的。可以重写finalize方法来实现自己想要的资源释放操作,比如数据库连接等。…

    2022年9月19日
    0
  • goland 2021激活码_最新在线免费激活[通俗易懂]

    (goland 2021激活码)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlS32PGH0SQB-eyJsaWNlbnNlSWQi…

    2022年3月26日
    68
  • ZIP 压缩解压命令

    ZIP 压缩解压命令压缩一个文件,打开cmd窗口 zip f:\wenjian.zip f:\wenjian.txt压缩一种格式的文件 zip f:\wenjian.zip f:\*.txt压缩文件夹及里面的内容 -r表示压缩文件夹wenjian及里面的内容 zip-rf:\wenjian.zip wenjian  移除压缩包里的一

    2022年5月16日
    53

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号