Jsp implementation generates the Chinese flag image effect code, jsp flag
This example describes how to use jsp to generate a Chinese flag image. We will share this with you for your reference. The details are as follows:
The image is as follows:
The 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 ("Expi Res ", 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. setRenderingHin T (RenderingHints. KEY_ANTIALIASING, RenderingHints. VALUE_ANTIALIAS_ON); // draw a chess face g. setColor (java. awt. color. red); g. fillRect (0, 0, width, height); // g. fillRect (50, 50, width, height); g. setColor (java. awt. color. yellow); // draw the star 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 [I Dx] * 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 (); required imageencoder encoder = required codec. 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) ;}%>
Save the code as a jsp file and place it in the running directory.
Code passed the test in tomcat6.0
I hope this article will help you with JSP program design.