Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 485|回复: 0

[servlet学习]servlet绘制甘特图

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-10 01:53:22 | 显示全部楼层 |阅读模式
    绘制甘特图的Servlet
    一、web.xml文件:

        <?xml version="1.0" encoding="GB2312"?>
        <!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
    "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
        <web-app>

    <servlet>
    <servlet-name>GanterServlet</servlet-name>
    <description>绘制甘特图的Servlet</description>
    <servlet-class>com.fatcat.webchart.GanterServlet</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>GanterServlet</servlet-name>
    <url-pattern>/ganter</url-pattern>
    </servlet-mapping>
        </web-app>
       


       
      
       
       
       

       
      
      
      
       
      
    1. 二、效果图:
    2. [img]http://img.javaxxz.com/2014/10/10/015321500.gif[/img]
    3. 三、servlet源码
    4. // Fig. 5.8:  GanterServlet.java
    5. // 绘制甘特图的Servlet
    6. package com.fatcat.webchart;
    7. import javax.servlet.*;
    8. import javax.servlet.http.*;
    9. import java.io.*;
    10. import java.util.*;
    11. import java.awt.image.*;
    12. import java.awt.*;
    13. import javax.imageio.*;
    14. public class GanterServlet extends HttpServlet
    15. {
    16.     // 处理 HTTP get 请求
    17.     public void doGet(HttpServletRequest request, HttpServletResponse response)
    18.         throws ServletException, IOException
    19.     {
    20.         // 清空缓冲区
    21.         response.reset();
    22.         // 注意这里的MIME类型
    23.         response.setContentType("image/png");
    24.         int totalProcess = 8;
    25.         int[] startDay = new int[totalProcess];
    26.         int[] processDay = new int[totalProcess];
    27.         for (int i = 0; i < totalProcess; i++)
    28.         {
    29.             startDay[i] = 1+(int)(Math.random() * 9);
    30.             processDay[i] = 1+(int)(Math.random() * (11-startDay[i]));
    31.         }
    32.         // 创建一个 550X440 的图像
    33.         int width = 550, height = 440;
    34.         BufferedImage image = new BufferedImage(width, height,
    35.             BufferedImage.TYPE_INT_RGB);
    36.         // 得到图形环境对象 g
    37.         Graphics g = image.getGraphics();
    38.         // 填充背景
    39.         g.setColor(Color.YELLOW);
    40.         g.fillRect(0, 0, width, height);
    41.         g.setColor(Color.white);
    42.         g.fillRect(11, 35, width - 21, height - 47);
    43.         g.setColor(Color.black);
    44.         g.drawRect(10, 35, width - 20, height - 46);
    45.         g.setColor(Color.black);
    46.         g.setFont(new Font("方正粗宋简体", Font.PLAIN, 25));
    47.         g.drawString("绘制甘特图的Servlet", 45, 25);
    48.         Color color[] = new Color[8];
    49.         color[0] = new Color(99, 99, 0);
    50.         color[1] = new Color(255, 169, 66);
    51.         color[2] = new Color(33, 133, 66);
    52.         color[3] = new Color(33, 0, 255);
    53.         color[4] = new Color(255, 0, 66);
    54.         color[5] = new Color(128, 0, 118);
    55.         color[6] = new Color(126, 126, 66);
    56.         color[7] = new Color(0, 126, 166);
    57.         g.setFont(new Font("黑体", Font.BOLD, 15));
    58.         g.drawString("计划名称", 25, 50);
    59.         g.drawString("时间", 505, 420);
    60.         g.setFont(new Font("SansSerif", Font.PLAIN, 12));
    61.         for (int i = 0; i <= 400; i += 40)
    62.         {
    63.             g.setColor(Color.BLACK);
    64.             g.drawString("2-" + (1+i / 40), (i + 70), 420);
    65.             g.setColor(Color.LIGHT_GRAY);
    66.             g.drawLine((i + 80), 65, (i + 80), 400);
    67.         }
    68.       
    69.         // 绘制坐标轴
    70.         g.setColor(Color.BLACK);
    71.         g.fillRect(77, 55, 3, 345);
    72.         g.fillRect(80, 397, 420, 3);
    73.         int drawWidth = 0;
    74.         for (int i = 0; i < totalProcess; i++)
    75.         {
    76.             g.setColor(Color.BLACK);
    77.             g.drawString("计划" + (i + 1), 40, 95+i * 40);
    78.             g.setColor(Color.LIGHT_GRAY);
    79.             g.drawLine(80, 90+i * 40, 480, 90+i * 40);
    80.             g.setColor(color[i]);
    81.             drawWidth = processDay[i] * 40;
    82.             g.fill3DRect(80+(startDay[i] - 1) * 40, 78+i * 40, drawWidth, 25, true);
    83.             g.setColor(Color.WHITE);
    84.             g.setFont(new Font("黑体", Font.BOLD, 15));
    85.             g.drawString("" + processDay[i] + "天", 70+(startDay[i] - 1) * 40 +
    86.                 drawWidth / 2, 95+i * 40);
    87.             g.setFont(new Font("SansSerif", Font.PLAIN, 12));
    88.         }
    89.         // 利用ImageIO类的write方法对图像进行编码
    90.         ServletOutputStream sos = response.getOutputStream();
    91.         ImageIO.write(image, "PNG", sos);
    92.         sos.close();
    93.     }
    94.     // 处理 HTTP post 请求, 和doGet一样
    95.     public void doPost(HttpServletRequest request, HttpServletResponse response)
    96.         throws ServletException, IOException
    97.     {
    98.         doGet(request, response);
    99.     }
    100. }
    101. /**************************************************************************
    102. * (C) Copyright 2004-2005 by Jingkui Zhong(钟京馗) and Huan Tang(唐桓).  *
    103. * All Rights Reserved.                                                   *
    104. *                                                                        *
    105. * DISCLAIMER: The authors of this code have used their                   *
    106. * best efforts in preparing the code. These efforts include the          *
    107. * development, research, and testing of the theories and programs        *
    108. * to determine their effectiveness. The authors and publisher make       *
    109. * no warranty of any kind, expressed or implied, with regard to these    *
    110. * programs or to the documentation contained in these codes. The authors *
    111. * shall not be liable in any event for incidental or consequential       *
    112. * damages in connection with, or arising out of, the furnishing,         *
    113. * performance, or use of these programs.                                 *
    114.   **************************************************************************/
    115. 运行效果:
    116.                   
    复制代码


      
      
       
       

         
       

       
       
      

      

      










    源码下载:http://file.javaxxz.com/2014/10/10/015322265.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 18:16 , Processed in 0.325090 second(s), 36 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表