jsp實現產生中國國旗圖片效果代碼_JSP編程

來源:互聯網
上載者:User

本文執行個體講述了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程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.