java.awt.Graphics2D绘制流程图基本元素

转自: http://blog.csdn.net/hu_shengyang/article/details/7190031
   ----java.awt.Graphics
----java.awt.Graphics2D

 

由于项目需要,要求对用户流程进行图形化展示:用户对自己的操作通过查看流程图一目了然。于是进行了一下前期的java绘图探索,通过java.awt.Graphics2D对绘制流程图的基本元素进行了编码,并将其展示在了jsp页面上。

首先编写一个servlet,然后在web.xml中对其进行配置,最后将图片展示在jsp页面。

1. 绘图方法:

 

[java]  view plain copy
 
  1. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  2.     response.setContentType("image/jpeg");//声明文件格式  
  3.     //绘制宽=480,长=640的图板  
  4.     int width=480,hight=720;  
  5.     BufferedImage image = new BufferedImage(width,hight,BufferedImage.TYPE_INT_RGB);  
  6.     //获取图形上下文,graphics想象成一个画笔  
  7.     Graphics2D graphics = (Graphics2D)image.getGraphics();  
  8.       
  9.     //消除线条锯齿  
  10.     graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
  11.       
  12.     //对指定的矩形区域填充颜色  
  13.     graphics.setColor(Color.ORANGE);    //GREEN:绿色;  红色:RED;   灰色:GRAY  
  14.     graphics.fillRect(00240720);  
  15.     //对指定的矩形区域填充颜色  
  16.     graphics.setColor(Color.PINK);    
  17.     graphics.fillRect(2400240720);  
  18.       
  19.     //生成随机数  
  20.     Random random = new Random();  
  21.     /* 
  22.      * 画线 x,y是坐标,定义线段的两个坐标点 
  23.      */  
  24.     graphics.setColor(Color.BLACK);  
  25.     int x=100,y=100,x1=100,y1=y;  
  26.     graphics.drawLine(x,y,x+x1,y1);  
  27.     /* 
  28.      *画出一个折线 
  29.      */  
  30.     int[] xPoints = {100,100,250,250};  
  31.     int[] yPoints = {180,150,150,180};  
  32.     graphics.drawPolyline(xPoints, yPoints, 4);  
  33.     /* 
  34.      * 画出一个闭合多边形(三角形) 
  35.      */  
  36.     int[] xPoints1 = {100,100,200};  
  37.     int[] yPoints1 = {240,320,280};  
  38.     graphics.drawPolygon(xPoints1, yPoints1, 3);  
  39.     /* 
  40.      * 画出一个闭合多边形(菱形) 
  41.      */  
  42.     int[] xPoints2 = {240,300,360,300};  
  43.     int[] yPoints2 = {280,240,280,320};  
  44.     graphics.drawPolygon(xPoints2, yPoints2, 4);  
  45.     graphics.setColor(Color.ORANGE);  
  46.     graphics.fillPolygon(xPoints2, yPoints2, 4);  
  47.     /* 
  48.      *绘制一个椭圆形  
  49.      */  
  50.     graphics.setColor(Color.GREEN);  
  51.     int xOval=100,yOval=360;  
  52.     graphics.drawOval(xOval, yOval, 100100);  
  53.   
  54.     /* 
  55.      *绘制一个矩形 
  56.      */  
  57.     //graphics.setColor(Color.GRAY);//--设置矩形边框颜色 。GREEN:绿色;  红色:RED;   灰色:GRAY  
  58.     int xRect=240,yRect=360;  
  59.     graphics.drawRect(xRect, yRect, 200100);  
  60.   
  61.     //设置文字颜色  
  62.     graphics.setColor(new Color( 20+random.nextInt(100),  20+random.nextInt(100),  20+random.nextInt(100) ));  
  63.     //设置文字内容、位置  
  64.     graphics.drawString("直线",100+50,100-5);  
  65.     graphics.drawString("折线"200150-5);  
  66.     graphics.drawString("空心三角形"110280);  
  67.     graphics.drawString("实心菱形"300-20280);  
  68.     graphics.drawString("椭圆形"100+50360+50);  
  69.     graphics.drawString("矩形"240+50360+50);  
  70.     //graphics.drawString("错误的背景颜色", 100, 540);  
  71.       
  72.     //graphics.setPaintMode();  
  73.     //graphics.translate(400, 600);  
  74.       
  75.     graphics.dispose();//释放此图形的上下文并释放它所使用的所有系统资源  
  76.       
  77.     ImageIO.write(image,"JPEG",response.getOutputStream());  
  78.     PrintWriter out = response.getWriter();  
  79.     out.flush();  
  80.     out.close();  
  81.     //super.doGet(request, response);  
  82. }  

 

2. web.xml中配置servlet:

 

[html]  view plain copy
 
  1. <servlet>  
  2.     <servlet-name>graphics1</servlet-name>  
  3.     <servlet-class>hsy.graphics.test.GraphicsServlet</servlet-class>  
  4. </servlet>  
  5.   
  6. <servlet-mapping>  
  7.     <servlet-name>graphics1</servlet-name>  
  8.     <url-pattern>/servlet/graphics1</url-pattern>  
  9. </servlet-mapping>  

 

3. Jsp页面中只需如下一句即可展示图形:

 

[html]  view plain copy
 
  1. <span style="white-space:pre">  </span><img id="" style="" src="../servlet/graphics1">  

 

4.页面图形展示如下:

 

现在大家可以看看,充分发挥一下想象力,上图中是否包含了 ‘泳道’、‘开始节点’、‘结束节点’、’普通节点‘、‘流转线条’ 、‘描述文字’ 等一系列元素。那么剩下来的事情就是如何去绘制一个完整的流程图了,显然这不是一件简单的事情,但是有了这些基本的元素,再难应该也是可以绘制出来的。童鞋们,结合自己的项目,充分发挥你的想象力去完成流程图的绘制吧!

转载于:https://www.cnblogs.com/woniuzhongdetou/p/3635445.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值