用java画工作流流程图,java生成流程图

Posted killer-leon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用java画工作流流程图,java生成流程图相关的知识,希望对你有一定的参考价值。

          阿里云低价服务器1折特惠,优惠爽翻天,点我立即低价购买

/*
 * Copyright 2015 iminasha.com All right reserved. This software is the confidential and proprietary information of
 * iminasha.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with iminasha.com.
 */
package com.imiansha.wms.controller.workflow;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;

import com.jfinal.kit.PathKit;
import com.jfinal.render.Render;

/**
 * 类DrawFlowChart.java的实现描述:根据审批人画流程图
 * 
 * @author leon 2015年7月14日 上午9:37:56
 */
public class DrawFlowChart extends Render 

    private int                width;
    private final int          intervalOfX = 100;// 每个元素之间的x轴间距
    private List<List<String>> approvalers;//审批人组集合

    /**
     * @param width
     * @param approvalers
     */
    public DrawFlowChart(int width, List<List<String>> approvalers)
        super();
        this.width = width;
        this.approvalers = approvalers;
    

    /**
     * @return the width
     */
    public int getWidth() 
        return width;
    

    /**
     * @param width the width to set
     */
    public void setWidth(int width) 
        this.width = width;
    

    /**
     * @return the approvalers
     */
    public List<List<String>> getApprovalers() 
        return approvalers;
    

    /**
     * @param approvalers the approvalers to set
     */
    public void setApprovalers(List<List<String>> approvalers) 
        this.approvalers = approvalers;
    

    @Override
    public void render() 
        BufferedImage image = drawFlowChartImg();
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        ServletOutputStream sos = null;
        try 
            sos = response.getOutputStream();
            ImageIO.write(image, "jpeg", sos);
         catch (Exception e) 
            throw new RuntimeException(e);
         finally 
            if (sos != null) try 
                sos.close();
             catch (IOException e) 
                e.printStackTrace();
            
        
    

    /**
     * 画流程图
     * 
     * @return
     */
    private BufferedImage drawFlowChartImg() 
        // 获取箭头图标文件
        File srcFile = new File(PathKit.getRootClassPath() + File.separator + "arrow.png");
        Image srcImg = null;
        try 
            srcImg = ImageIO.read(srcFile);
         catch (IOException e) 
            e.printStackTrace();
        
        // 计算画布的高度
        // 画布高度=每组审批人的总高度+箭头高度
        // 获取最多审批人的组
        // 每组中的每个审批人y坐标间隔20
        int every_approvaler_distance = 20;
        int maxApprovalCount = this.getMaxListSize(approvalers);
        // 计算画布的最高高度
        int height = maxApprovalCount * every_approvaler_distance + srcImg.getHeight(null);

        // 绘制宽=width,长=hight的图板
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 获取图形上下文,graphics想象成一个画笔
        Graphics2D graphics = (Graphics2D) image.getGraphics();

        // 对指定的矩形区域填充颜色
        graphics.setColor(Color.WHITE); // GREEN:绿色; 红色:RED; 灰色:GRAY
        graphics.fillRect(0, 0, width, height);
        // 箭头数量
        int arrowCount = 1;
        // 设置文字颜色
        graphics.setColor(Color.BLACK);
        graphics.setFont(new Font("宋体", Font.BOLD, 14));
        int lastGroupElementX = 0;// 最后一组元素的X坐标
        int arrowY = height / 2 - srcImg.getHeight(null);// 箭头的y坐标
        if (approvalers != null && approvalers.size() > 0) 
            for (List<String> approvalerList : approvalers) 
                int i = 1;
                int maxWidthOfGroup = 0;// 每组最大宽度
                int compareTmp = 0;// 比较宽度的变量
                lastGroupElementX += intervalOfX;// 最后一组的x坐标
                int approvalerListSize = approvalerList.size();// 每组审批人数量
                // 计算每个组的第一个元素的y坐标
                int everyGruopFirstElementY = 0;
                if (approvalerListSize % 2 == 0) 
                    everyGruopFirstElementY = maxApprovalCount * every_approvaler_distance / 2
                                              - every_approvaler_distance
                                              - (approvalerListSize / 2 - 1) * every_approvaler_distance;
                 else 
                    everyGruopFirstElementY = maxApprovalCount * every_approvaler_distance / 2
                                              - (approvalerListSize / 2 + 1) * every_approvaler_distance;
                

                // 将审批人画在图片上
                for (String elementStr : approvalerList) 
                    int strWidth = graphics.getFontMetrics().stringWidth(elementStr);
                    if (approvalerListSize < maxApprovalCount) 
                        graphics.drawString(elementStr, lastGroupElementX,
                                            everyGruopFirstElementY + i * every_approvaler_distance);
                     else 
                        graphics.drawString(elementStr, lastGroupElementX, i * every_approvaler_distance);

                    
                    // 计算最大的审批人字符串宽度
                    maxWidthOfGroup = strWidth;
                    if (maxWidthOfGroup < compareTmp) 
                        maxWidthOfGroup = compareTmp;
                     else 
                        compareTmp = strWidth;
                    
                    i++;
                
                lastGroupElementX += maxWidthOfGroup;// 重新计算最后一个元素的y坐标
                // 将箭头画在图片上
                if (arrowCount < approvalers.size()) 
                    graphics.drawImage(srcImg, lastGroupElementX + srcImg.getWidth(null), arrowY, null);
                    lastGroupElementX += srcImg.getWidth(null);
                
                arrowCount++;
            

        

        graphics.dispose();// 释放此图形的上下文并释放它所使用的所有系统资源
        return image;

    

    /**
     * 获取list集合中最大的list size
     * 
     * @param list
     * @return
     */
    private int getMaxListSize(List<List<String>> list) 

        if (list == null || list.size() == 0) 
            return 0;
        
        int maxSize = list.get(0).size();
        int compareTmp = maxSize;

        for (List<String> listTmp : list) 

            maxSize = listTmp.size();
            if (maxSize < compareTmp) 
                maxSize = compareTmp;
             else 
                compareTmp = listTmp.size();
            
        

        return maxSize;
    


以上是关于用java画工作流流程图,java生成流程图的主要内容,如果未能解决你的问题,请参考以下文章

按要求设计递归算法。只需写出伪代码或画流程图,不需语言实现,但算法必须完整清晰。

java Graphics2D根据流程xml文件画流程图(完整代码)

如何用visio2016画流程图

用Visio画流程图

processon怎么画流程图

思维导图用啥软件做?