Chinaunix首页 | 论坛 | 博客
  • 博客访问: 16493448
  • 博文数量: 5645
  • 博客积分: 9880
  • 博客等级: 中将
  • 技术积分: 68081
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-28 13:35
文章分类

全部博文(5645)

文章存档

2008年(5645)

我的朋友

分类:

2008-04-28 20:36:23

下载本文示例代码
一、前言    本文原作者为Tony Wang ,该文章涉及到文件的读写和jpg图片的自动生成。利用jsp servlet的技术,jsp调用servlet生成图片。 二:写文件write.jsp 三、servlet原代码: VoteImage.java : /* Author: Tony Wang E-mail: lucky_tony@163.net Date: 2001-01-01 如果对程序有什么疑问,可以和我联系,另外程序如果有什么bug,麻烦指出!! */ import java.io.*; import java.util.*; import com.sun.image.codec.jpeg.*; import javax.servlet.*; import javax.servlet.http.*; import java.awt.*; import java.awt.geom.*; import java.awt.image.*; public class VoteImage extends HttpServlet { private String strFile = null; private Color color[]={Color.red,Color.black,Color.orange,Color.green}; private int baseAng = 30; public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { strFile = request.getRealPath(“count.txt“); float[][] xy = new float[4][2]; xy = getNumAndPercent(strFile); int[] ang = new int[4]; ang[0] = (int)(xy[0][1]*360); ang[1] = (int)(xy[1][1]*360); ang[2] = (int)(xy[2][1]*360); ang[3] = 360-ang[0]-ang[1]-ang[2]; response.setHeader(“Cache-Control“,“no-store“); response.setDateHeader(“Expires“,0); response.setContentType(“image/jpeg“); ServletOutputStream out=response.getOutputStream(); BufferedImage image=new BufferedImage(150,100,BufferedImage.TYPE_INT_RGB); Graphics2D g=(Graphics2D)image.getGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(Color.white); g.fillRect(0,0,150,100); AffineTransform at = null; Arc2D arc = null; int fromAng = baseAng; at = AffineTransform.getRotateInstance((-20*java.lang.Math.PI)/180,45,37); g.setTransform(at); int r =6; int dx = (int)(r*java.lang.Math.cos((baseAng ang[0])/2.0*java.lang.Math.PI/180)); int dy = (int)(r*java.lang.Math.sin((baseAng ang[0])/2.0*java.lang.Math.PI/180)); arc = new Arc2D.Double(10 dx,24-dy,80,50,fromAng,ang[0],Arc2D.PIE); g.setColor(color[0]); g.fill(arc); fromAng =ang[0]; for (int i=1;i<4;i ) { g.setColor(color[i]); arc = new Arc2D.Double(10,24,80,50,fromAng,ang[i],Arc2D.PIE); g.fill(arc); fromAng =ang[i]; if (fromAng>360) { fromAng-=360; } } at = AffineTransform.getRotateInstance(0,arc.getCenterX(),arc.getCenterY()); g.setTransform(at); for (int i=0;i<4;i ){ g.setColor(color[i]); g.fillRect(100,15*i 20,10,10); g.drawString((char)(‘A‘ i) ““,120,15*i 20 8); } JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { doGet(request,response); } public synchronized float[][] getNumAndPercent(String sFileName) { float xx[][] = new float[4][2]; int totalNum = 0 ; String strTemp = null; int i = 0; java.io.RandomAccessFile rf = null; try { rf = new java.io.RandomAccessFile (sFileName,“r“); } catch(Exception e) { System.out.println(e); System.exit(0); } for (i=0;i<4;i ) { int m=0; try { strTemp = rf.readLine(); } catch (Exception e){ strTemp = “0“; } if (strTemp == null) strTemp = “0“; m = new Integer(strTemp).intValue(); xx[i][0]=m; totalNum = m; } if (totalNum==0) totalNum=1; for ( i=0;i<4;i ){ xx[i][1] = 共2页。 1 2 : 一、前言    本文原作者为Tony Wang ,该文章涉及到文件的读写和jpg图片的自动生成。利用jsp servlet的技术,jsp调用servlet生成图片。 二:写文件write.jsp 三、servlet原代码: VoteImage.java : /* Author: Tony Wang E-mail: lucky_tony@163.net Date: 2001-01-01 如果对程序有什么疑问,可以和我联系,另外程序如果有什么bug,麻烦指出!! */ import java.io.*; import java.util.*; import com.sun.image.codec.jpeg.*; import javax.servlet.*; import javax.servlet.http.*; import java.awt.*; import java.awt.geom.*; import java.awt.image.*; public class VoteImage extends HttpServlet { private String strFile = null; private Color color[]={Color.red,Color.black,Color.orange,Color.green}; private int baseAng = 30; public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { strFile = request.getRealPath(“count.txt“); float[][] xy = new float[4][2]; xy = getNumAndPercent(strFile); int[] ang = new int[4]; ang[0] = (int)(xy[0][1]*360); ang[1] = (int)(xy[1][1]*360); ang[2] = (int)(xy[2][1]*360); ang[3] = 360-ang[0]-ang[1]-ang[2]; response.setHeader(“Cache-Control“,“no-store“); response.setDateHeader(“Expires“,0); response.setContentType(“image/jpeg“); ServletOutputStream out=response.getOutputStream(); BufferedImage image=new BufferedImage(150,100,BufferedImage.TYPE_INT_RGB); Graphics2D g=(Graphics2D)image.getGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(Color.white); g.fillRect(0,0,150,100); AffineTransform at = null; Arc2D arc = null; int fromAng = baseAng; at = AffineTransform.getRotateInstance((-20*java.lang.Math.PI)/180,45,37); g.setTransform(at); int r =6; int dx = (int)(r*java.lang.Math.cos((baseAng ang[0])/2.0*java.lang.Math.PI/180)); int dy = (int)(r*java.lang.Math.sin((baseAng ang[0])/2.0*java.lang.Math.PI/180)); arc = new Arc2D.Double(10 dx,24-dy,80,50,fromAng,ang[0],Arc2D.PIE); g.setColor(color[0]); g.fill(arc); fromAng =ang[0]; for (int i=1;i<4;i ) { g.setColor(color[i]); arc = new Arc2D.Double(10,24,80,50,fromAng,ang[i],Arc2D.PIE); g.fill(arc); fromAng =ang[i]; if (fromAng>360) { fromAng-=360; } } at = AffineTransform.getRotateInstance(0,arc.getCenterX(),arc.getCenterY()); g.setTransform(at); for (int i=0;i<4;i ){ g.setColor(color[i]); g.fillRect(100,15*i 20,10,10); g.drawString((char)(‘A‘ i) ““,120,15*i 20 8); } JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.close(); } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { doGet(request,response); } public synchronized float[][] getNumAndPercent(String sFileName) { float xx[][] = new float[4][2]; int totalNum = 0 ; String strTemp = null; int i = 0; java.io.RandomAccessFile rf = null; try { rf = new java.io.RandomAccessFile (sFileName,“r“); } catch(Exception e) { System.out.println(e); System.exit(0); } for (i=0;i<4;i ) { int m=0; try { strTemp = rf.readLine(); } catch (Exception e){ strTemp = “0“; } if (strTemp == null) strTemp = “0“; m = new Integer(strTemp).intValue(); xx[i][0]=m; totalNum = m; } if (totalNum==0) totalNum=1; for ( i=0;i<4;i ){ xx[i][1] = 共2页。 1 2 : 下载本文示例代码


JSP生成jpeg图片用于投票JSP生成jpeg图片用于投票JSP生成jpeg图片用于投票JSP生成jpeg图片用于投票JSP生成jpeg图片用于投票JSP生成jpeg图片用于投票JSP生成jpeg图片用于投票JSP生成jpeg图片用于投票JSP生成jpeg图片用于投票JSP生成jpeg图片用于投票JSP生成jpeg图片用于投票JSP生成jpeg图片用于投票JSP生成jpeg图片用于投票JSP生成jpeg图片用于投票JSP生成jpeg图片用于投票
阅读(195) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~