java画图像表格

直接上代码

画线的精髓在这

        // 字体大小
        int fontTitileSize = 12;
        // 横线的行数
        int totalrow = 16;
        // 竖线的行数
        int totalcol = 10;
        // 图片宽度
        int imageWidth = 700;
        // 行高
        int rowheight = 38;
        // 图片高度
        int imageHeight = totalrow*rowheight+41;
        // 起始高度
        int startHeight = 70;
        // 起始宽度
        int startWidth = 0;
        // 单元格宽度
        int colwidth = 60;

// 初始化画笔
 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(220,240,240));

        int tmpWidth = 5;

        graphics.setColor(Color.black);
        graphics.drawLine(startWidth, startHeight, startWidth+10*colwidth, startHeight); 
//划线是根据开始坐标和结束坐标两点画一条线的。
//纵坐标不变画的是一条横线
// 横坐标不变画的是一条竖线,如下
 graphics.drawLine(startWidth+10*colwidth, startHeight, startWidth+10*colwidth, startHeight+15*rowheight);






画完线之后,如何写上文本呢

 //设置字体
        Font font = new Font("微软雅黑",Font.PLAIN,fontTitileSize);
        graphics.setFont(font);
       graphics.drawString("文本内容", 0, 40);
//  什么意思呢,即 在(0,40)这个点的位置,写文本


最后生成图片

调用 createImage(image, path);


public static void createImage(BufferedImage image, String fileLocation) {
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
    
        fos = new FileOutputStream(fileLocation);
        bos = new BufferedOutputStream(fos);
//            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
//            encoder.encode(image);
            ImageIO.write(image,"jpg",bos)

    }

起初,需要用到com.sum.image.codec.jpeg.JPEGImageEncoder这个类,但是这个是rt.jar包中的sun私有包,涉及到版权的问题,关键是一些大公司使用的是openjdk,这个openjdk简单的理解就是开源精简版,它是没有sun这个包的,那么就会出现java.lang.NoclassDefFoundError这个错误,在openjdk下,建议用


java画图像表格

效果图就不给大家看了,有兴趣的,可以自己试试。

展开阅读全文

页面更新:2024-03-02

标签:纵坐标   横坐标   竖线   横线   微软   画笔   画图   字体大小   初始化   坐标   效果图   精髓   宽度   表格   文本   高度   图片   科技

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top