This article describes the JSP implementation of the creation of the Chinese flag picture effect code. Share to everyone for your reference, specific as follows:
Picture screenshot as follows:
The specific code is as follows:
<%@ 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);
Draw chess face G.setcolor (java.awt.Color.red);
G.fillrect (0, 0, width, height); g.fiLlrect (x, m, width, height);
G.setcolor (Java.awt.Color.yellow);
Draw big stars Double ox = height*maxx, oy = Height*maxy;
G.fill (Createpentacle (OX,OY,HEIGHT*MAXR,-MATH.PI/2));
Draw a small pentagram 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.p
I/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);
}%>
Save the code as a JSP file and execute it under the run directory.
The code is tested under tomcat6.0
I hope this article will help you with JSP program design.