TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
绘制甘特图的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>
- 二、效果图:
- [img]http://img.javaxxz.com/2014/10/10/015321500.gif[/img]
- 三、servlet源码
- // Fig. 5.8: GanterServlet.java
- // 绘制甘特图的Servlet
- package com.fatcat.webchart;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.io.*;
- import java.util.*;
- import java.awt.image.*;
- import java.awt.*;
- import javax.imageio.*;
- public class GanterServlet extends HttpServlet
- {
- // 处理 HTTP get 请求
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException
- {
- // 清空缓冲区
- response.reset();
- // 注意这里的MIME类型
- response.setContentType("image/png");
- int totalProcess = 8;
- int[] startDay = new int[totalProcess];
- int[] processDay = new int[totalProcess];
- for (int i = 0; i < totalProcess; i++)
- {
- startDay[i] = 1+(int)(Math.random() * 9);
- processDay[i] = 1+(int)(Math.random() * (11-startDay[i]));
- }
- // 创建一个 550X440 的图像
- int width = 550, height = 440;
- BufferedImage image = new BufferedImage(width, height,
- BufferedImage.TYPE_INT_RGB);
- // 得到图形环境对象 g
- Graphics g = image.getGraphics();
- // 填充背景
- g.setColor(Color.YELLOW);
- g.fillRect(0, 0, width, height);
- g.setColor(Color.white);
- g.fillRect(11, 35, width - 21, height - 47);
- g.setColor(Color.black);
- g.drawRect(10, 35, width - 20, height - 46);
- g.setColor(Color.black);
- g.setFont(new Font("方正粗宋简体", Font.PLAIN, 25));
- g.drawString("绘制甘特图的Servlet", 45, 25);
- Color color[] = new Color[8];
- color[0] = new Color(99, 99, 0);
- color[1] = new Color(255, 169, 66);
- color[2] = new Color(33, 133, 66);
- color[3] = new Color(33, 0, 255);
- color[4] = new Color(255, 0, 66);
- color[5] = new Color(128, 0, 118);
- color[6] = new Color(126, 126, 66);
- color[7] = new Color(0, 126, 166);
- g.setFont(new Font("黑体", Font.BOLD, 15));
- g.drawString("计划名称", 25, 50);
- g.drawString("时间", 505, 420);
- g.setFont(new Font("SansSerif", Font.PLAIN, 12));
- for (int i = 0; i <= 400; i += 40)
- {
- g.setColor(Color.BLACK);
- g.drawString("2-" + (1+i / 40), (i + 70), 420);
- g.setColor(Color.LIGHT_GRAY);
- g.drawLine((i + 80), 65, (i + 80), 400);
- }
-
- // 绘制坐标轴
- g.setColor(Color.BLACK);
- g.fillRect(77, 55, 3, 345);
- g.fillRect(80, 397, 420, 3);
- int drawWidth = 0;
- for (int i = 0; i < totalProcess; i++)
- {
- g.setColor(Color.BLACK);
- g.drawString("计划" + (i + 1), 40, 95+i * 40);
- g.setColor(Color.LIGHT_GRAY);
- g.drawLine(80, 90+i * 40, 480, 90+i * 40);
- g.setColor(color[i]);
- drawWidth = processDay[i] * 40;
- g.fill3DRect(80+(startDay[i] - 1) * 40, 78+i * 40, drawWidth, 25, true);
- g.setColor(Color.WHITE);
- g.setFont(new Font("黑体", Font.BOLD, 15));
- g.drawString("" + processDay[i] + "天", 70+(startDay[i] - 1) * 40 +
- drawWidth / 2, 95+i * 40);
- g.setFont(new Font("SansSerif", Font.PLAIN, 12));
- }
- // 利用ImageIO类的write方法对图像进行编码
- ServletOutputStream sos = response.getOutputStream();
- ImageIO.write(image, "PNG", sos);
- sos.close();
- }
- // 处理 HTTP post 请求, 和doGet一样
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException
- {
- doGet(request, response);
- }
- }
- /**************************************************************************
- * (C) Copyright 2004-2005 by Jingkui Zhong(钟京馗) and Huan Tang(唐桓). *
- * All Rights Reserved. *
- * *
- * DISCLAIMER: The authors of this code have used their *
- * best efforts in preparing the code. These efforts include the *
- * development, research, and testing of the theories and programs *
- * to determine their effectiveness. The authors and publisher make *
- * no warranty of any kind, expressed or implied, with regard to these *
- * programs or to the documentation contained in these codes. The authors *
- * shall not be liable in any event for incidental or consequential *
- * damages in connection with, or arising out of, the furnishing, *
- * performance, or use of these programs. *
- **************************************************************************/
- 运行效果:
-
复制代码
源码下载:http://file.javaxxz.com/2014/10/10/015322265.zip |
|