BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 创建画布 Graphics2D g = (Graphics2D) img.getGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //抗锯齿,避免栅格严重 g.fillRect(0, 0,width, height); //清除背景, 会将颜色设为白色 g.setColor(new Color(0, 0, 0)); //设置颜色为黑色 g.setFont(new Font("黑体", Font.PLAIN,18)); //设置字体 g.drawString("hi", x, y); //绘制文本 //画线 g2.draw(new Line2D.Double(x1, y1,x2,y2)); //利用形状绘图 Shape circle = new Arc2D.Double(x, y, widht, heithg, 0, 360, Arc2D.CHORD); //整圆 g.fill(circle); //填充 g.draw(circle); //绘制轮廓 //路径 GeneralPath path = new GeneralPath(); path.moveTo(x, y); path.lineTo(x2,y2); path.closePath(); g.fill(path); //填充 g.draw(path); //绘制轮廓 //注:路径支持直线和贝塞尔曲线,但残疾的是不支持圆弧, //如果需要对含有圆弧的区域进行填充就只能自己拆分成多边形拟合了 BufferedImage img = ImageIO.read(new File(imgPath)); //加载图像 ImageIO.write(img, "png", new File(fileName)); //保存图像 g.drawImage(img, x, y, null); //绘制图像,支持拉伸