Java uses bufferedimage and graphics to draw.

Source: Internet
Author: User

Question: Most of the content on the web is repetitive and simple to describe. Note: content: Create a picture of a word, and the background color of the picture and the color of the word on the picture is required.

Solution: First Use the Graphics method SetColor set the color, and then use the class FillRect fill the background color, and then use the class SetColor set a color, and then the class of drawstring stooped. Imageio.write output picture. Finally, the resource is freed with the dispose of the class.

Local code:

int imagewidth = 200;
int imageheight = 200;
BufferedImage image = New BufferedImage (ImageWidth, ImageHeight, Bufferedimage.type_int_rgb);
Graphics graphics = Image.getgraphics ();
int fontSize = 100;
Font font = new Font ("Italic", Font.plain, fontSize);
Graphics.setfont (font);
Graphics.setcolor (New Color (246, 96, 0));
Graphics.fillrect (0, 0, imagewidth, imageheight);
Graphics.setcolor (New Color (255, 255, 255));
int strwidth = Graphics.getfontmetrics (). Stringwidth ("good");
Graphics.DrawString ("Good", FontSize-(STRWIDTH/2), fontSize + 30);
Imageio.write (Image, "PNG", New File ("D:\\abc.png"));
Graphics.dispose ();

Java uses bufferedimage and graphics to draw.

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.