Use JSP to directly draw pictures and output to IE

Source: Internet
Author: User
JS The following code successfully tested, directly copied into the JSP that can run

<%@ page import= "java.awt.*"%>
<%@ page import= "java.awt.image.*"%>
<%@ page import= "com.sun.image.codec.jpeg.*"%>
<%@ page import= "java.util.*"%>


<%
Test by Liboy

int width=400;
int height= 400;

BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb);

Graphics g = image.getgraphics ();
Graphics2D g2d = Image.creategraphics ();


G.setcolor (Color.White);
G.fillrect (0, 0, width, height);
G.setcolor (Color.Black);
G.drawrect (0,0,width-1,height-1);
Create Random Polygon
Polygon Poly = new Polygon ();
Random Random = new Random ();
for (int i=0; i<5; i++) {
Poly.addpoint (Random.nextint (width), random.nextint (height));
}
Fill Polygon
G.setcolor (Color.cyan);
G.fillpolygon (poly);
Dispose context
G.dispose ();


Response.reset ();
Response.setcontenttype ("Image/jpeg");

Servletoutputstream SOS = Response.getoutputstream ();
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (SOS);
Encoder.encode (image);

%>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.