⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chartservlet.java

📁 一个java写的加密算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * $Id: ChartServlet.java,v 1.4 2004/11/14 07:33:14 tcfujii Exp $ *//* * Copyright 2004-2005 Sun Microsystems, Inc.  All rights reserved. * Use is subject to license terms. */package components.renderkit;import components.model.ChartItem;import com.sun.image.codec.jpeg.*;import java.awt.Color;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Rectangle;import java.awt.image.BufferedImage;import java.awt.geom.Ellipse2D;import java.awt.RenderingHints;import java.io.IOException;import java.io.OutputStream;import java.util.List;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;/** * <p><strong>ChartServlet</strong> is used to render the chart image. */public final class ChartServlet extends HttpServlet {    /**     * <p>The <code>ServletConfig</code> instance for this servlet.</p>     */    private ServletConfig servletConfig = null;        /**     * <p>Release all resources acquired at startup time.</p>     */    public void destroy() {        servletConfig = null;    }    /**     * <p>Return the <code>ServletConfig</code> instance for this servlet.</p>     */    public ServletConfig getServletConfig() {        return (this.servletConfig);    }    /**     * <p>Return information about this Servlet.</p>     */    public String getServletInfo() {        return (this.getClass().getName());    }    /**     * <p>Perform initialization.</p>     *     * @exception ServletException if, for any reason,      * bn error occurred during the processing of     * this <code>init()</code> method.     */    public void init(ServletConfig servletConfig) throws ServletException {	        // Save our ServletConfig instance        this.servletConfig = servletConfig;    }    /**     * <p>Process an incoming request, and create the corresponding     * response.</p>     *     * @param request The servlet request we are processing     * @param response The servlet response we are creating     *     * @exception IOException if an input/output error occurs during processing     * @exception ServletException if a servlet error occurs during processing     */    public void doGet(HttpServletRequest request, HttpServletResponse response)        throws IOException, ServletException {	// Here's where we'd get the ChartBean from the session and determine	// whether we're generating a pie chart or bar chart...	//	String type = request.getParameter("type");	if ((type == null) || 	    (!type.equals("bar")) && (!type.equals("pie"))) {	    type = "bar";	}        	if (type.equals("bar")) {	    generateBarChart(request, response);	} else {            generatePieChart(request, response);	}    }    /**     * <p>Process an incoming request, and create the corresponding     * response.</p>     *     * @param request The servlet request we are processing     * @param response The servlet response we are creating     *     * @exception IOException if an input/output error occurs during processing     * @exception ServletException if a servlet error occurs during processing     */    public void doPost(HttpServletRequest request, HttpServletResponse response)        throws IOException, ServletException {	doGet(request, response);    }    /**     * <p> Generate a bar chart from data.     *     * @param request The servlet request we are processing     * @param response The servlet response we are creating     *     * @exception IOException if an input/output error occurs during processing     * @exception ServletException if a servlet error occurs during processing     */    private void generateBarChart(HttpServletRequest request,		                  HttpServletResponse response)        throws IOException, ServletException {        final int VERTICAL = 0;	final int HORIZONTAL = 1;	response.setContentType("image/jpeg");	        String id = request.getParameter("chartId");        	// get chart parameters	String title = request.getParameter("title");	if (title == null) {	    title = "Chart";	}		int orientation = VERTICAL;	String orientationStr = request.getParameter("orientation");	if ((orientationStr == null) || 	    (!orientationStr.equals("horizontal")) && (!orientationStr.equals("vertical"))) {	    orientation = VERTICAL;	} else if (orientationStr.equals("vertical")) {	    orientation = VERTICAL;	} else {	    orientation = HORIZONTAL;	}	// label for x/y axis	String xLabel = request.getParameter("xlabel");	String yLabel = request.getParameter("ylabel");	// default image size	int width = 400;	int height = 300;	String widthStr = request.getParameter("width");	String heightStr = request.getParameter("height");	if (widthStr != null) {	    width = Integer.parseInt(widthStr);	}	if (heightStr != null) {	    height = Integer.parseInt(heightStr);	}		// get an array of chart items containing our data..        HttpSession session = request.getSession(true);	ChartItem[] chartItems = (ChartItem[])session.getAttribute(id);	if (chartItems == null) {            System.out.println("No data items specified...");	    throw new ServletException("No data items specified...");	}        // remove the chart data from session now that chart has been rendered.        session.removeAttribute(id);        	// maximum data value	int maxDataValue = 0;	// maximum label width	int maxLabelWidth = 0;	// space between bars	int barSpacing = 10;	// width of each bar	int barWidth = 0;	// x,y coordinates	int cx, cy;	// number of chart items	int columns = chartItems.length;	int scale = 10;        // an individual chart data item	ChartItem chartItem = null;	String label = null;	int value = 0;        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);        Graphics2D g2d = bi.createGraphics();        Font titleFont = new java.awt.Font("Courier", Font.BOLD, 12);        FontMetrics titleFontMetrics = g2d.getFontMetrics(titleFont);		// loop through and figure out the the widest item label, as well as	// the maximum value.        for (int i=0; i < columns; i++) {	    chartItem = chartItems[i];	    label = chartItem.getLabel();	    value = chartItem.getValue();	    if (value > maxDataValue) {	        maxDataValue = value;	    }	    maxLabelWidth = Math.max(titleFontMetrics.stringWidth(label), maxLabelWidth);	}	// calculate chart dimensions	int[] xcoords = new int[columns];	int[] ycoords = new int[columns];	int totalWidth = 0;	int totalHeight = 0;	for (int i=0; i < columns; i++) {	    switch (orientation) {	      case VERTICAL:	      default:                 barWidth = maxLabelWidth;		cx = (Math.max((barWidth + barSpacing),maxLabelWidth) * i) +		    barSpacing;		totalWidth = cx + (4 * titleFont.getSize());		break;	      case HORIZONTAL:		barWidth = titleFont.getSize();		cy = ((barWidth + barSpacing) * i) + barSpacing;		totalHeight = cy + (4 * titleFont.getSize()); 		break;	    }	}	if (orientation == VERTICAL) {            totalHeight = maxDataValue + (8 * titleFont.getSize());	    totalWidth = totalWidth + 50;	} else {	    totalWidth = maxDataValue + (4 * titleFont.getSize() +		(Integer.toString(maxDataValue).length() * titleFont.getSize())+50);	}	// Make sure the the total height of the chart provides enough room	// for the vertical label..	//	int yLabelHeight = 0;	for (int i=0; i<yLabel.length(); i++) {	    yLabelHeight += titleFontMetrics.getAscent();	}	if ((yLabelHeight+(12 * titleFontMetrics.getDescent())) > totalHeight) {	    totalHeight = yLabelHeight+(8*titleFont.getSize());	}	            bi = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB);        g2d = bi.createGraphics();        titleFontMetrics = g2d.getFontMetrics(titleFont);	// graph dimensions	Dimension graphDim = new Dimension(totalWidth,totalHeight);        Rectangle graphRect = new Rectangle(graphDim);	// border dimensions	Dimension borderDim = new Dimension(totalWidth-2,totalHeight-2);        Rectangle borderRect = new Rectangle(borderDim);		// background color	g2d.setColor(Color.white);	g2d.fill(graphRect);	// draw border	g2d.setColor(Color.black);	borderRect.setLocation(1,1);        g2d.draw(borderRect);	// draw the title centered at the bottom of the bar graph	int i = titleFontMetrics.stringWidth(title);	g2d.setFont(titleFont);	g2d.setColor(Color.black);	g2d.drawString(title, Math.max((totalWidth - i)/2, 0),	    totalHeight - titleFontMetrics.getDescent());	// draw the x axis label	i = titleFontMetrics.stringWidth(xLabel);	g2d.drawString(xLabel, Math.max((totalWidth - i)/2, 0),	    totalHeight - (6 * titleFontMetrics.getDescent()));	// draw the y axis label	i = titleFontMetrics.stringWidth(yLabel);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -