java + Graphics2D 绘制表单

思路:布局出最外围的形状,分割成几个部分,目的是为了基于最外围固定坐标,然后具体的内容根据固定好的进行 加减宽高 画图,当最外围的宽高出现变更时,内容不串行

public String test(String uploadPath, String fileName) throws IOException {
        int width = 2970;
        int height = 2100;
        int lineMargin = 200;//线条两边的间距
        int textMargin = lineMargin + 100;//正文左间距
        double topMargin = 0.05;
        Font font = new Font("微软雅黑", Font.BOLD, 50);//标题
        Font fontText = new Font("微软雅黑", Font.PLAIN, 40);//正文

        String prescriptionTitle = "标题";
        String str1 = "文本1111";
        //得到图片缓冲区
        BufferedImage bi = new BufferedImage
                (width, height, BufferedImage.TYPE_INT_RGB);//INT精确度达到一定,RGB三原色,高度70,宽度150

        Graphics2D g2 = (Graphics2D) bi.getGraphics();

        //设置颜色
        g2.setColor(Color.WHITE);
        g2.fillRect(0, 0, width, height);//填充整张图片(设置背景颜色)
        float d = 4;
        g2.setStroke(new BasicStroke(d));

        g2.setColor(Color.BLACK);//设置字体颜色
        g2.setFont(font); //设置字体:字体、字号、大小

        //标题
        FontMetrics fontMetrics = g2.getFontMetrics(font);
        // 计算出中心点 x 位置
        int centerX = width / 2;
        // 文字宽度-标题
        int textWidth = fontMetrics.stringWidth(prescriptionTitle);
        g2.drawString(prescriptionTitle, centerX - textWidth / 2, (int) (height * 0.08));
        // 文字宽度
        int textWidth1 = fontMetrics.stringWidth(str1);
        g2.drawString(str1, centerX - textWidth1 / 2, (int) (height * 0.12));

        //右上方:正方形(X坐标,Y坐标,宽,高)
        Rectangle2D rectangle2D = new Rectangle2D.Float(width / 4 * 3, (int) (height * 0.04), 200, 200);
        g2.draw(rectangle2D);

        //正方形内的文字
        g2.drawString("正方形内的文字", width / 4 * 3 + 10, (int) (height * 0.08));

        g2.setFont(fontText); //设置字体:字体、字号、大小
        //画线条:2 基数 0.17
        double double2 = 0.17;
        Line2D line2D1 = new Line2D.Float(lineMargin, (int) (height * double2), width - lineMargin, (int) (height * double2));
        g2.draw(line2D1);//画线条

        //画线条 上间距+5
        Line2D line2D2 = new Line2D.Float(lineMargin, (int) (height * (double2 + topMargin * 3)), width - lineMargin, (int) (height * (double2 + topMargin * 3)));
        g2.draw(line2D2);

        File f = new File(uploadPath);
        // 判断文件父目录是否存在
        if (!f.exists()) {
            f.mkdirs();
        }

        ImageIO.write(bi, "JPEG", new FileOutputStream(f + "/" + fileName));//保存图片 JPEG表示保存格式
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String scheme = request.getScheme();//http
        String serverName = request.getServerName();//localhost
        int serverPort = request.getServerPort();//8082
        String contextPath = request.getContextPath();//项目名
        String url = scheme + "://" + serverName + ":" + serverPort + contextPath;//http://127.0.0.1:8080/test
        return url + uploadPath + "/" + fileName;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值