📄 triangleservlet.java
字号:
// Fig. 5.02_02: TriangleServlet.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 com.sun.image.codec.jpeg.*;
public class TriangleServlet extends HttpServlet
{
int[]TriangleXValues = new int[3];
int[]TriangleYValues = new int[3];
int alpha = 0; //等腰三角形的底角
int baseLine = 0; //等腰三角形的底边长
// 三角形的方向
final int UP = 1, RIGHT = 2, DOWN = 3, LEFT = 4;
// 三角形的绘制风格
final int OUTLINE = 1, FILL = 2, MIXED = 3;
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 setAlpha(int alpha)
{
this.alpha = alpha;
}
public void setFillColor(Color fillColor)
{
this.fillColor = fillColor;
}
public void setOutlineColor(Color outlineColor)
{
this.outlineColor = outlineColor;
}
// 处理 HTTP get 请求
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// 清空缓冲区
response.reset();
// 注意这里的MIME类型
response.setContentType("image/jpeg");
// 创建一个 400X300 的图像
int width = 400, height = 300;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 得到图形环境对象 g
Graphics g = image.getGraphics();
Color oColor[] = new Color[5];
oColor[0] = new Color(99, 99, 0);
oColor[1] = new Color(255, 169, 66);
oColor[2] = new Color(33, 255, 66);
oColor[3] = new Color(33, 0, 255);
oColor[4] = new Color(255, 0, 66);
Color fColor[] = new Color[5];
fColor[0] = Color.RED;
fColor[1] = Color.BLUE;
fColor[2] = Color.GREEN;
fColor[3] = Color.ORANGE;
fColor[4] = Color.CYAN;
// 填充背景
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
g.setColor(Color.BLACK);
g.setFont(new Font("方正粗宋简体", Font.PLAIN, 30));
g.drawString("随机生成三角形-Servlet版", 10, 35);
int tempBaseLine = 100+(int)(Math.random() * 50);
setBaseLine(tempBaseLine);
int tempAlpha = 30+(int)(Math.random() * 30);
setAlpha(tempAlpha);
int tempOutlineColorIndex = (int)(Math.random() * 5);
setOutlineColor(oColor[tempOutlineColorIndex]);
int tempfillColorIndex = (int)(Math.random() * 5);
setFillColor(fColor[tempfillColorIndex]);
int tempDirection = 1+(int)(Math.random() * 4);
int tempStyle = 1+(int)(Math.random() * 3);
// 绘制三角形
drawTrigangle(180, 150, tempDirection, tempStyle, g);
ServletOutputStream out = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.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. *
**************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -