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

📄 drawpolygon.jsp

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

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

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

		// 创建一个 640X480 的图像
		int width = 640, height = 480;

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

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

		g.setColor(Color.BLACK);
		
        // 设置字体
        g.setFont(new Font("汉真广标", Font.PLAIN, 35));
        g.drawString("多边形的画法", 10, 35);

        g.setColor(Color.BLUE);

        // 绘制一个五角星		
        int[] xValues = { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 };
        int[] yValues = { 70, 106, 106, 124, 166, 142, 166, 124, 106, 106 };
        Polygon fiveStar = new Polygon(xValues, yValues, xValues.length);
        g.drawPolygon(fiveStar);

        // 空心多边形的画法2,创建一个新的不包含任何顶点的Ploygon实例
        Polygon sharp = new Polygon();
        sharp.addPoint(185, 45);
        sharp.addPoint(195, 110);
        sharp.addPoint(290, 160);
        sharp.addPoint(220, 180);
        sharp.addPoint(150, 140);

        g.setColor(Color.RED);
        g.drawPolygon(sharp);

		g.setColor(Color.BLUE);

        // 填充一个五角星		
        int[] xValues2 = { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 };
        int[] yValues2 = { 220, 256, 256, 274, 316, 292, 316, 274, 256, 256 };
        Polygon fiveStar2 = new Polygon(xValues2, yValues2, xValues2.length);
        g.fillPolygon(fiveStar2);

        // 填充一个多边形
        Polygon sharp2 = new Polygon();
        sharp2.addPoint(185, 195);
        sharp2.addPoint(195, 260);
        sharp2.addPoint(290, 310);
        sharp2.addPoint(220, 330);
        sharp2.addPoint(150, 290);
        g.setColor(Color.RED);
        g.fillPolygon(sharp2);

		// 部署图形
		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 + -