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.