欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jsp实现生成中国国旗图片效果代码

程序员文章站 2023-08-24 16:11:13
本文实例讲述了jsp实现生成中国国旗图片效果代码。分享给大家供大家参考,具体如下: 图片截图如下: 具体代码如下: <%@ page languag...

本文实例讲述了jsp实现生成中国国旗图片效果代码。分享给大家供大家参考,具体如下:

图片截图如下:

jsp实现生成中国国旗图片效果代码

具体代码如下:

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%@ page contenttype="image/jpeg" import="java.awt.*,
java.awt.image.*,java.util.*,javax.imageio.*,java.awt.shape.*,java.awt.geom.*,com.sun.image.codec.jpeg.*" %>
<%
 out.clear();
 response.setcontenttype("image/jpeg");
 response.setheader("pragma","no-cache");
 response.setheader("cache-control","no-cache");
 response.setdateheader("expires", 0);
 int width =300;
 int height = width/3*2; 
   double maxr = 0.15, minr = 0.05; 
   double maxx = 0.50, maxy = 0.50; 
   double[] minx = {0.75, 0.85, 0.85, 0.75}; 
   double[] miny = {0.35, 0.45, 0.60, 0.70}; 
 bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb);
 java.awt.graphics2d g=(java.awt.graphics2d)image.creategraphics();
 g.clearrect(0, 0,width,height);
 g.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on);
 //画棋面
 g.setcolor(java.awt.color.red);
 g.fillrect(0, 0, width, height);
 //g.fillrect(50, 50, width, height);
 g.setcolor(java.awt.color.yellow);
 //画大五角星
    double ox = height*maxx, oy = height*maxy;
 g.fill(createpentacle(ox,oy,height*maxr,-math.pi/2)); 
 //画小五角星
    for(int idx =0;idx < 4;idx ++){ 
      double sx = minx[idx]*height, sy = miny[idx]*height; 
      double theta = math.atan2(oy-sy,ox-sx); 
      g.fill(createpentacle(sx,sy,height*minr,theta)); 
    } 
 g.dispose();
 out.clearbuffer(); 
 out = pagecontext.pushbody();
 //imageio.write(image, "jpeg", response.getoutputstream());
  servletoutputstream outstream = response.getoutputstream();
    jpegimageencoder encoder =jpegcodec.createjpegencoder(outstream);
    encoder.encode(image);
    outstream.close();
%>
<%!
 public static java.awt.shape createpentacle(double sx, double sy, double radius,double theta) {
 final double arc = math.pi / 5;
 final double rad = math.sin(math.pi / 10) / math.sin(3 * math.pi / 10);
 generalpath path = new generalpath();
 path.moveto(1, 0);
 for (int idx = 0; idx < 5; idx++) {
  path.lineto(rad * math.cos((1 + 2 * idx) * arc),rad * math.sin((1 + 2 * idx) * arc));
  path.lineto(math.cos(2 * (idx + 1) * arc),math.sin(2 * (idx + 1) * arc));
 }
 path.closepath();
 affinetransform atf = affinetransform.getscaleinstance(radius, radius);
 atf.translate(sx / radius, sy / radius);
 atf.rotate(theta);
 return atf.createtransformedshape(path);
 }
%>

将代码另存为jsp文件,放在运行目录下执行即可。

代码在tomcat6.0下测试通过

希望本文所述对大家jsp程序设计有所帮助。