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

📄 drawtriangle.jsp

📁 提供 精通Java Web动态图表编程 一书的源代码, 大家赶快下载呀
💻 JSP
字号:
<!--
	Fig. 6.04_07_01: drawTriangle.jsp
	功能:绘制三角形
-->
<%@ page language="java" contentType="image/png;charset=GB2312"
	import="java.awt.image.*"
	import="java.awt.*"
	import="javax.imageio.*"
%>

<%!
    int[] TriangleXValues = new int[3];
    int[] TriangleYValues = new int[3];
    int alpha = 60; //等腰三角形的底角
    int baseLine = 80; //等腰三角形的底边长
	int basePointX = 80; // 缺省的绘制横坐标
    int basePointY = 100; // 缺省的绘制纵坐标

    Color fillColor = Color.BLACK; // 缺省的填充颜色
    Color outlineColor = Color.BLACK; //缺省的轮廓颜色

    public void drawTrigangle(int x, int y, int triangleDirection, int
        triangleStryle, Graphics g)
    {
        // 计算偏移量
        int offset = (int)(baseLine / 2 * Math.tan(alpha * Math.PI / 180));

        // 计算三角形3个顶点的坐标
        switch (triangleDirection)
        {
            case 1:
                TriangleXValues[0] = x - baseLine / 2;
                TriangleYValues[0] = y;
                TriangleXValues[1] = x + baseLine / 2;
                TriangleYValues[1] = y;
                TriangleXValues[2] = x;
                TriangleYValues[2] = y - offset;
                break;

            case 2:
                TriangleXValues[0] = x;
                TriangleYValues[0] = y - baseLine / 2;
                TriangleXValues[1] = x;
                TriangleYValues[1] = y + baseLine / 2;
                TriangleXValues[2] = x + offset;
                TriangleYValues[2] = y;
                break;

            case 3:
                TriangleXValues[0] = x - baseLine / 2;
                TriangleYValues[0] = y;
                TriangleXValues[1] = x + baseLine / 2;
                TriangleYValues[1] = y;
                TriangleXValues[2] = x;
                TriangleYValues[2] = y + offset;
                break;

            case 4:
                TriangleXValues[0] = x;
                TriangleYValues[0] = y - baseLine / 2;
                TriangleXValues[1] = x;
                TriangleYValues[1] = y + baseLine / 2;
                TriangleXValues[2] = x - offset;
                TriangleYValues[2] = y;
                break;
        }

        // 根据三角形的坐标及风格进行绘制
        switch (triangleStryle)
        {
            case 1:
                g.setColor(outlineColor);
                g.drawPolygon(TriangleXValues, TriangleYValues, 3);
                break;

            case 2:
                g.setColor(fillColor);
                g.fillPolygon(TriangleXValues, TriangleYValues, 3);
                break;

            case 3:
                g.setColor(fillColor);
                g.fillPolygon(TriangleXValues, TriangleYValues, 3);
                g.setColor(outlineColor);
                g.drawPolygon(TriangleXValues, TriangleYValues, 3);
                break;
        }
    }

    public void setBaseLine(int baseLine)
    {
        this.baseLine = baseLine;
    }

    public void setFillColor(Color fillColor)
    {
        this.fillColor = fillColor;
    }

    public void setOutlineColor(Color outlineColor)
    {
        this.outlineColor = outlineColor;
    }

	public void setAlpha(int alpha)
	{
		this.alpha = alpha;
	}

	public void setBasePointX(int x)
	{
	    this.basePointX = x;
	}

	public void setBasePointY(int y)
	{
	    this.basePointY = y;
	}
%>

<%
		// 清空缓冲区
		response.reset();

		// 注意这里的MIME类型
		response.setContentType("image/png");

		// 创建一个 200X160 的图像
		int width = 200, height = 120;

		BufferedImage image = new BufferedImage(width, height,
		  BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();

		// 填充背景
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, width, height);

        setFillColor(Color.RED);

		// 获取客户端提交的等腰三角形的基准点参数
		String basePointXString = request.getParameter("x");
		String basePointYString = request.getParameter("y");
		if (basePointXString != null && basePointYString != null)
		{
		   setBasePointX(Integer.parseInt(basePointXString));
		   setBasePointY(Integer.parseInt(basePointYString));
		}

		// 获取客户端提交的等腰三角形的底角参数
		String alphaString = request.getParameter("alpha");
		if (alphaString != null)
		{
		   setAlpha(Integer.parseInt(alphaString));

		}

		// 获取客户端提交的等腰三角形的底边长度参数
		String baseLineString = request.getParameter("baseLine");
		if (baseLineString != null)
		{
		   this.baseLine = Integer.parseInt(baseLineString);
		}

		// 获取客户端提交的等腰三角形的方向参数
		String directionString = request.getParameter("direction");
		int direction = 1;
		if (directionString != null)
		{
		   direction = Integer.parseInt(directionString);
		}

    	// 获取客户端提交的等腰三角形的绘制风格参数
		String styleString = request.getParameter("style");
		int style = 1;
		if (directionString != null)
		{
		   style = Integer.parseInt(styleString);
		}

        // 绘制三角形	
        drawTrigangle(basePointX, basePointY, direction, style, g);

		// 部署图形
		g.dispose();

		// 利用ImageIO类的write方法对图像进行编码
		ServletOutputStream sos = response.getOutputStream();
		ImageIO.write(image, "PNG", sos);
		sos.close();
%>


<%
/**************************************************************************
 * (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.                                 *
 **************************************************************************/
%>

⌨️ 快捷键说明

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