Java用BufferedImage和Graphics畫圖
解決:先用Graphics的方法setColor設定一下顏色,然後再用該類的fillRect填充背景色,接著再用該類的setColor設定一下顏色,再接著就是用該類的drawString畫字了。ImageIO.write輸出圖片。最後用該類的dispose釋放資源。
區域性程式碼:
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("楷體", 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("好");
graphics.drawString("好", fontSize - (strWidth / 2), fontSize + 30);
ImageIO.write(image, "PNG", new File("D:\\abc.png"));
graphics.dispose();
最後歡迎大家訪問我的個人網站: 1024s